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

編寫與數據庫表
編寫與數據庫表t_comment和t_article對應的實體類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配置文件中進行數據庫連接配置
# MySQL數據庫連接配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/springbootdata?
serverTimezone=UTC&characterEncoding=UTF-8
username: root
password: wu7787879
注解方式整合Mybatis
需求:實現通過ID查詢Comment信息
(1)創(chuàng)建一個對t_comment表數據操作的接口CommentMapper
public interface CommentMapper {
@Select("SELECT * FROM t_comment WHERE id =#{id}")
public Comment findById(Integer id);
}
(2)在Spring Boot項目啟動類上添加@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);
}
}
打印結果:

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

創(chuàng)建接口類
創(chuàng)建一個用于對數據庫表t_article數據操作的接口ArticleMapper
@Mapper
public interface ArticleMapper {
public Article selectArticle(Integer id);
}
創(chuàng)建XML映射文件
resources目錄下創(chuàng)建一個統(tǒng)一管理映射文件的包mapper,并在該包下編寫與ArticleMapper接口方應的映射文件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映射文件路徑
在項目中編寫的XML映射文件,Spring Boot并無從知曉,所以無法掃描到該自定義編寫的XML配置文件,還必須在全局配置文件application.properties中添加MyBatis映射文件路徑的配置,同時需要添加實體類別名映射路徑,示例代碼如下
mybatis:
#配置MyBatis的xml配置文件路徑
mapper-locations: classpath:mapper/*.xml
#配置XML映射文件中指定的實體類別名路徑
type-aliases-package: com.lagou.base.pojo
編寫單元測試進行接口方法測試
@Autowired
private ArticleMapper articleMapper;
@Test
void contextLoads2() {
Article article = articleMapper.selectByPrimaryKey(1);
System.out.println(article);
}
打印結果:

到此這篇關于SpringBoot MyBatis保姆級整合教程的文章就介紹到這了,更多相關SpringBoot MyBatis內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot打印系統(tǒng)執(zhí)行的sql語句及日志配置指南
這篇文章主要給大家介紹了關于SpringBoot打印系統(tǒng)執(zhí)行的sql語句及日志配置的相關資料,在Java SpringBoot項目中如果使用了Mybatis框架,默認情況下執(zhí)行的所有SQL操作都不會打印日志,需要的朋友可以參考下2023-10-10
Mybatis代碼生成器Mybatis Generator(MBG)實戰(zhàn)詳解
本文我們主要實戰(zhàn)Mybatis官方的代碼生成器:Mybatis Generator(MBG),掌握它以后,可以簡化大部分手寫代碼,我們只需要寫復雜邏輯代碼,需要的朋友可以參考下2023-05-05
spring boot application properties配置實例代碼詳解
本文通過代碼給大家介紹了spring boot application properties配置方法,需要的的朋友參考下吧2017-07-07
詳解Spring中singleton?bean如何同時服務多個請求
這篇文章主要介紹了詳解Spring中singleton?bean如何同時服務多個請求2023-02-02

