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;
}
}oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@localhost:1521:orcl test
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());
}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;
}
}oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@localhost:1521:orcl test
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);
}Listlist = ht.find("select o from UserModel o");
Object params[] = new Object[1]; params[0] = "test1"; Listlist = 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"; Listlist = 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 list = (Collection)ht.execute(hcb); public class Impl extends HibernateDaoSupport implements Api{
public Collection testQuery(){
List list = this.getHibernateTemplate().find("select o from UserModel o");
for(UserModel tempUm : list){
System.out.println("uuid=="+tempUm.getUuid()+",name="+tempUm.getName());
}
return list;
}
} Session session = getSession(getSessionFactory(), false);
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26660100/viewspace-717240/ ,如需转载,请注明出处,否则将追究法律责任。