什么是通用mapper
通用mapper就是為了解決單表增刪改查,基于mybatis的插件。開發(fā)人員不需要編寫sql,不需要在dao中增加方法,只要寫好實體類,就能支持相應的增刪改查方法。
關于mybatis,大部分人都很熟悉。mybatis 是一款優(yōu)秀的持久層框架,它支持定制化 sql、存儲過程以及高級映射。mybatis 避免了幾乎所有的 jdbc 代碼和手動設置參數(shù)以及獲取結果集。mybatis 可以使用簡單的 xml 或注解來配置和映射原生信息,將接口和 java 的 pojos(plain old java objects,普通的 java對象)映射成數(shù)據(jù)庫中的記錄。
不管是ddd(domain driven design,領域驅(qū)動建模)還是分層架構的風格,都會涉及到對數(shù)據(jù)庫持久層的操作,本文將會講解spring boot集成mybatis如何實現(xiàn)通用mapper。
spring boot集成mybatis
引入依賴
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
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> <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.3 . 1 </version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> </dependency> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> </dependency> <dependency> <groupid>com.zaxxer</groupid> <artifactid>hikaricp</artifactid> </dependency> |
可以看到如上關于mybatis引入了 mybatis-spring-boot-starter
,由mybatis提供的starter。
數(shù)據(jù)庫配置
在application.yml中增加如下配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
spring: datasource: hikari: connection-test-query: select 1 minimum-idle: 1 maximum-pool-size: 5 pool-name: dbcp1 driver- class -name: com.mysql.jdbc.driver url: jdbc:mysql: //localhost:3306/test?autoreconnect=true&usessl=false&useunicode=true&characterencoding=utf-8 username: user password: pwd type: com.zaxxer.hikari.hikaridatasource schema[ 0 ]: classpath:/init.sql initialize: true |
可以看到,我們配置了hikari和數(shù)據(jù)庫的基本信息。在應用服務啟動時,會自動初始化classpath下的sql腳本。
1
2
3
4
5
|
create table if not exists `test` ( `id` bigint( 20 ) unsigned not null , `local_name` varchar( 128 ) not null , primary key (`id`) ) engine=innodb default charset=utf8; |
在sql腳本中,我們創(chuàng)建了一張 test 表。
到這里,后面我們一般需要配置mybatis映射的xml文件和實體類的路徑。根據(jù)mybatis generator 自動生成代碼。包括 xxmapper.java , xxentity.java , xxmapper.xml 。這里我們就不演示了,直接進入下一步的通用mapper實現(xiàn)。
通用mapper的使用
引入依賴
1
2
3
4
5
|
<dependency> <groupid>tk.mybatis</groupid> <artifactid>mapper</artifactid> <version> 3.4 . 0 </version> </dependency> |
通用mapper的作者 abel533 ,有興趣可閱讀源碼。
配置通用mapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import tk.mybatis.spring.mapper.mapperscannerconfigurer; import java.util.properties; @configuration public class mybatismapperscannerconfig{ @bean public mapperscannerconfigurer mapperscannerconfigurer(){ mapperscannerconfigurer mapperscannerconfigurer = new mapperscannerconfigurer(); mapperscannerconfigurer.setsqlsessionfactorybeanname( "sqlsessionfactory" ); mapperscannerconfigurer.setbasepackage( "com.blueskykong.mybatis.dao" ); //掃描該路徑下的dao properties properties = new properties(); properties.setproperty( "mappers" , "com.blueskykong.mybatis.config.basedao" ); //通用dao properties.setproperty( "notempty" , "false" ); properties.setproperty( "identity" , "mysql" ); mapperscannerconfigurer.setproperties(properties); return mapperscannerconfigurer; } } |
在配置中,設定了指定路徑下的dao,并指定了通用dao。需要注意的是, mapperscannerconfigurer 來自于 tk.mybatis.spring.mapper 包下。
1
2
3
4
5
|
basedao import tk.mybatis.mapper.common.mapper; import tk.mybatis.mapper.common.mysqlmapper; public interface basedao<t> extends mapper<t>,mysqlmapper<t>{ } |
通用mapper接口,其他接口繼承該接口即可。
創(chuàng)建實體
我們需要添加 test 表對應的實體。
1
2
3
4
5
6
7
8
9
10
11
|
@data @table (name = "test" ) @allargsconstructor @noargsconstructor public class testmodel{ @id @column (name = "id" ) @generatedvalue (strategy = generationtype.identity) private integer id; private string localname; } |
其中, @table(name = "test")
注解指定了該實體對應的數(shù)據(jù)庫表名。
配置文件
1
2
3
|
mybatis: configuration: map-underscore-to-camel- case : true |
為了更好地映射java實體和數(shù)據(jù)庫字段,我們指定下劃線駝峰法的映射配置。
testdao編寫
1
2
3
4
|
public interface testdaoextends basedao<testmodel>{ @insert ( "insert into test(id, local_name) values(#{id}, #{localname})" ) integerinserttestmodel(testmodel testmodel); } |
testdao 繼承自 basedao ,并指定了泛型為對應的 testmodel 。 testdao 包含繼承的方法,如:
1
2
3
4
5
6
|
int deletebyprimarykey(integer userid); int insert(user record); int insertselective(user record); userselectbyprimarykey(integer userid); int updatebyprimarykeyselective(user record); int updatebyprimarykey(user record); |
還可以自定義一些方法,我們在上面自定義了一個 inserttestmodel 方法。
service層和控制層
本文略過這兩層,比較簡單,讀者可以參見本文對應的源碼地址。
結果驗證
我們在插入一條數(shù)據(jù)之后,查詢對應的實體。對應執(zhí)行的結果也都是成功,可以看到控制臺的如下日志信息:
c.b.mybatis.dao.testdao.inserttestmodel : ==> preparing: insert into test(id, local_name) values(?, ?)
c.b.mybatis.dao.testdao.inserttestmodel : ==> parameters: 5953(integer), testname(string)
c.b.mybatis.dao.testdao.inserttestmodel : <== updates: 1
c.b.m.dao.testdao.selectbyprimarykey : ==> preparing: select id,local_name from test where id = ?
c.b.m.dao.testdao.selectbyprimarykey : ==> parameters: 5953(integer)
c.b.m.dao.testdao.selectbyprimarykey : <== total: 1
spring boot集成mybatis實現(xiàn)通用mapper到此就大功告成。
小結
mybatis是持久層非常常用的組件,spring boot倡導約定優(yōu)于配置,特別是很多xml的配置。當然還有很多同學使用spring data。相比而言,我覺得mybatis的sql比spring data更加靈活,至于具體比較不在此討論。
本文對應的源碼地址:
https://github.com/keets2012/spring-boot-samples/tree/master/mybatis-demo
總結
以上所述是小編給大家介紹的spring boot集成mybatis實現(xiàn)通用mapper的配置及使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網(wǎng)站的支持!
原文鏈接:http://blueskykong.com/2018/08/22/mybatis/