public interface Api {
public boolean create(UserModel um);
}
public class Impl implements Api{
private DataSource ds = null;
public void setDs(DataSource ds){
this.ds = ds;
}
public boolean create(UserModel um) {
JdbcTemplate jt = new JdbcTemplate(ds);
jt.execute("insert into tbl_user (uuid,name) values('"+um.getUuid()+"','"+um.getName()+"')");
return false;
}
}
<bean name="api" class="com.bjpowernode.Spring3.jdbc.Impl"> <property name="ds" ref="dataSource"></property> </bean> <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property> <property name="url"><value>jdbc:oracle:thin:@localhost:1521:orcl</value></property> <property name="username"> <value>test</value> </property> <property name="password" value="test"/> </bean>
public static void main(String[] args)throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"applicationContext-jdbc.xml"});
Api api = (Api)context.getBean("api");
UserModel um = new UserModel();
um.setUuid("test1");
um.setName("test1");
api.create(um);
}
public boolean create(UserModel um1){
JdbcTemplate jt = new JdbcTemplate(ds);
final UserModel um = um1;
class myCallBack implements PreparedStatementCallback{
public Object doInPreparedStatement(PreparedStatement pstmt)
throws SQLException, DataAccessException {
pstmt.setString(1,um.getUuid());
pstmt.setString(2,um.getName());
System.out.println("dddddddd");
return pstmt.executeUpdate();
}
}
jt.execute("insert into tbl_user (uuid,name) values(?,?)",new myCallBack());
return false;
}
NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(ds);
Map paramMap = new HashMap();
paramMap.put("uuid",um.getUuid());
List list = jt.queryForList("select * from tbl_user where uuid=:uuid",paramMap);
Iterator it = list.iterator();
while(it.hasNext()){
Map map = (Map)it.next();
System.out.println("uuid="+map.get("uuid")+",name="+map.get("name"));
}
NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(ds);
Map paramMap = new HashMap();
paramMap.put("uuid",um1.getUuid());
RowMapper mapper = new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
UserModel um = new UserModel();
um.setName(rs.getString("name"));
um.setUuid(rs.getString("uuid"));
return um;
}
};
List list = jt.query("select * from tbl_user where uuid=:uuid",paramMap,mapper);
Iterator it = list.iterator();
while(it.hasNext()){
UserModel tempUm = (UserModel)it.next();
System.out.println("uuid="+tempUm.getUuid()+",name="+tempUm.getName());
}
<bean id="hbConfig" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"><ref local="dataSource"/></property> <property name="mappingResources"> <list> <value>com/lx/Parent.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>
public interface Api {
public boolean create(UserModel um);
}
public class Impl implements Api{
private SessionFactory sf = null;
public void setSf(SessionFactory sf){
this.sf = sf;
}
public boolean create(UserModel um){
HibernateTemplate ht = new HibernateTemplate(sf);
ht.save(um);
return false;
}
}
<bean name="api" class="com.bjpowernode.Spring3.h3.Impl"> <property name="sf" ref="hbConfig"></property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>oracle.jdbc.driver.OracleDriver</value> </property> <property name="url"> <value>jdbc:oracle:thin:@localhost:1521:orcl</value> </property> <property name="username"> <value>test</value> </property> <property name="password" value="test"/> </bean>
<bean id="hbConfig" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"><ref local="dataSource"/></property> <property name="mappingResources"> <list> <value>cn/bjpowernode/Spring3/h3/UserModel.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle8iDialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>
public static void main(String[] args)throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"applicationContext-h3.xml"});
Api api = (Api)context.getBean("api");
UserModel um = new UserModel();
um.setUuid("test1");
um.setName("test1");
api.create(um);
}
List<UserModel> list = ht.find("select o from UserModel o");
Object params[] = new Object[1];
params[0] = "test1";
List<UserModel> list = ht.find("select o from UserModel o where o.uuid=?",params);
Object params[] = new Object[1];
params[0] = "test1";
String names[] = new String[1];
names[0] = "uuid";
List<UserModel> list = ht.findByNamedParam("select o from UserModel o where o.uuid=:uuid",names,params);
HibernateCallback hcb =new HibernateCallback(){
public Object doInHibernate(Session session)throws HibernateException, SQLException {
Query q = session.createQuery("select o from UserModel o where o.uuid=:uuid");
q.setString("uuid", um.getUuid());
return q.list();
}
};
Collection<UserModel> list = (Collection<UserModel>)ht.execute(hcb);
public class Impl extends HibernateDaoSupport implements Api{
public Collection testQuery(){
List<UserModel> list = this.getHibernateTemplate().find("select o from UserModel o");
for(UserModel tempUm : list){
System.out.println("uuid=="+tempUm.getUuid()+",name="+tempUm.getName());
}
return list;
}
}
<bean name="api" class="com.bjpowernode.Spring3.h3.Impl"> <property name="sessionFactory" ref="hbConfig"></property> </bean>
Session session = getSession(getSessionFactory(), false);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有