mybatis是支持定制化sql、存儲過程以及高級映射的優(yōu)秀的持久層框架,避免了幾乎所有的jdbc代碼和手動設(shè)置參數(shù)以及獲取結(jié)果集。spring boot是能支持快速創(chuàng)建spring應(yīng)用的java框架。本文通過一個例子來學(xué)習(xí)spring boot如何集成mybatis,而且過程中不需要xml配置。
創(chuàng)建數(shù)據(jù)庫
本文的例子使用mysql數(shù)據(jù)庫,首先創(chuàng)建一個用戶表,執(zhí)行sql語句如下:
1
2
3
4
5
6
|
create table if not exists user ( `id` int ( 10 ) not null auto_increment, `name` varchar( 50 ) null default null , `age` int ( 2 ) not null , primary key (id) ) |
工程目錄結(jié)構(gòu)與依賴配置
首先新建一個maven工程,并配置pom依賴,本例中所用到的依賴如下:
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
|
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.4 . 2 .release</version> <relativepath /> </parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version> 1.1 . 1 </version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version> 5.1 . 40 </version> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> |
然后創(chuàng)建一下工程目錄結(jié)構(gòu),如下圖所示:
代碼文件內(nèi)容
0. 創(chuàng)建配置文件——application.properties
寫入一下內(nèi)容:
1
2
3
4
|
spring.datasource.driver- class -name=com.mysql.jdbc.driver spring.datasource.url=jdbc:mysql: //localhost:3306/test?usessl=false&useunicode=true&characterencoding=utf-8 spring.datasource.username=root spring.datasource.password= 123456 |
1. 創(chuàng)建pojo——entity/user.java
這是一個pojo,包含了id, name, age三個屬性,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.xyz.dbtest.entity; public class user { private int id; private string name; private int age; 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 getage() { return age; } public void setage( int age) { this .age = age; } } |
2. 創(chuàng)建一個數(shù)據(jù)層接口——service/userservice.java
這是一個mapper類,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.xyz.dbtest.dao; import com.xyz.dbtest.entity.user; import org.apache.ibatis.annotations.mapper; import org.apache.ibatis.annotations.result; import org.apache.ibatis.annotations.results; import org.apache.ibatis.annotations.select; import java.util.list; @mapper //1 public interface userdao { @results ({ //2 @result (property = "id" , column = "id" ), //2 @result (property = "name" , column = "name" ), @result (property = "age" , column = "age" ) }) @select ( "select * from user where age = #{age}" ) //3 list<user> get( int age); @insert ( "insert into user(name, age) values (#{name}, #{age})" ) //3 void insert(user user); } |
//1 @mapper將userdao聲明為一個mapper接口
//2 @results是結(jié)果映射列表,@result中property是user類的屬性名,colomn是數(shù)據(jù)庫表的字段名
//3 @select, @insert 分別代表了執(zhí)行的真實sql
3. 創(chuàng)建一個用戶服務(wù)——service/userservice.java
這是一個服務(wù)類bean,提供三個函數(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
|
package com.xyz.dbtest.service; import com.xyz.dbtest.dao.userdao; import com.xyz.dbtest.entity.user; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import java.util.list; @service //聲明成一個spring bean public class userservice { @autowired //連接到userdao bean private userdao userdao; public string show() { return "hello world!" ; } public list<user> showdao( int age) { return userdao.get(age); } public string insert(string name, int age) { //插入一條記錄 user user = new user(); user.setname(name); user.setage(age); userdao.insert(user); return "insert ( \"" +name+ "\", age" +age+ ") ok!" ; } } |
4. 常見一個web controller——controller/usercontroller.java
這是一個spring web的controller類,引入了spring-boot-starter-web依賴,代碼如下:
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 com.xyz.dbtest.controller; import com.xyz.dbtest.service.userservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.responsebody; import org.springframework.web.bind.annotation.restcontroller; @restcontroller //聲明為一個restful的controller public class usercontroller { @autowired //自動連接到userservice bean private userservice userservice; @requestmapping (value = "/show" ) public string show() { return userservice.show(); } @requestmapping (value = "/showdao" ) public object showdao( int age) { return userservice.showdao(age); } @requestmapping (value= "/insert" ) public string insert(string name, int age) { return userservice.insert(name, age); } } |
5. 創(chuàng)建啟動類——main/startapp.java
這是一個spring boot啟動類。代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.xyz.dbtest.main; import org.mybatis.spring.annotation.mapperscan; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; @springbootapplication (scanbasepackages = "com.xyz.dbtest" ) //1 @mapperscan (basepackages = "com.xyz.dbtest.dao" ) //2 public class startapp { public static void main(string[] args) { springapplication.run(startapp. class , args); } } |
//1 由于startapp類位于基礎(chǔ)包的自包中,因此需要設(shè)置scanbasepackage
//2 設(shè)置mapper接口所在的包
運行結(jié)果
運行sql語句創(chuàng)建數(shù)據(jù)庫表后,運行startapp類。啟動成功如下圖所示
測試show服務(wù),結(jié)果如下:
測試showdao服務(wù),在輸入url時需要將參數(shù)打包進url,結(jié)果如下:
不帶參數(shù)時,訪問錯誤:
帶參數(shù)時,訪問成功,由于數(shù)據(jù)庫中沒有記錄,所以結(jié)果是一個空列表:
測試insert服務(wù)
再次測試showdao服務(wù)
結(jié)語
通過本文的例子可以看出,使用spring boot集成mybatis幾乎不用任何配置工作,能有效加快開發(fā)效率!
代碼庫地址:github地址
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/xuyuzhuang1991/article/details/54143945