国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Hibernate+JDBC實(shí)現(xiàn)批量插入、更新及刪除的方法詳解

Hibernate+JDBC實(shí)現(xiàn)批量插入、更新及刪除的方法詳解

2021-01-29 10:56淺淺請(qǐng)留步 Java教程

這篇文章主要介紹了Hibernate+JDBC實(shí)現(xiàn)批量插入、更新及刪除的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Hibernate與JDBC針對(duì)數(shù)據(jù)庫(kù)的批量操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Hibernate JDBC實(shí)現(xiàn)批量插入、更新及刪除的方法。分享給大家供大家參考,具體如下:

一、批量插入(兩種方式)

1. 通過(guò)Hibernate緩存

如果這樣寫(xiě)代碼進(jìn)行批量插入(初始設(shè)想):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.anlw.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import com.anlw.entity.Student;
public class SessionUtil {
  Configuration conf = null;
  ServiceRegistry st = null;
  SessionFactory sf = null;
  Session sess = null;
  Transaction tx = null;
  public void HIbernateTest() {
    conf = new Configuration().configure();
    st = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
    sf = conf.buildSessionFactory(st);
    try {
      sess = sf.openSession();
      tx = sess.beginTransaction();
      for (int i = 0; i < 10; i++) {
        Student s = new Student();
        s.setAge(i + 1);
        s.setName("test");
        sess.save(s);
      }
      tx.commit();
    } catch (Exception e) {
      if (tx != null) {
        tx.rollback();
      }
    } finally {
      sess.close();
      sf.close();
    }
  }
  public static void main(String[] args) {
    new SessionUtil().HIbernateTest();
  }
}

如果數(shù)據(jù)量太大,會(huì)有可能出現(xiàn)內(nèi)存溢出的異常;

小知識(shí):

(1).Hibernate一級(jí)緩存,對(duì)其容量沒(méi)有限制,強(qiáng)制使用,由于所有的對(duì)象都被保存到這個(gè)緩存中,內(nèi)存總會(huì)達(dá)到一定數(shù)目時(shí)出現(xiàn)內(nèi)存溢出的情況;
(2).Hibernate二級(jí)緩存可以進(jìn)行大小配置;

要解決內(nèi)存溢出的問(wèn)題,就應(yīng)該定時(shí)的將Sessiion緩存中的數(shù)據(jù)刷到數(shù)據(jù)庫(kù),正確的批量插入方式:

(1).設(shè)置批量尺寸(博主至今還沒(méi)有明白下面這個(gè)屬性和flush()方法的區(qū)別)

?
1
<property name="hibernate.jdbc.batch_size">2</property>

配置這個(gè)參數(shù)的原因就是盡量少讀數(shù)據(jù)庫(kù),該參數(shù)值越大,讀數(shù)據(jù)庫(kù)的次數(shù)越少,速度越快;上面這個(gè)配置,是Hibernate是等到程序積累了100個(gè)sql之后在批量提交;

(2).關(guān)閉二級(jí)緩存(這個(gè)博主也不是很明白)

?
1
<property name="hibernate.cache.use_second_level_cache">false</property>

除了Session級(jí)別的一級(jí)緩存,Hibernate還有一個(gè)SessionFactory級(jí)別的二級(jí)緩存,如果啟用了二級(jí)緩存,從機(jī)制上來(lái)說(shuō),Hibernate為了維護(hù)二級(jí)緩存,在批量插入時(shí),hibernate會(huì)將對(duì)象納入二級(jí)緩存,性能上就會(huì)有很大損失,也可能引發(fā)異常,因此最好關(guān)閉SessionFactory級(jí)別的二級(jí)緩存;

(3).在一二設(shè)置完成的基礎(chǔ)上,清空Session級(jí)別的一級(jí)緩存;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.anlw.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import com.anlw.entity.Student;
public class SessionUtil {
  Configuration conf = null;
  ServiceRegistry st = null;
  SessionFactory sf = null;
  Session sess = null;
  Transaction tx = null;
  public void HIbernateTest() {
    conf = new Configuration().configure();
    st = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
    sf = conf.buildSessionFactory(st);
    try {
      sess = sf.openSession();
      tx = sess.beginTransaction();
      for (int i = 0; i < 10; i++) {
        Student s = new Student();
        s.setAge(i + 1);
        s.setName("test");
        sess.save(s);
        if(i%100 == 0){       //以每100個(gè)數(shù)據(jù)作為一個(gè)處理單元
          sess.flush();      //保持與數(shù)據(jù)庫(kù)數(shù)據(jù)的同步
          sess.clear();      //清楚Session級(jí)別的一級(jí)緩存的全部數(shù)據(jù),及時(shí)釋放占用的內(nèi)存
        }
      }
      tx.commit();
    } catch (Exception e) {
      if (tx != null) {
        tx.rollback();
      }
    } finally {
      sess.close();
      sf.close();
    }
  }
  public static void main(String[] args) {
    new SessionUtil().HIbernateTest();
  }
}

2. 繞過(guò)Hibernate,直接調(diào)用JDBC API

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.anlw.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
public class SessionUtil {
  Configuration conf = null;
  ServiceRegistry st = null;
  SessionFactory sf = null;
  Session sess = null;
  Transaction tx = null;
  public void HIbernateTest() {
    conf = new Configuration().configure();
    st = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
    sf = conf.buildSessionFactory(st);
    try {
      sess = sf.openSession();
      tx = sess.beginTransaction();
      //執(zhí)行Work對(duì)象指定的操作,即調(diào)用Work對(duì)象的execute()方法
      //Session會(huì)把當(dāng)前使用的數(shù)據(jù)庫(kù)連接傳給execute()方法
      sess.doWork(new Work() {
        @Override
        public void execute(Connection arg0) throws SQLException {//需要注意的是,不需要調(diào)用close()方法關(guān)閉這個(gè)連接
          //通過(guò)JDBC API執(zhí)行用于批量插入的sql語(yǔ)句
          String sql = "insert into student(name,age) values(?,?)";
          PreparedStatement ps = arg0.prepareStatement(sql);
          for(int i=0;i<10;i++){
            ps.setString(1, "kobe");
            ps.setInt(2,12);
            ps.addBatch();
          }
          ps.executeBatch();
        }
      });
      tx.commit();
    } catch (Exception e) {
      if (tx != null) {
        tx.rollback();
      }
    } finally {
      sess.close();
      sf.close();
    }
  }
  public static void main(String[] args) {
    new SessionUtil().HIbernateTest();
  }
}

注意:通過(guò)JDBC API中的PreparedStatement接口來(lái)執(zhí)行sql語(yǔ)句,sql語(yǔ)句涉及到的數(shù)據(jù)不會(huì)被加載到Session的緩存中,因此不會(huì)占用內(nèi)存空間,因此直接調(diào)用JDBC API批量化插入的效率要高于Hibernate緩存的批量插入;

更新&&刪除

語(yǔ)法格式:(HQL)

update  |     delete from? <ClassName> [where where_conditions]

1>在from子句中,from關(guān)鍵字是可選的,即完全可以不寫(xiě)from關(guān)鍵字
2>在from子句中,只能有一個(gè)類(lèi)名,可以在該類(lèi)名后指定別名
3>不能在批量HQL語(yǔ)句中使用連接,顯示或者隱式的都不行,但可以在where子句中使用子查詢(xún)
4>整個(gè)where子句是可選的,where子句的語(yǔ)法sql語(yǔ)句中where子句的語(yǔ)法完全相同
5>Query.executeUpdate()方法返回一個(gè)整型值,該值是受此操作影響的記錄數(shù)量,由于hibernate的底層操作實(shí)際上是由JDBC完成的,因此,如果有批量update或delete操作被轉(zhuǎn)換成多條update或delete語(yǔ)句,(關(guān)聯(lián)或者繼承映射),該方法只能返回最后一條sql語(yǔ)句影響的記錄行數(shù),不是所有的記錄行數(shù),需要注意;

二、批量更新(兩種方式)

1. 使用Hibernate直接進(jìn)行批量更新

(1)方式1:(Hibernate的HQL直接支持update/delete的批量更新語(yǔ)法)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.anlw.util;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class SessionUtil {
  Configuration conf = null;
  ServiceRegistry st = null;
  SessionFactory sf = null;
  Session sess = null;
  Transaction tx = null;
  public void HIbernateTest() {
    conf = new Configuration().configure();
    st = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
    sf = conf.buildSessionFactory(st);
    try {
      sess = sf.openSession();
      tx = sess.beginTransaction();
      //在HQL查詢(xún)中使用update進(jìn)行批量更新,下面的的語(yǔ)句是HQL語(yǔ)句,不是sql語(yǔ)句
      Query query = sess.createQuery("update Student set name = 'www'");
      query.executeUpdate();
      tx.commit();
    } catch (Exception e) {
      if (tx != null) {
        tx.rollback();
      }
    } finally {
      sess.close();
      sf.close();
    }
  }
  public static void main(String[] args) {
    new SessionUtil().HIbernateTest();
  }
}

(2)方式2:(強(qiáng)烈不推薦)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.anlw.util;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import org.hibernate.CacheMode;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
import com.anlw.entity.Student;
public class SessionUtil {
  Configuration conf = null;
  ServiceRegistry st = null;
  SessionFactory sf = null;
  Session sess = null;
  Transaction tx = null;
  public void HIbernateTest() {
    conf = new Configuration().configure();
    st = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
    sf = conf.buildSessionFactory(st);
    try {
      sess = sf.openSession();
      tx = sess.beginTransaction();
      //查詢(xún)表中的所有數(shù)據(jù)
      ScrollableResults student = sess.createQuery("from Student")
          .setCacheMode(CacheMode.IGNORE)
          .scroll(ScrollMode.FORWARD_ONLY);
      int count = 0;
      while(student.next()){
        Student s = (Student)student.get(0);
        s.setName("haha");
        if(++count%3 == 0){
          sess.flush();
          sess.clear();
        }
      }
      tx.commit();
    } catch (Exception e) {
      if (tx != null) {
        tx.rollback();
      }
    } finally {
      sess.close();
      sf.close();
    }
  }
  public static void main(String[] args) {
    new SessionUtil().HIbernateTest();
  }
}

通過(guò)這種方式,雖然可以執(zhí)行批量更新,但效果非常不好,執(zhí)行效率不高,需要先執(zhí)行數(shù)據(jù)查詢(xún),然后再執(zhí)行數(shù)據(jù)更新,而且這種更新將是逐行更新,即每更新一行記錄,都要執(zhí)行一條update語(yǔ)句,性能非常低;

2. 繞過(guò)Hibernate,調(diào)用JDBC API

(1)方式1:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.anlw.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
public class SessionUtil {
  Configuration conf = null;
  ServiceRegistry st = null;
  SessionFactory sf = null;
  Session sess = null;
  Transaction tx = null;
  public void HIbernateTest() {
    conf = new Configuration().configure();
    st = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
    sf = conf.buildSessionFactory(st);
    try {
      sess = sf.openSession();
      tx = sess.beginTransaction();
      //執(zhí)行Work對(duì)象指定的操作,即調(diào)用Work對(duì)象的execute()方法
      //Session會(huì)把當(dāng)前使用的數(shù)據(jù)庫(kù)連接傳給execute()方法
      sess.doWork(new Work() {
        @Override
        public void execute(Connection arg0) throws SQLException {//需要注意的是,不需要調(diào)用close()方法關(guān)閉這個(gè)連接
          String sql = "update student set name = 'oracle'";
          //創(chuàng)建一個(gè)Satement對(duì)象
          Statement st = arg0.createStatement();
          //調(diào)用JDBC的update進(jìn)行批量更新
          st.executeUpdate(sql);
        }
      });
      tx.commit();
    } catch (Exception e) {
      if (tx != null) {
        tx.rollback();
      }
    } finally {
      sess.close();
      sf.close();
    }
  }
  public static void main(String[] args) {
    new SessionUtil().HIbernateTest();
  }
}

(2)方式2:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.anlw.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
public class SessionUtil {
  Configuration conf = null;
  ServiceRegistry st = null;
  SessionFactory sf = null;
  Session sess = null;
  Transaction tx = null;
  public void HIbernateTest() {
    conf = new Configuration().configure();
    st = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
    sf = conf.buildSessionFactory(st);
    try {
      sess = sf.openSession();
      tx = sess.beginTransaction();
      //執(zhí)行Work對(duì)象指定的操作,即調(diào)用Work對(duì)象的execute()方法
      //Session會(huì)把當(dāng)前使用的數(shù)據(jù)庫(kù)連接傳給execute()方法
      sess.doWork(new Work() {
        @Override
        public void execute(Connection arg0) throws SQLException {//需要注意的是,不需要調(diào)用close()方法關(guān)閉這個(gè)連接
          String sql = "update student set name = ? where name=?";
          PreparedStatement ps = arg0.prepareStatement(sql);
          for(int i=0;i<10;i++){
            ps.setString(1,"tom");
            ps.setString(2, "oracle");
            ps.addBatch();
          }
          ps.executeBatch();
        }
      });
      tx.commit();
    } catch (Exception e) {
      if (tx != null) {
        tx.rollback();
      }
    } finally {
      sess.close();
      sf.close();
    }
  }
  public static void main(String[] args) {
    new SessionUtil().HIbernateTest();
  }
}

三、批量刪除(兩種方式)

1. 使用Hibernate直接進(jìn)行批量刪除

(1)方式1:(Hibernate的HQL直接支持update/delete的批量更新語(yǔ)法)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.anlw.util;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class SessionUtil {
  Configuration conf = null;
  ServiceRegistry st = null;
  SessionFactory sf = null;
  Session sess = null;
  Transaction tx = null;
  public void HIbernateTest() {
    conf = new Configuration().configure();
    st = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
    sf = conf.buildSessionFactory(st);
    try {
      sess = sf.openSession();
      tx = sess.beginTransaction();
      //在HQL查詢(xún)中使用delete進(jìn)行批量刪除,下面的的語(yǔ)句是HQL語(yǔ)句,不是sql
      Query query = sess.createQuery("delete Student");//也可以是delete from,from關(guān)鍵字是可選的,可以不要,加條件的時(shí)候可以指定類(lèi)的別名
      query.executeUpdate();
      tx.commit();
    } catch (Exception e) {
      if (tx != null) {
        tx.rollback();
      }
    } finally {
      sess.close();
      sf.close();
    }
  }
  public static void main(String[] args) {
    new SessionUtil().HIbernateTest();
  }
}

(2)方式2:(強(qiáng)烈不推薦)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.anlw.util;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import org.hibernate.CacheMode;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
import com.anlw.entity.Student;
public class SessionUtil {
  Configuration conf = null;
  ServiceRegistry st = null;
  SessionFactory sf = null;
  Session sess = null;
  Transaction tx = null;
  public void HIbernateTest() {
    conf = new Configuration().configure();
    st = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
    sf = conf.buildSessionFactory(st);
    try {
      sess = sf.openSession();
      tx = sess.beginTransaction();
      //查詢(xún)表中的所有數(shù)據(jù)
      ScrollableResults student = sess.createQuery("from Student")
          .setCacheMode(CacheMode.IGNORE)
          .scroll(ScrollMode.FORWARD_ONLY);
      int count = 0;
      while(student.next()){
        Student s = (Student)student.get(0);
        sess.delete(s);
      }
      tx.commit();
    } catch (Exception e) {
      if (tx != null) {
        tx.rollback();
      }
    } finally {
      sess.close();
      sf.close();
    }
  }
  public static void main(String[] args) {
    new SessionUtil().HIbernateTest();
  }
}

通過(guò)這種方式,雖然可以執(zhí)行批量刪除,但效果非常不好,執(zhí)行效率不高,需要先執(zhí)行數(shù)據(jù)查詢(xún),然后再執(zhí)行數(shù)據(jù)刪除,而且這種刪除將是逐行刪除,即每刪除一行記錄,都要執(zhí)行一條delete語(yǔ)句,性能非常低;

2. 繞過(guò)Hibernate,調(diào)用JDBC API

(1)方式1:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.anlw.util;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
import com.anlw.entity.Student;
public class SessionUtil {
  Configuration conf = null;
  ServiceRegistry st = null;
  SessionFactory sf = null;
  Session sess = null;
  Transaction tx = null;
  public void HIbernateTest() {
    conf = new Configuration().configure();
    st = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
    sf = conf.buildSessionFactory(st);
    try {
      sess = sf.openSession();
      tx = sess.beginTransaction();
      sess.doWork(new Work() {
        @Override
        public void execute(Connection arg0) throws SQLException {
          String sql = "delete from student where age > 5"; //mysql中刪除語(yǔ)句不能省略from
          Statement st = arg0.createStatement();
          st.executeUpdate(sql);
        }
      });
      tx.commit();
    } catch (Exception e) {
      if (tx != null) {
        tx.rollback();
      }
    } finally {
      sess.close();
      sf.close();
    }
  }
  public static void main(String[] args) {
    new SessionUtil().HIbernateTest();
  }
}

2)方式2:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.anlw.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
import com.anlw.entity.Student;
public class SessionUtil {
  Configuration conf = null;
  ServiceRegistry st = null;
  SessionFactory sf = null;
  Session sess = null;
  Transaction tx = null;
  public void HIbernateTest() {
    conf = new Configuration().configure();
    st = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
    sf = conf.buildSessionFactory(st);
    try {
      sess = sf.openSession();
      tx = sess.beginTransaction();
      sess.doWork(new Work() {
        @Override
        public void execute(Connection arg0) throws SQLException {
          String sql = "delete from student where age = ?"; //mysql中刪除語(yǔ)句不能省略from
          PreparedStatement ps = arg0.prepareStatement(sql);
          for(int i=0;i<10;i++){
            if(i%2 == 0){
              ps.setInt(1, i);
              ps.addBatch();
            }
            ps.executeBatch();
          }
        }
      });
      tx.commit();
    } catch (Exception e) {
      if (tx != null) {
        tx.rollback();
      }
    } finally {
      sess.close();
      sf.close();
    }
  }
  public static void main(String[] args) {
    new SessionUtil().HIbernateTest();
  }
}

希望本文所述對(duì)大家基于Hibernate的java程序設(shè)計(jì)有所幫助。

原文鏈接:http://blog.csdn.net/an_2016/article/details/51759890

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品久久久 | 羞羞的视频在线免费观看 | 色婷婷精品国产一区二区三区 | 欧美日韩视频在线第一区 | 亚洲精品成人在线 | 国产成人免费 | 国产欧美日韩综合精品一区二区 | 91在线视频观看 | 黄免费看| 欧美日韩国产综合视频 | 成人在线视频网 | 91中文在线观看 | 国产一区视频在线 | 色爱区成人综合网 | 国产激情在线 | 91久久久久久久久久久久久 | 97久久香蕉国产线看观看 | 亚洲成人免费在线 | 欧美日韩一区二区三区不卡视频 | 欧美日韩一二三区 | 吴梦梦到粉丝家实战华中在线观看 | 久久久在线 | 欧美激情一区二区三级高清视频 | 亚洲毛片网站 | 日韩不卡一二三 | 亚洲 成人 av | 骚视频在线观看 | 久久精品国产91精品亚洲高清 | 日韩av一区二区在线观看 | 日本在线观看一区二区 | 精品国产乱码久久久久久久软件 | 国产精品国产a级 | 久久久久a | 国产第一亚洲 | 美欧一级片| 最新日韩av | 中文字幕的 | 精品视频一区二区三区在线观看 | 成人综合网站 | 久久久精品网站 | 在线播放国产精品 |