最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot MyBatis保姆級整合教程

 更新時(shí)間:2022年06月29日 10:14:12   作者:程序員阿紅  
因?yàn)镾pring Boot框架開發(fā)的便利性,所以實(shí)現(xiàn)Spring Boot與數(shù)據(jù)訪問層框架(例如MyBatis)的整合非常簡單,主要是引入對應(yīng)的依賴啟動(dòng)器,并進(jìn)行數(shù)據(jù)庫相關(guān)參數(shù)設(shè)置即可

Spring Boot整合MyBatis

MyBatis 是一款優(yōu)秀的持久層框架,Spring Boot官方雖然沒有對MyBatis進(jìn)行整合,但是MyBatis團(tuán)隊(duì)自行適配了對應(yīng)的啟動(dò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)的啟動(dò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)目啟動(dò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)文章

最新評論

富源县| 汕头市| 绥江县| 惠安县| 西乡县| 滨海县| 象山县| 交口县| 高碑店市| 沅陵县| 大足县| 罗定市| 泗水县| 嵊州市| 平遥县| 阳原县| 锦屏县| 惠安县| 土默特左旗| 宜州市| 湛江市| 宁都县| 长春市| 旬阳县| 姚安县| 翁源县| 睢宁县| 忻州市| 天门市| 黄大仙区| 高州市| 航空| 沁阳市| 五寨县| 济南市| 榆树市| 邵阳县| 黄大仙区| 桐柏县| 天津市| 天峨县|