javascript
Spring Boot + Mybatis 快速整合
引言
最近在工作結(jié)束后抽時(shí)間學(xué)習(xí)了一下mybatis的知識(shí),因?yàn)橹坝袑W(xué)習(xí)過(guò),但是經(jīng)久不用,也未曾踏實(shí)地整理,因此有所淡忘。
super meeting會(huì)議管理系統(tǒng)是我廠(chǎng)最近開(kāi)發(fā)的一套會(huì)議預(yù)約平臺(tái)。持久層框架經(jīng)討論,選為靈活優(yōu)秀的半自動(dòng)持久層框架Mybatis。
關(guān)于mybatis的有點(diǎn)和簡(jiǎn)介不做本系列學(xué)習(xí)博客的重點(diǎn),在此不做記錄。
學(xué)習(xí)的方式采用視頻+實(shí)踐的學(xué)練組合方式。結(jié)合一直接觸的spring boot框架,重溫mybatis的使用和各種應(yīng)用場(chǎng)景的解決方案。
學(xué)習(xí)的視頻連接:https://www.bilibili.com/video/av21272940
項(xiàng)目搭建
pom依賴(lài)
項(xiàng)目采用maven管理的spring boot方式,添加pom依賴(lài):
此處需要引入Mybatis的spring boot start包 和 mysql的驅(qū)動(dòng)依賴(lài)
<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)依賴(lài)可以直接在spring boot初始化界面的Search for dependencies 中找到:https://start.spring.io/
如何搭建spring boot項(xiàng)目,請(qǐng)參考《SpringBoot————快速搭建springboot項(xiàng)目》
建庫(kù)建表
為方便日后學(xué)習(xí)mapper的使用,此處直接建立兩張關(guān)聯(lián)表:dept(部門(mén)表),employee(員工表)
插入測(cè)試數(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此處先簡(jiǎn)單配置數(shù)據(jù)源,mybatis相關(guān)配置稍后介紹。
定義接口與Mapper配置文件
根據(jù)mybatis的接口式編程方式,定義數(shù)據(jù)庫(kù)操作方法的api可以與Mapper進(jìn)行動(dòng)態(tài)綁定。
接口與mapper.xml文件進(jìn)行綁定后,調(diào)用接口中的增刪改查時(shí),Mybatis會(huì)為用戶(hù)創(chuàng)建一個(gè)代理對(duì)象,由這個(gè)代理對(duì)象執(zhí)行操作。
Mapper接口
@Mapper public interface EmpDao {/*** 根據(jù)員工id查找員工*/Employee getEmpById(Integer empId); }@Mapper代表這是一個(gè)mybatis可以識(shí)別的Mapper接口。
mybatis的mapper-locations配置項(xiàng)、@Mapper注解、<mapper>標(biāo)簽的namespace屬性,三者可以使mybatis找到任意目錄下的Mapper配置文件以及與其綁定的接口。因此,接口名稱(chēng)可以根據(jù)喜好使用Dao或者M(jìn)apper結(jié)尾,并沒(méi)有限制。
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指定綁定接口的全類(lèi)名;id標(biāo)識(shí)接口中與之綁定的方法;resultType表示返回值類(lèi)型;#{empId}此處的empId代表接口中傳入的參數(shù)(這里注意,如果是一個(gè)參數(shù)mybatis不會(huì)特殊處理;如果是多個(gè)參數(shù),參數(shù)必須指定參數(shù)名,否則會(huì)報(bào)錯(cuò)!后續(xù)文章會(huì)說(shuō)明這個(gè)問(wèn)題。)
小提示:添加dtd約束后,如果在書(shū)寫(xiě)xml的時(shí)候沒(méi)有任何提示,則可以在聯(lián)網(wǎng)狀態(tài)下Ctrl+左鍵點(diǎn)擊約束中的鏈接“http://mybatis.org/dtd/mybatis-3-mapper.dtd”,這樣可以加載xml的提示內(nèi)容。
全局配置
最開(kāi)始的spring 整合 mybatis的版本都是使用mybatis-config.xml配置文件,進(jìn)行數(shù)據(jù)源,以及各種配置信息的配置。
當(dāng)然這些配置項(xiàng)依然不變,變化的是配置項(xiàng)書(shū)寫(xiě)的方式,以及去掉了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(下劃線(xiàn)轉(zhuǎn)駝峰)、lazyLoadingEnabled(懶加載)、aggressiveLazyLoading(侵入式懶加載)、cacheEnabled(全局緩存)等配置項(xiàng)都是configuration中的子配置項(xiàng)。
項(xiàng)目搭建所需的簡(jiǎn)單配置項(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最后觀(guān)察一下我們的項(xiàng)目結(jié)構(gòu):
測(cè)試
為了簡(jiǎn)便測(cè)試,我們定義一個(gè)controller直接調(diào)用dao層的方法,以此來(lái)進(jìn)行方法的測(cè)試,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 的簡(jiǎn)單整合與使用,后續(xù)會(huì)進(jìn)行更加深入的學(xué)習(xí)和使用。
總結(jié)
以上是生活随笔為你收集整理的Spring Boot + Mybatis 快速整合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java 多线程 —— ThreadLo
- 下一篇: Spring Boot + JSP