一、Spring Boot使用Mybatis配置
首先需要在maven中添加mybatis依賴:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>
接著,在application.properties文件中配置Mybatis相關屬性:
mybatis.config-location=classpath:mybatis-config.xml mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.demo.domain
其中,mybatis.config-location指定了Mybatis的配置文件路徑,mybatis.mapper-locations指定了Mybatis的Mapper文件路徑,mybatis.type-aliases-package指定了實體類的包路徑。
二、Spring Boot使用Mybatis連接MySQL
在使用Mybatis之前,需要先配置MySQL的連接信息。可以在application.properties中設置相關屬性:
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456
其中,spring.datasource.url指定了MySQL的連接地址和埠號,spring.datasource.driver-class-name指定了MySQL的驅動名,spring.datasource.username和spring.datasource.password指定了MySQL連接的用戶名和密碼。
接著,我們就可以編寫Mapper映射文件和Dao層代碼進行MySQL資料庫的操作了。
三、Spring Boot使用Mybatis存null數據
在操作MySQL資料庫時,如果某個欄位的值為null,那麼Mybatis在執行插入或更新操作時會將該欄位的值設置為NULL,而不是直接忽略該欄位。如果不希望將null值存入資料庫中,可以在Mapper映射文件中使用sqlProvider方式指定非空欄位。
<insert id="insertUser" parameterType="User"> <selectKey keyProperty="id" resultType="int" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey> insert into user <set> <if test="name != null"> name = #{name}, </if> <if test="age != null"> age = #{age}, </if> <if test="address != null"> address = #{address}, </if> </set> </insert>
在上述代碼中,<if>標籤中判斷了欄位的值是否為null,如果不為null則插入該欄位,否則不插入。
四、Spring Boot使用Mybatis緩存
Mybatis默認開啟了一級緩存,無需任何配置即可使用,但是一級緩存的作用範圍只限於同一個SqlSession。對於不同的SqlSession,無法共享緩存資源。Mybatis也提供了二級緩存的功能,可以自行選擇是否啟用。
在application.properties中配置如下緩存屬性:
# 啟用緩存 mybatis.configuration.cache-enabled=true # 指定緩存類型為Ehcache mybatis.configuration.cache-enabled-type=EHCACHE
在Mapper映射文件的<mapper>標籤中使用cache配置指定緩存,例如:
<cache eviction="FIFO" flushInterval="60000" size="512"> </cache>
其中,eviction指定了緩存的淘汰策略,flushInterval指定了緩存的刷新時間間隔,size指定了緩存容量。
五、Spring Boot使用的技術
Spring Boot結合Mybatis可以使用很多其它技術棧來實現更加完善的功能,例如:
1. Spring Boot結合FreeMarker模板引擎使用
首先需要在maven中添加FreeMarker依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
接著,在application.properties中配置FreeMarker相關屬性:
spring.freemarker.template-loader-path=classpath:/templates/ spring.freemarker.suffix=.ftl
其中,spring.freemarker.template-loader-path指定了模板文件的路徑,spring.freemarker.suffix指定了模板文件的後綴名。
通過在Controller中返回ModelAndView對象,就可以使用FreeMarker模板引擎來渲染頁面了。
2. Spring Boot結合AOP使用
Spring Boot結合AOP可以很方便地實現類似於日誌、事務等方面的功能。
首先需要在maven中添加AOP依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
接著,在需要添加切面的類上使用@Aspect註解,並定義相關的切點和通知方法即可實現AOP功能。
3. Spring Boot結合註解使用
Spring Boot結合註解可以更加簡化開發過程,也更加直觀。
通過在Controller中使用@RestController註解、在Mapper介面中使用@Mapper註解等方式,可以大大簡化代碼的編寫。
六、Spring Boot使用Mybatis總結
本文對Spring Boot結合Mybatis的相關使用做了詳細的闡述,包括Mybatis的配置、MySQL連接、存null數據、緩存以及結合其它技術棧使用的方法等。通過本文的學習,相信讀者已經對Spring Boot如何使用Mybatis有了更加深入的了解。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/233950.html