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相關聯(lián)
# 創(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 '關聯(lián)的文章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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
關于java.lang.NumberFormatException: null的問題及解決
這篇文章主要介紹了關于java.lang.NumberFormatException: null的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09SpringBoot自定義Starter與自動配置實現方法詳解
在Spring Boot官網為了簡化我們的開發(fā),已經提供了非常多場景的Starter來為我們使用,即便如此,也無法全面的滿足我們實際工作中的開發(fā)場景,這時我們就需要自定義實現定制化的Starter2023-02-02