SpringBoot MyBatis保姆級整合教程
Spring Boot整合MyBatis
MyBatis 是一款優(yōu)秀的持久層框架,Spring Boot官方雖然沒有對MyBatis進(jìn)行整合,但是MyBatis團(tuán)隊(duì)自行適配了對應(yīng)的啟動器,進(jìn)一步簡化了使用MyBatis進(jìn)行數(shù)據(jù)的操作
基礎(chǔ)環(huán)境搭建
數(shù)據(jù)準(zhǔn)備
在MySQL中,先創(chuàng)建了一個(gè)數(shù)據(jù)庫springbootdata,然后創(chuàng)建了兩個(gè)表t_article和t_comment并向表中插入數(shù)據(jù)。其中評論表t_comment的a_id與文章表t_article的主鍵id相關(guān)聯(lián)
# 創(chuàng)建數(shù)據(jù)庫
CREATE DATABASE springbootdata;
# 選擇使用數(shù)據(jù)庫
USE springbootdata;
# 創(chuàng)建表t_article并插入相關(guān)數(shù)據(jù)
DROP TABLE IF EXISTS t_article;
CREATE TABLE t_article (
id int(20) NOT NULL AUTO_INCREMENT COMMENT '文章id',
title varchar(200) DEFAULT NULL COMMENT '文章標(biāo)題',
content longtext COMMENT '文章內(nèi)容',
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
INSERT INTO t_article VALUES ('1', 'Spring Boot基礎(chǔ)入門', '從入門到精通講解...');
INSERT INTO t_article VALUES ('2', 'Spring Cloud基礎(chǔ)入門', '從入門到精通講
解...');
# 創(chuàng)建表t_comment并插入相關(guān)數(shù)據(jù)
DROP TABLE IF EXISTS t_comment;
CREATE TABLE t_comment (
id int(20) NOT NULL AUTO_INCREMENT COMMENT '評論id',
content longtext COMMENT '評論內(nèi)容',
author varchar(200) DEFAULT NULL COMMENT '評論作者',
a_id int(20) DEFAULT NULL COMMENT '關(guān)聯(lián)的文章id',
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO t_comment VALUES ('1', '很全、很詳細(xì)', 'lucy', '1');
INSERT INTO t_comment VALUES ('2', '贊一個(gè)', 'tom', '1');
INSERT INTO t_comment VALUES ('3', '很詳細(xì)', 'eric', '1');
INSERT INTO t_comment VALUES ('4', '很好,非常詳細(xì)', '張三', '1');
INSERT INTO t_comment VALUES ('5', '很不錯(cuò)', '李四', '2');
創(chuàng)建項(xiàng)目引入相應(yīng)的啟動器

編寫與數(shù)據(jù)庫表
編寫與數(shù)據(jù)庫表t_comment和t_article對應(yīng)的實(shí)體類Comment和Article
public class Comment {
private Integer id;
private String content;
private String author;
private Integer aId;
}
public class Article {
private Integer id;
private String title;
private String content;
}
編寫配置文件
在application.properties配置文件中進(jìn)行數(shù)據(jù)庫連接配置
# MySQL數(shù)據(jù)庫連接配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/springbootdata?
serverTimezone=UTC&characterEncoding=UTF-8
username: root
password: wu7787879
注解方式整合Mybatis
需求:實(shí)現(xiàn)通過ID查詢Comment信息
(1)創(chuàng)建一個(gè)對t_comment表數(shù)據(jù)操作的接口CommentMapper
public interface CommentMapper {
@Select("SELECT * FROM t_comment WHERE id =#{id}")
public Comment findById(Integer id);
}
(2)在Spring Boot項(xiàng)目啟動類上添加@MapperScan(“xxx”)注解
@SpringBootApplication
@MapperScan("com.lagou.mapper")
public class Springboot02MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot02MybatisApplication.class, args);
}
}
(3)編寫測試方法
@RunWith(SpringRunner.class)
@SpringBootTest
class SpringbootPersistenceApplicationTests {
@Autowired
private CommentMapper commentMapper;
@Test
void contextLoads() {
Comment comment = commentMapper.findById(1);
System.out.println(comment);
}
}
打印結(jié)果:

配置文件的方式整合MyBatis
第一、二步驟使用Free Mybatis plugin插件生成

創(chuàng)建接口類
創(chuàng)建一個(gè)用于對數(shù)據(jù)庫表t_article數(shù)據(jù)操作的接口ArticleMapper
@Mapper
public interface ArticleMapper {
public Article selectArticle(Integer id);
}
創(chuàng)建XML映射文件
resources目錄下創(chuàng)建一個(gè)統(tǒng)一管理映射文件的包mapper,并在該包下編寫與ArticleMapper接口方應(yīng)的映射文件ArticleMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lagou.mapper.ArticleMapper">
<select id="selectArticle" resultType="Article">
select * from Article
</select>
</mapper>
配置XML映射文件路徑
在項(xiàng)目中編寫的XML映射文件,Spring Boot并無從知曉,所以無法掃描到該自定義編寫的XML配置文件,還必須在全局配置文件application.properties中添加MyBatis映射文件路徑的配置,同時(shí)需要添加實(shí)體類別名映射路徑,示例代碼如下
mybatis:
#配置MyBatis的xml配置文件路徑
mapper-locations: classpath:mapper/*.xml
#配置XML映射文件中指定的實(shí)體類別名路徑
type-aliases-package: com.lagou.base.pojo
編寫單元測試進(jìn)行接口方法測試
@Autowired
private ArticleMapper articleMapper;
@Test
void contextLoads2() {
Article article = articleMapper.selectByPrimaryKey(1);
System.out.println(article);
}
打印結(jié)果:

到此這篇關(guān)于SpringBoot MyBatis保姆級整合教程的文章就介紹到這了,更多相關(guān)SpringBoot MyBatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
實(shí)例解析使用Java實(shí)現(xiàn)基本的音頻播放器的編寫要點(diǎn)
這篇文章主要介紹了使用Java實(shí)現(xiàn)基本的音頻播放器的代碼要點(diǎn)實(shí)例分享,包括音頻文件的循環(huán)播放等功能實(shí)現(xiàn)的關(guān)鍵點(diǎn),需要的朋友可以參考下2016-01-01
Java基于高精度整型實(shí)現(xiàn)fibonacci數(shù)列的方法
這篇文章主要介紹了Java基于高精度整型實(shí)現(xiàn)fibonacci數(shù)列的方法,是比較典型的算法,需要的朋友可以參考下2014-09-09
關(guān)于java.lang.NumberFormatException: null的問題及解決
這篇文章主要介紹了關(guān)于java.lang.NumberFormatException: null的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
我總結(jié)的幾種@Transactional失效原因說明
這篇文章主要是我總結(jié)的幾種@Transactional失效原因說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Java數(shù)據(jù)類型轉(zhuǎn)換實(shí)例解析
這篇文章主要介紹了Java數(shù)據(jù)類型轉(zhuǎn)換實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
SpringBoot自定義Starter與自動配置實(shí)現(xiàn)方法詳解
在Spring Boot官網(wǎng)為了簡化我們的開發(fā),已經(jīng)提供了非常多場景的Starter來為我們使用,即便如此,也無法全面的滿足我們實(shí)際工作中的開發(fā)場景,這時(shí)我們就需要自定義實(shí)現(xiàn)定制化的Starter2023-02-02

