Hibernate事务增删改查(第一部分)
生活随笔
收集整理的這篇文章主要介紹了
Hibernate事务增删改查(第一部分)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.Hibernate中的事物
1.1 設置事物隔離級別
Hibernate中書屋的隔離級別都是用字節存儲的二進制 十進制 read uncommitted 讀未提交 0001 1 read committed 讀已提交 0010 2 repeatable read 重復讀 0100 4 mysql默認級別 serializable 可串行化 1000 8 ? 配置數據庫的隔離級別直接寫十進制的值即可 在主配置文件中配置數據庫的隔離級別 <property name="Hibernate.connection.isolation">4</property>1.2 Hibernate管理事務的方法
//開啟事務Transcation t = session.beginTranscation();try{//.......寫自己的代碼(do som work....)//提交事務 t.commit();}catch(Exception e){//異常:事務回滾if(t!=null){t.rollback();}}1.3 Hibernate得到Session對象的方式
Configuration c = new Configuration();c.Configure();SessionFactory factory = c.bulidSessionFactory();//方式一:Session session = factory.openSession();//方式二:Session session = factory.getCurrentSession();1.4 openSession 和 getCurrentSession的區別
openSession()和getCurrentSession() 1.openSession()每次調用生成新的SessiongetCurrentSession()每次調用都使用的同一個session(與當前線程綁定的session)2.openSession()使用不需要配置getCurrentSession()必須要配置,在主配置文件hibernate.cfg.xml中配置<property name="hibernate.current_session_context_class">thread</property>3. openSession()使用之后可以關閉 getCurrentSession()不能關閉4.Session要線程安全建議使用getCurrentSession(); ?建議用getCurrentSession()1.5 HibernateUtils封裝的工具類
public class HibernateUtils {private static SessionFactory sessionFactory=null;static {Configuration configuration = new Configuration();configuration.configure();sessionFactory = configuration.buildSessionFactory();}public static Session openSession() {return sessionFactory.openSession();}public static Session getCurrentSession() {return sessionFactory.getCurrentSession();}//openSession產生的session要關閉public static void closeSession(Session session) {session.close();}}2.Hibernate的增刪改查
2.1 插入(save)
Session session = HibernateUtils.getCurrentSession();Transaction t = session.beginTransaction();try {User user =new User();user.setUser_name("皮皮蝦");user.setUser_pwd("123456");session.save(user);t.commit();} catch (Exception e) {// TODO: handle exception t.rollback();}2.2 修改(update)
Session session = HibernateUtils.getCurrentSession();Transaction t = session.beginTransaction();try {//根據唯一標識來修改 //如果對象的某些屬性沒有設值,修改后這些屬性的值為null User user =new User();user.setUser_id(1);user.setUser_name("看誰在睡覺?");session.update(user);t.commit();} catch (Exception e) {// TODO: handle exception t.rollback();}2.3 刪除(delete)
Session session = HibernateUtils.getCurrentSession();Transaction t = session.beginTransaction();try {//根據唯一表示來修改User user =new User();user.setUser_id(1);session.delete(user);t.commit();} catch (Exception e) {// TODO: handle exception t.rollback();}2.4 查詢(Query)
2.4.1 以oid查詢 //參數1:查詢的類型:T.class 參數2:唯一標識id值session.find(T.class,id);session.get(T.class,id);session.load(T.class,id); Session session = HibernateUtils.getCurrentSession(); Transaction transaction = session.beginTransaction(); try {//Student student = session.find(Student.class, 5);Student student = session.get(Student.class, 5);//Student student = session.load(Student.class, 5); System.out.println(student);transaction.commit(); } catch (Exception e) {// TODO: handle exception transaction.rollback(); } 2.4.2 對象導航圖查詢 常用于多表查詢? 2.4.3 原生sql語句查詢
SQL(結構化查詢語言 Structor Query Language)
Hibernate5.2以后用 session.createNativeQuery(sql語句);Hibernate5.2以前用session.createSQLQuery(sql語句);//取sessionSession session = HibernateUtils.getCurrentSession();Transaction t= session.beginTransaction();try {//使用原生sql語句查詢查詢所有//createNativeQuery是hibernate5.2版本之后建議用/*NativeQuery<User> query= session.createNativeQuery("select * from user", User.class);*///createSQLQuery是Hibernate5.2版本之前使用的,但是目前或者將來不會使用了NativeQuery<User> query=session.createSQLQuery("select * from user");//設置從第幾個位置開始取數據query.setFirstResult(0);//設置每次最多取多少條數據query.setMaxResults(4);//query.list() 與query.getResultList()等價System.out.println("=--==="+query.list());t.commit();} catch (Exception e) {// TODO: handle exceptionif (t!=null) {t.rollback();}} } ? 2.4.4 HQL查詢()1.基礎查詢 Session session = HibernateUtils.getCurrentSession();Transaction transaction = session.beginTransaction();try {//基本語法:from 包名+類名(如果整個程序中只有這么一個類,直接省略包名,默認查找該類)String HQL = " from com.xx.domain.Student";//參數1:HQL語句 參數2:結果類型Query<Student> query = session.createQuery(HQL, Student.class);List<Student> list = query.list()System.out.println(list); transaction.commit();} catch (Exception e) {// TODO: handle exception transaction.rollback();}
2.別名查詢
查詢所有的信息Session session = HibernateUtils.getCurrentSession();Transaction transaction = session.beginTransaction();try {String HQL = "select s from Student s";Query<Student> query = session.createQuery(HQL, Student.class);List<Student> list = query.list();System.out.println(list); transaction.commit();} catch (Exception e) {// TODO: handle exception transaction.rollback();}
查詢持久化類的某個成員變量
Session session = HibernateUtils.getCurrentSession();Transaction transaction = session.beginTransaction();try {String HQL = "select s.name from Student s where id=:id"; Query query = session.createQuery(HQL); query.setParameter("id", 2); System.out.println(query.list()); transaction.commit(); } catch (Exception e) { // TODO: handle exception transaction.rollback(); }
? 3.條件查詢
方式一:Session session = HibernateUtils.getCurrentSession();Transaction transaction = session.beginTransaction();try {//:user_id表示占位String HQL="from com.xx.domain.Student where id=:id";Query<User> query = session.createQuery(HQL, Student.class);//參數1:表示屬性名 ,參數2:屬性名對應的值query.setParameter("id", 2);System.out.println(query.uniqueResult());transaction.commit();} catch (Exception e) {// TODO: handle exceptionif (transaction!=null) {transaction.rollback();}} 方式二:Session session = HibernateUtils.getCurrentSession();Transaction transaction = session.beginTransaction();try {String HQL = " from Student where id=?";Query<Student> query = session.createQuery(HQL, Student.class);//?號的位置從0開始query.setParameter(0, 2);System.out.println(query.uniqueResult());transaction.commit();} catch (Exception e) {// TODO: handle exception transaction.rollback();}
?
?
轉載于:https://www.cnblogs.com/a77355699/p/7872584.html
總結
以上是生活随笔為你收集整理的Hibernate事务增删改查(第一部分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache beam其他学习记录
- 下一篇: 通过uwsgi+nginx启动flask