hibernate中的事务管理是可以让开发者能够使用一个统一的操作界面,使得自己的项目可以在不同的环境和容器之间方便地移植。事务对象通过Session创建。
Configuration config=new Configuration().configure();
SessionFactory sessionFactory=config.bulidSessionFactory();(采用了线程安全设计,一个数据库共享一个SessionFactory)
Session session=
sessionFactory.openSession();(非线程安全,一个Session实例同时只可有一个线程使用)
Transaction ts=session.beginTransaction();
Query query=session.createQuerty("from Kcb where kch=198");(或者可以为:
Query query=session.createQuerty("from Kcb where kch=?"); Query.setString(0,"要设置的值");还可以为:Query query=session.createQuerty("from Kcb where kch=:kchValue"); Query.setString("kchValue","要设置的课程号值"))
如果为int整型则把setString改为setInt;通用的方法为setParameter()
如果为一个list集合则:
Query query=session.createQuerty("from Kcb where kch=198"); List list=query.list();