- Spring 中获得session对象
- public class BaseDao extends HibernateDaoSupport {
- private Logger log = Logger.getLogger(this.getClass());
- public Session openSession() {
- return SessionFactoryUtils.getSession(getSessionFactory(), true);
- }
- /** 获得Query对象
- */
- public Query getQuery(String sql) throws Exception{
- Session session = this.openSession();
- Query query = session.createQuery(sql);
- return query;
- }
- /**
- * 获得Criteria对象
- */
- public Criteria getCriteria(Class clazz) throws Exception{
- Session session=this.openSession();
- Criteria criteria = session.createCriteria(clazz);
- return criteria;
- }
- }
- Test.java
- public class UserDao extends BaseDao implements IUser{
- Session session = this.openSession();;
- Transaction tx = session.beginTransaction();;
- String hql = "update User set nickname = ? where userId = ?";
- session.createQuery(hql).setString(0,nickname).setString(1,userID).executeUpdate
- ();
- tx.commit();;
- //session.close();
- }
Spring 中获得session对象
最新推荐文章于 2024-11-05 11:09:43 发布