Spring Boot集成MyBatis訪問數(shù)據(jù)庫的方法
基于spring boot開發(fā)的微服務(wù)應(yīng)用,與MyBatis如何集成?
集成方法
可行的方法有:
1.基于XML或者Java Config,構(gòu)建必需的對象,配置MyBatis。
2.使用MyBatis官方提供的組件,實(shí)現(xiàn)MyBatis的集成。
方法一
建議參考如下文章,完成集成的驗(yàn)證。
MyBatis學(xué)習(xí) 之 一、MyBatis簡介與配置MyBatis+Spring+MySql
基于Spring + Spring MVC + Mybatis 高性能web構(gòu)建
MyBatis學(xué)習(xí)總結(jié)(八)——Mybatis3.x與Spring4.x整合
由于不是本文的重點(diǎn),因此不附上樣例。
方法二
有如下步驟:
- 修改pom.xml,增加軟件依賴
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency>
- 修改application.yml,增加數(shù)據(jù)源的定義
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
- 修改application.yml,增加MyBatis的配置
mybatis:
type-aliases-package: com.example.domain.model
type-handlers-package: com.example.typehandler
configuration:
map-underscore-to-camel-case: true
default-fetch-size: 100
default-statement-timeout: 30
日志的配置
通過觀察日志,可有效的分析MyBatis生成的SQL,檢查SQL配置的正確性。
修改application.yml,增加如下配置
logging:
level:
net:
jackieathome:
db:
mapper: DEBUG
其中net.jackieathome.db.mapper下定義了訪問數(shù)據(jù)庫的mapper接口。
輸出的日志樣例如下
2017-04-16 11:32:23.266 DEBUG 27801 --- [io-11002-exec-1] n.j.db.mapper.UserMapper.createUser : ==> Preparing: insert into `user`(id, name, password) values(?, ?, ?)
2017-04-16 11:32:23.293 DEBUG 27801 --- [io-11002-exec-1] n.j.db.mapper.UserMapper.createUser : ==> Parameters: id1492313542(String), null, null
2017-04-16 11:32:23.366 DEBUG 27801 --- [io-11002-exec-1] n.j.db.mapper.UserMapper.createUser : <== Updates: 1
2017-04-16 11:32:23.372 DEBUG 27801 --- [io-11002-exec-1] n.j.db.mapper.UserMapper.findUserById : ==> Preparing: select * from `user` where id = ?
2017-04-16 11:32:23.373 DEBUG 27801 --- [io-11002-exec-1] n.j.db.mapper.UserMapper.findUserById : ==> Parameters: id1492313542(String)
2017-04-16 11:32:23.417 DEBUG 27801 --- [io-11002-exec-1] n.j.db.mapper.UserMapper.findUserById : <== Total: 1
事務(wù)的使用
依據(jù)MyBatis的官方文檔,允許用戶將事務(wù)交給Spring來管理,使用編程和注解來控制事務(wù)。這里以注解方式來舉例說明使用方法,樣例代碼如下:
1.mapper的定義,如下
package net.jackieathome.db.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import net.jackieathome.bean.User; @Mapper public interface UserMapper { // 創(chuàng)建用戶 void createUser(User user); // 查找用戶 User findUserById(@Param("id") String id); }
2.數(shù)據(jù)庫訪問的中間層代碼,對上述mapper進(jìn)行了封裝。
使用@Transactional標(biāo)記該類,表明該類的公有方法全部都啟用了事務(wù)的支持。關(guān)于@Transactional的使用,可以參考相關(guān)的官方文檔。
package net.jackieathome.dao; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import net.jackieathome.bean.User; import net.jackieathome.db.mapper.UserMapper; @Component @Transactional public class UserDao { @Autowired private UserMapper userMapper; /** * 重復(fù)插入相同的用戶數(shù)據(jù),確認(rèn)事務(wù)是否生效 */ public List<String> createBatch() { long time = System.currentTimeMillis() / 1000; User user = null; List<String> ids = new ArrayList<>(); String id = "id" + time; String name = "name" + time; String password = "password" + time; user = new User(); user.setId(id); user.setName(name); user.setPassword(password); userMapper.createUser(user); ids.add(id); user = new User(); user.setId(id); user.setName(name); user.setPassword(password); userMapper.createUser(user); ids.add(id); return ids; } }
3.業(yè)務(wù)層實(shí)現(xiàn)
package net.jackieathome.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import net.jackieathome.bean.User; import net.jackieathome.dao.UserDao; import net.jackieathome.db.mapper.UserMapper; @RestController public class UserController { @Autowired private UserMapper userMapper; @Autowired private UserDao userDao; @RequestMapping(method = RequestMethod.GET, value = "/user/create/batch") public List<User> createBatch() { try { userDao.createBatch(); } catch (Exception e) { } return userMapper.loadAllUsers(); } }
從實(shí)際測試看,上述事務(wù)的實(shí)現(xiàn)有效,可保證當(dāng)數(shù)據(jù)出現(xiàn)主鍵沖突時(shí),事務(wù)中的插入操作可全部撤銷,不會(huì)出現(xiàn)部分?jǐn)?shù)據(jù)插入成功、部分失敗的現(xiàn)象。
注意事項(xiàng):
由于注解事務(wù)的實(shí)現(xiàn)依賴Spring AOP,因此只有當(dāng)注入行為存在時(shí),注解事務(wù)的控制才會(huì)生效。
1.假如在上述UserController類中定義createBatch方法,并且使用注解@Transactional標(biāo)記,經(jīng)驗(yàn)證可確認(rèn)此時(shí)注解事務(wù)是無效的。
2.假如在上述UserDao中定義了多個(gè)公有方法,存在相互調(diào)用的行為,基于相同的原因,這些方法相互調(diào)用時(shí)注解事務(wù)并不會(huì)生效。如果確實(shí)需要保證事務(wù)可用,可以考慮調(diào)整類的設(shè)計(jì)或者使用編程的方式來控制事務(wù)。
以上所述是小編給大家介紹的Spring Boot集成MyBatis訪問數(shù)據(jù)庫的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring加載properties文件的兩種方式實(shí)例詳解
這篇文章主要介紹了Spring加載properties文件的兩種方式,需要的朋友可以參考下2018-02-02SpringBoot security安全認(rèn)證登錄的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot security安全認(rèn)證登錄的實(shí)現(xiàn)方法,也就是使用默認(rèn)用戶和密碼登錄的操作方法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02java使用CompletableFuture分批處理任務(wù)實(shí)現(xiàn)
本文主要介紹了java使用CompletableFuture分批處理任務(wù)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07springboot項(xiàng)目攔截前端請求中的特殊字符串(解決方案)
springboot項(xiàng)目中,需要對前端請求數(shù)據(jù)進(jìn)行過濾,攔截特殊字符,本文通過實(shí)例代碼給大家分享完美解決方案,感興趣的朋友一起看看吧2023-10-10Activiti工作流學(xué)習(xí)筆記之自動(dòng)生成28張數(shù)據(jù)庫表的底層原理解析
這篇文章主要介紹了Activiti工作流學(xué)習(xí)筆記之自動(dòng)生成28張數(shù)據(jù)庫表的底層原理解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03