java中,操作數(shù)據(jù)庫有jdbc、hibernate、mybatis等技術(shù),今天整理了下,來講一講下mybatis。也為自己整理下文檔;
hibernate是一個完全的orm框架,是完全面向?qū)ο蟮摹5怯捎谌慷际且詫ο蟮男问絹聿僮鲾?shù)據(jù)庫,sql代碼都是由框架自動生成,可操作性和靈活性比較mybatis都要差一些。所以,mybatis慢慢的變成多數(shù)開發(fā)的標準配置;
一、mybatis框架建設(shè)
mybatis的整體框架結(jié)構(gòu)如下圖所示,按照下圖的工程原樣建立即可(其中jar包在文章末尾提供)
二、編寫數(shù)據(jù)庫表對應(yīng)的實體類
首先,數(shù)據(jù)庫表結(jié)構(gòu)如下(mysql數(shù)據(jù)庫):
1、實體類user.java中寫入如下代碼:
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
|
public class user { @override public string tostring() { return "user [id=" + id + ", username=" + username + ", sex=" + sex + ", address=" + address + ", birthday=" + birthday + "]" ; } private int id; private string username; private string sex; private string address; private date birthday; public int getid() { return id; } public void setid( int id) { this .id = id; } public string getusername() { return username; } public void setusername(string username) { this .username = username; } public string getsex() { return sex; } public void setsex(string sex) { this .sex = sex; } public string getaddress() { return address; } public void setaddress(string address) { this .address = address; } public date getbirthday() { return birthday; } public void setbirthday(date birthday) { this .birthday = birthday; } } |
2、向sqlmapconfig.xml中寫入配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<!doctype configuration public "-//mybatis.org//dtd config 3.0//en" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <!-- 和spring整合后 environments配置將廢除--> <environments default = "development" > <environment id= "development" > <!-- 使用jdbc事務(wù)管理--> <transactionmanager type= "jdbc" /> <!-- 數(shù)據(jù)庫連接池--> <datasource type= "pooled" > <property name= "driver" value= "com.mysql.jdbc.driver" /> <property name= "url" value= "jdbc:mysql://localhost:3306/mybatis?characterencoding=utf-8" /> <property name= "username" value= "root" /> <property name= "password" value= "123456" /> </datasource> </environment> </environments> <mappers> <mapper resource= "sqlmap/user.xml" ></mapper> </mappers> </configuration> |
注意:sqlmapconfig中的mysql賬戶與密碼需要改成你自己的賬號密碼
三、在config包下建立log4j.properties(公用文件)
在文件中寫入默認參數(shù):
1
2
3
4
5
6
|
# global logging configuration,建議開發(fā)環(huán)境中要用debug log4j.rootlogger=debug, stdout # console output... log4j.appender.stdout=org.apache.log4j.consoleappender log4j.appender.stdout.layout=org.apache.log4j.patternlayout log4j.appender.stdout.layout.conversionpattern=%5p [%t] - %m%n |
四、開始編寫user.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?xml version= "1.0" encoding= "utf-8" ?> <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <!-- namespace命名空間,為了對sql語句進行隔離,方便管理 ,mapper開發(fā)dao方式,使用namespace有特殊作用 --> <mapper namespace= "test" > <!-- 在mapper.xml文件中配置很多的sql語句,執(zhí)行每個sql語句時,封裝為mappedstatement對象 mapper.xml以statement為單位管理sql語句 --> <!-- 根據(jù)id查詢用戶信息 --> <!-- id:唯一標識 一個statement #{}:表示 一個占位符,如果#{}中傳入簡單類型的參數(shù),#{}中的名稱隨意 parametertype:輸入 參數(shù)的類型,通過#{}接收parametertype輸入 的參數(shù) resulttype:輸出結(jié)果 類型,不管返回是多條還是單條,指定單條記錄映射的pojo類型 --> <select id= "finduserbyid" parametertype= "int" resulttype= "cn.qkp.mybatis.po.user" > select * from user where id=#{id} </select> </mapper> |
以上配置完成,整個mybatis基礎(chǔ)框架就算是搭建完畢了。我們就可以使用代碼來操作數(shù)據(jù)庫了。我們先對數(shù)據(jù)庫進行簡單的查詢操作;
=========================使用mybatis!===============================
一、在first包中建立測試類(mybatisfrist.java)
內(nèi)容代碼為:
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
|
private sqlsessionfactory sqlsessionfactory; //存儲sqlsessionfactory 對象 @before public void star() throws exception{ //設(shè)置配置路徑,mybitis是以sqlmapconfig.xml為主路徑。因為sqlmapconfig中的mapper關(guān)聯(lián)了user.xml //因為在config根目錄下,所以可以直接引用而不用帶config string resource = "sqlmapconfig.xml" ; //sqlmapconfig.xml讀給輸入流,使用mybitis的resources類下的getresourceasstream實現(xiàn) inputstream inputstream = resources.getresourceasstream(resource); //創(chuàng)建mybitis的sqlsessionfactory工廠類 sqlsessionfactory = new sqlsessionfactorybuilder().build(inputstream); } @test public void test(){ //通過工廠類打開數(shù)據(jù)接口 sqlsession sqlsession = sqlsessionfactory.opensession(); //設(shè)置接收對象 user user = null ; try { //查詢數(shù)據(jù)selectone為查詢一條的方法第一個參數(shù)是user.xml中的namespace.id;第二個參數(shù)是user配置文件中的#{id} user = sqlsession.selectone( "test.finduserbyid" , 1 ); } catch (exception e) { // todo: handle exception } finally { sqlsession.close(); //讀完要關(guān)閉sqlsession } system.out.println(user); //打印輸出 } |
運行上述代碼,結(jié)果為:
以上便成功連通數(shù)據(jù)庫,并獲取到指定數(shù)據(jù)!
ps:以上是查詢單一數(shù)值的查詢方法,如果要查詢一組數(shù)據(jù)使用sqlsession的selectlist方法,接收一個list;代碼如下:
在user.xml中:
1
2
3
|
<select id= "selectbyname" parametertype= "string" resulttype= "cn.qkp.po.user" > select * from user where username like '%${value}%' </select> |
ps:在xml中,使用#{}占位符表示系統(tǒng)自動會進行java類型和jdbc類型自動轉(zhuǎn)換;而${}表示原樣輸出,使用${}會造成sql注入漏洞攻擊,慎用;
在mybatisrun代碼中:
1
2
3
4
5
6
7
|
@test public void star2(){ sqlsession sqlsession = sqlsessionfactory.opensession(); list<user> user = sqlsession.selectlist( "test.selectbyname" , "小明" ); sqlsession.close(); system.out.println(user.get( 0 )); } |
結(jié)果為:
相關(guān)的jar包連接:http://pan.baidu.com/s/1i4tijd7
以上就是基本的框架搭建和簡單的查詢操作,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持服務(wù)器之家!
原文鏈接:http://www.cnblogs.com/mylydg/p/6419498.html