javascript
Spring Boot + Mybatis 快速整合
引言
最近在工作結(jié)束后抽時(shí)間學(xué)習(xí)了一下mybatis的知識,因?yàn)橹坝袑W(xué)習(xí)過,但是經(jīng)久不用,也未曾踏實(shí)地整理,因此有所淡忘。
super meeting會議管理系統(tǒng)是我廠最近開發(fā)的一套會議預(yù)約平臺。持久層框架經(jīng)討論,選為靈活優(yōu)秀的半自動持久層框架Mybatis。
關(guān)于mybatis的有點(diǎn)和簡介不做本系列學(xué)習(xí)博客的重點(diǎn),在此不做記錄。
學(xué)習(xí)的方式采用視頻+實(shí)踐的學(xué)練組合方式。結(jié)合一直接觸的spring boot框架,重溫mybatis的使用和各種應(yīng)用場景的解決方案。
學(xué)習(xí)的視頻連接:https://www.bilibili.com/video/av21272940
項(xiàng)目搭建
pom依賴
項(xiàng)目采用maven管理的spring boot方式,添加pom依賴:
此處需要引入Mybatis的spring boot start包 和 mysql的驅(qū)動依賴
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version> </dependency> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope> </dependency>這兩項(xiàng)依賴可以直接在spring boot初始化界面的Search for dependencies 中找到:https://start.spring.io/
如何搭建spring boot項(xiàng)目,請參考《SpringBoot————快速搭建springboot項(xiàng)目》
建庫建表
為方便日后學(xué)習(xí)mapper的使用,此處直接建立兩張關(guān)聯(lián)表:dept(部門表),employee(員工表)
插入測試數(shù)據(jù):
數(shù)據(jù)源配置
servert.port=8080 #mysql spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver此處先簡單配置數(shù)據(jù)源,mybatis相關(guān)配置稍后介紹。
定義接口與Mapper配置文件
根據(jù)mybatis的接口式編程方式,定義數(shù)據(jù)庫操作方法的api可以與Mapper進(jìn)行動態(tài)綁定。
接口與mapper.xml文件進(jìn)行綁定后,調(diào)用接口中的增刪改查時(shí),Mybatis會為用戶創(chuàng)建一個(gè)代理對象,由這個(gè)代理對象執(zhí)行操作。
Mapper接口
@Mapper public interface EmpDao {/*** 根據(jù)員工id查找員工*/Employee getEmpById(Integer empId); }@Mapper代表這是一個(gè)mybatis可以識別的Mapper接口。
mybatis的mapper-locations配置項(xiàng)、@Mapper注解、<mapper>標(biāo)簽的namespace屬性,三者可以使mybatis找到任意目錄下的Mapper配置文件以及與其綁定的接口。因此,接口名稱可以根據(jù)喜好使用Dao或者M(jìn)apper結(jié)尾,并沒有限制。
Mapper.xml配置
添加mybatis 的mapper.xml文件約束:
官網(wǎng)查找地址:http://www.mybatis.org/mybatis-3/getting-started.html
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.dao.EmpDao"><select id="getEmpById" resultType="com.example.demo.entity.Employee">SELECT * FROM employee WHERE emp_id = #{empId}</select> </mapper>namespace指定綁定接口的全類名;id標(biāo)識接口中與之綁定的方法;resultType表示返回值類型;#{empId}此處的empId代表接口中傳入的參數(shù)(這里注意,如果是一個(gè)參數(shù)mybatis不會特殊處理;如果是多個(gè)參數(shù),參數(shù)必須指定參數(shù)名,否則會報(bào)錯(cuò)!后續(xù)文章會說明這個(gè)問題。)
小提示:添加dtd約束后,如果在書寫xml的時(shí)候沒有任何提示,則可以在聯(lián)網(wǎng)狀態(tài)下Ctrl+左鍵點(diǎn)擊約束中的鏈接“http://mybatis.org/dtd/mybatis-3-mapper.dtd”,這樣可以加載xml的提示內(nèi)容。
全局配置
最開始的spring 整合 mybatis的版本都是使用mybatis-config.xml配置文件,進(jìn)行數(shù)據(jù)源,以及各種配置信息的配置。
當(dāng)然這些配置項(xiàng)依然不變,變化的是配置項(xiàng)書寫的方式,以及去掉了mybatis-config全局配置文件。
官網(wǎng)中提供了有關(guān)spring boot需要的mybatis的全部配置信息:
http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/index.html
而configuration屬性,更是包含了其他更加詳細(xì)的settings項(xiàng):
NOTE?configuration property cannot be used at the same time with the?config-location.
在configuration中,包含諸如mapUnderscoreToCamelCase(下劃線轉(zhuǎn)駝峰)、lazyLoadingEnabled(懶加載)、aggressiveLazyLoading(侵入式懶加載)、cacheEnabled(全局緩存)等配置項(xiàng)都是configuration中的子配置項(xiàng)。
項(xiàng)目搭建所需的簡單配置項(xiàng)如下:
#mybatis mybatis.mapper-locations=classpath:com/example/demo/dao/xml/*.xml mybatis.configuration.mapUnderscoreToCamelCase=true mybatis.type-aliases-package=com.example.demo.entity最后觀察一下我們的項(xiàng)目結(jié)構(gòu):
測試
為了簡便測試,我們定義一個(gè)controller直接調(diào)用dao層的方法,以此來進(jìn)行方法的測試,web接口采用RESTful?風(fēng)格。
@RestController @RequestMapping("/emps") public class EmpController {@Autowiredprivate EmpDao empDao;@GetMapping("/{empId}")public Employee getEmpById(@PathVariable Integer empId) {return empDao.getEmpById(empId);} }瀏覽器調(diào)用接口:
?
綜上,就是關(guān)于spring boot + mybatis 的簡單整合與使用,后續(xù)會進(jìn)行更加深入的學(xué)習(xí)和使用。
總結(jié)
以上是生活随笔為你收集整理的Spring Boot + Mybatis 快速整合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 多线程 —— ThreadLo
- 下一篇: Eclipse使用————Working