hibernate使用session操作数据库

来源:互联网 发布:轰炸商务印书馆知乎 编辑:程序博客网 时间:2024/05/17 02:59

前面一章介绍spring boot使用hibernate,采用的是EntityManager实现hibernate数据交互,现在这里改用Session

@Autowired

private SessionFactory sessionFactory;

private Session getSession() {
       return this.sessionFactory.openSession();

}


@Override
public User select(String id) {
// TODO Auto-generated method stub
Session session=getSession();
String hql="from User where id=:id";
Query query=session.createQuery(hql);
query.setParameter("id", id);
return (User) query.list().get(0);
}

原创粉丝点击