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

SpringBoot MyBatis保姆級整合教程

 更新時間:2022年06月29日 10:14:12   作者:程序員阿紅  
因為Spring Boot框架開發(fā)的便利性,所以實現Spring Boot與數據訪問層框架(例如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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

兴仁县| 常州市| 阳曲县| 古交市| 昭平县| 天祝| 彩票| 巩留县| 白银市| 吕梁市| 海原县| 日土县| 云龙县| 临沧市| 九龙县| 横峰县| 清苑县| 开阳县| 淄博市| 长海县| 曲水县| 潍坊市| 正镶白旗| 丽江市| 光泽县| 无棣县| 健康| 阿坝县| 白山市| 天全县| 循化| 连平县| 原平市| 安阳市| 甘肃省| 石首市| 南宫市| 安义县| 富顺县| 迭部县| 英山县|