今天學習了一下如何通過hibernate來實現對數據庫的增刪改查,下面來簡單介紹一下:
首先創建個student類:
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
|
package com.hibernate.model; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.table; @entity @table (name = "_teacher" ) public class teacher { private int id; private string name; private int age; private string birthplace; @id @generatedvalue @column (name = "_id" ) public int getid() { return id; } public void setid( int id) { this .id = id; } @column (name = "_name" ) public string getname() { return name; } public void setname(string name) { this .name = name; } @column (name = "_age" ) public int getage() { return age; } public void setage( int age) { this .age = age; } @column (name = "_birthplace" ) public string getbirthplace() { return birthplace; } public void setbirthplace(string birthplace) { this .birthplace = birthplace; } } |
然后創建個junit test case類型的teachertest:
先講講session.save()方法,也就是增:
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
|
package com.hibernate.model; import org.hibernate.query; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.cfg.annotationconfiguration; import org.junit.afterclass; import org.junit.beforeclass; import org.junit.test; public class teachertest { public static sessionfactory sf = null ; @beforeclass public static void beforeclass(){ try { sf = new annotationconfiguration().configure().buildsessionfactory(); //此處最好要獲取一下異常,因為annotation有一個bug有時出現有時不出現 } catch (exception e){ e.printstacktrace(); } finally { } } @test public void testsave() { //增 teacher t = new teacher(); t.setage( 23 ); t.setname( "moluo" ); t.setbirthplace( "huangshan" ); session session = sf.getcurrentsession(); session.begintransaction(); session.save(t); session.gettransaction().commit(); } @afterclass public static void afterclass(){ sf.close(); } } |
亮結果:
再講講session.delete()方法,也就是刪:
1
2
3
4
5
6
7
8
9
10
|
@test public void testdelete() { //刪 teacher t = new teacher(); t.setid( 2 ); session session = sf.getcurrentsession(); session.begintransaction(); session.delete(t); session.gettransaction().commit(); } |
亮結果:
接著再來session.get(),也就是查:
1
2
3
4
5
6
7
8
|
@test public void testget() { //get session session = sf.getcurrentsession(); session.begintransaction(); teacher t = (teacher)session.get(teacher. class , 1 ); system.out.println( "姓名為:" +t.getname()); session.gettransaction().commit(); } |
亮結果:
1
2
|
hibernate: select teacher0_._id as column1_1_0_, teacher0_._age as column2_1_0_, teacher0_._birthplace as column3_1_0_, teacher0_._name as column4_1_0_ from _teacher teacher0_ where teacher0_._id=? 姓名為:moluo |
再來另一種查詢方式:session.load():
1
2
3
4
5
6
7
8
|
@test public void testload() { //load session session = sf.getcurrentsession(); session.begintransaction(); teacher t = (teacher)session.load(teacher. class , 1 ); system.out.println( "姓名為:" +t.getname()); session.gettransaction().commit(); } |
亮結果:
1
2
|
hibernate: select teacher0_._id as column1_1_0_, teacher0_._age as column2_1_0_, teacher0_._birthplace as column3_1_0_, teacher0_._name as column4_1_0_ from _teacher teacher0_ where teacher0_._id=? 姓名為:moluo |
這里解釋一下這倆查詢之間的區別:首先當要查詢的對象不存在的時候,返回的信息是不同的。get方式會返回:java.lang.nullpointerexception
load方式會返回:org.hibernate.objectnotfoundexception:norowwiththegivenidentifierexists
其次,load返回的是代理對象,等真正用到的時候才會發出sql語句;另外get是直接從數據庫里加載數據,不存在延遲。
最后再講講最常用的更新方式,通過hql語句來更新:
1
2
3
4
5
6
7
8
9
10
11
|
@test public void testupdate() { //更新 session session = sf.getcurrentsession(); session.begintransaction(); string url = "update teacher t set t.birthplace = 'anhui' where id = 1" ; //注意這里的teacher必須是對象名字,而不是表名,t是對象的別名 query q = session.createquery(url); //這里導入的包是:import org.hibernate.query; q.executeupdate(); session.gettransaction().commit(); } |
亮結果:
通過更新,把摩羅我的籍貫從黃山更新成安徽了....
其實本身有session.update()這個更新方法的,但只是這個更新方法如果你每次只更新一部分列的話,這種更新方式也會把所有列都更新一遍,效率不是太高,所以就不怎么提倡使用,當然也有彌補方式,比如如果使用的是xml的話,可以在配置文件中某個不想被更新的列的property標簽里設置update="false";另外也可以在配置文件的class后面設置動態更新:dynamic-update="true";當然如果使用的是annotation,可以在不想更新的列上設置@column(update="false");其實這些都不靈活,使用hql才是最好的,所以這里就只貼出hql更新的代碼。
總結
以上就是本文關于hibernate通過session實現增刪改查操作實例解析的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/xingzhemoluo/article/details/39722213