mybatis是一個支持普通sql查詢,存儲過程和高級映射的優秀持久層框架。mybatis消除了幾乎所有的jdbc代碼和參數的手工設置以及對結果集的檢索封裝。mybatis可以使用簡單的xml或注解用于配置和原始映射,將接口和java的pojo(plain old java objects,普通的java對象)映射成數據庫中的記錄。
1.創建工程,導入jar包
創建一個java工程或者web工程都可以,然后導入mybatis的jar包和依賴包還有數據庫的jar包,本人使用oracle10g數據庫
mybatis-3.2.2.jar 核心驅動
asm-3.3.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
javassist-3.17.1-ga.jar
log4j-1.2.17.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
classes12.jar oracle10g的jar包
2.創建連接數據庫的核心配置文件sqlmapconfig.xml
在src目錄底下,創建sqlmapconfig.xml文件
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <!doctype configuration public "-//mybatis.org//dtd config 3.0//en" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <!--配置數據源,環境--> <!--可以配置多個環境,一個測試時候用的,一個上線時候用的--> <environments default = "test" > <!--測試環境--> <environment id= "test" > <!--配置事務 : jdbc事務/managed交給容器的事務--> <transactionmanager type= "jdbc" ></transactionmanager> <!--數據源 : pooled池化/unpooled非池化/jndi密碼加密,安全性高--> <datasource type= "pooled" > <property name= "driver" value= "oracle.jdbc.oracledriver" /> <property name= "url" value= "jdbc:oracle:thin:@localhost:1521:orcl" /> <property name= "username" value= "scott" /> <property name= "password" value= "luogg" /> </datasource> </environment> <!--服務器環境--> <environment id= "deploy" > <transactionmanager type= "jdbc" ></transactionmanager> <datasource type= "pooled" > <property name= "driver" value= "oracle.jdbc.oracledriver" /> <property name= "url" value= "jdbc:oracle:thin:@localhost:1521:orcl" /> <property name= "username" value= "scott" /> <property name= "password" value= "luogg" /> </datasource> </environment> </environments> <mappers> <mapper resource= "com/luogg/mapper/personmapper.xml" /> </mappers> </configuration> |
3.在src底下創建test文件夾,在test文件夾下創建testmybatis.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
|
package test; import com.luogg.domain.person; import org.apache.ibatis.io.resources; import org.apache.ibatis.session.sqlsession; import org.apache.ibatis.session.sqlsessionfactory; import org.apache.ibatis.session.sqlsessionfactorybuilder; import org.junit.test; import java.io.ioexception; import java.io.inputstream; import java.util.list; /** * created by luogg on 2017/2/17. */ public class testmybatis { @test public void init() throws ioexception { /** * 測試數據庫的連接 * 1.定義一個string類型的變量resource,指向剛才配置的連接數據庫的xml文件 * 2.創建一個輸入流,來讀取我們的數據庫配置文件 * 3.輸入流創建工廠. * 4.有了工廠之后open工廠 */ string resource = "sqlmapconfig.xml" ; inputstream is = resources.getresourceasstream(resource); sqlsessionfactory factory = new sqlsessionfactorybuilder().build(is); sqlsession session = factory.opensession(); } |
此時,我們可以進行單元測試了,看看session有沒有被創建,綠色表示創建成功,那么我們接下來創建數據庫表,并且寫sql語句
4.創建數據庫,并寫入數據
1
2
3
4
5
6
7
8
|
create table person( id number( 2 ), name varchar2( 20 ), sex number( 2 ), age number( 3 ) ) -- 寫入數據,點擊下方鎖子按鈕,然后點擊+ select * from person for update |
5.在src底下創建com.luogg.domain包,在包下創建person.java的實體bean
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
|
package com.luogg.domain; /** * created by luogg on 2017/2/17. */ public class person { private string name; private int sex; private int age; private int id; @override public string tostring() { return "person{" + "name='" + name + '\ '' + ", sex=" + sex + ", age=" + age + ", id=" + id + '}' ; } public int getid() { return id; } public void setid( int id) { this .id = id; } public string getname() { return name; } public void setname(string name) { this .name = name; } public int getsex() { return sex; } public void setsex( int sex) { this .sex = sex; } public int getage() { return age; } public void setage( int age) { this .age = age; } } |
6.創建與數據庫對應的映射文件,在src下創建com.luogg.mapper包下創建personmapper.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?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" > <!--配置命名空間,命名空間+ .id 是唯一的sql語句標示符--> <mapper namespace= "com.luogg.mapper.personmapper" > <!--查詢所有數據,參數有id,resulttype結果集,parametertype參數--> <!--注意 : sql語句中如果有要填寫集合的,比如查詢所有數據,返回一個person的結果集,那么resulttype參數直接寫 路徑+集合的類型 比如: 返回一個person集合,那么就填寫person bean所在的路徑+person--> <select id= "find" resulttype= "com.luogg.domain.person" > select * from person </select> </mapper> |
7.在核心配置文件sqlmapconfig.xml中做一個映射,讓其識別我們的寫了sql語句的配置文件
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <!doctype configuration public "-//mybatis.org//dtd config 3.0//en" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <!--配置數據源,環境--> <!--可以配置多個環境,一個測試時候用的,一個上線時候用的--> <environments default = "test" > <!--測試環境--> <environment id= "test" > <!--配置事務 : jdbc事務/managed交給容器的事務--> <transactionmanager type= "jdbc" ></transactionmanager> <!--數據源 : pooled池化/unpooled非池化/jndi密碼加密,安全性高--> <datasource type= "pooled" > <property name= "driver" value= "oracle.jdbc.oracledriver" /> <property name= "url" value= "jdbc:oracle:thin:@localhost:1521:orcl" /> <property name= "username" value= "scott" /> <property name= "password" value= "luogg" /> </datasource> </environment> <!--服務器環境--> <environment id= "deploy" > <transactionmanager type= "jdbc" ></transactionmanager> <datasource type= "pooled" > <property name= "driver" value= "oracle.jdbc.oracledriver" /> <property name= "url" value= "jdbc:oracle:thin:@localhost:1521:orcl" /> <property name= "username" value= "scott" /> <property name= "password" value= "luogg" /> </datasource> </environment> </environments> <!--映射文件mapper--> <mappers> <mapper resource= "com/luogg/mapper/personmapper.xml" /> </mappers> </configuration> |
8.回到測試類testmybatis.java,訪問配置文件中的sql語句并返回結果集
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
|
package test; import com.luogg.domain.person; import org.apache.ibatis.io.resources; import org.apache.ibatis.session.sqlsession; import org.apache.ibatis.session.sqlsessionfactory; import org.apache.ibatis.session.sqlsessionfactorybuilder; import org.junit.test; import java.io.ioexception; import java.io.inputstream; import java.util.list; /** * created by luogg on 2017/2/17. */ public class testmybatis { @test public void init() throws ioexception { /** * 測試數據庫的連接 * 1.定義一個string類型的變量resource,指向剛才配置的連接數據庫的xml文件 * 2.創建一個輸入流,來讀取我們的數據庫配置文件 * 3.輸入流創建工廠. * 4.有了工廠之后open工廠 * 5.通過session訪問配置文件中的sql語句 */ string resource = "sqlmapconfig.xml" ; inputstream is = resources.getresourceasstream(resource); sqlsessionfactory factory = new sqlsessionfactorybuilder().build(is); sqlsession session = factory.opensession(); //如何訪問personmapper.xml中的sql語句呢? 命名空間+ .id list<person> list = session.selectlist( "com.luogg.mapper.personmapper.find" ); system.out.println(list.size()); for (person p : list){ system.out.println(p); } } } |
運行結果 :
以上所述是小編給大家介紹的mybatis框架入門學習教程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/luogg/archive/2017/02/17/6410125.html