如下代码:
(注意该类继承自HibernateDaoSupport ,要在applicationContext.xml中将sessionFactory注入此类中)
public class DaoUtil extends HibernateDaoSupport {
public Object executeMySQL(final String sql){
System.out.println(sql);
return getHibernateTemplate().execute( new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Connection CurConn = session.connection();
PreparedStatement ps = CurConn.prepareStatement(sql);
ps.execute();
ps.close();
session.flush();
return null;
}
} );
}
public Object executeBetchSQL(final ArrayList sqlList){
return getHibernateTemplate().execute( new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Connection CurConn = session.connection();
int count = sqlList.size();
for(int i=0;i//System.out.println(sqlList.get(i));
PreparedStatement ps = CurConn.prepareStatement(sqlList.get(i));
ps.execute();
ps.close();
session.flush();
}
return null;
}
} );
}
public static DaoUtil getFromApplicationContext(
ApplicationContext ctx) {
return (DaoUtil) ctx.getBean("DaoUtil");
}
}
可以采用hibernate 的标准sql查询,直接执行的是纯sql
楼上说的对啊 getSession.createSqlQuery(“select dateadd(month,6,getdate()) ”);
HibernateDaoSupport 是spring提供的对hibernate支持的dao层类,hibernate本身是没有的。select dateadd(month,6,getdate()) 就是一个取database server时间的再+6个月的查询语句,和你app本身的domain对象一点关系都没有,完全没有必要放在hibernate的session中执行。直接用jdbc执行就好了。
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....