一 使用配置文件的方式整合MyBatis步骤
1.1 sql文件创建
# 创建数据库
CREATE DATABASE springbootdata;
# 选择使用数据库
USE springbootdata;
# 创建表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基础入门', '从入门到精通');
# 创建表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', '很全、很详细', '张三', '1');
INSERT INTO `t_comment` VALUES ('2', '赞一个', '李四', '1');
INSERT INTO `t_comment` VALUES ('3', '很详细', '王老五', '1');
INSERT INTO `t_comment` VALUES ('4', '很好,非常详细', '老刘', '1');
INSERT INTO `t_comment` VALUES ('5', '很不错', '老酒', '1');
1.2 Article文件
package com.example.chapter3.entity;
import java.util.List;
public class Article {
@Override
public String toString() {
return "Article{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
", commentList=" + commentList +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public List<Comment> getCommentList() {
return commentList;
}
public void setCommentList(List<Comment> commentList) {
this.commentList = commentList;
}
private Integer id;
private String title;
private String content;
private List<Comment> commentList;
}
1.3 创建mapper接口文件:@Mapper
package com.example.chapter3.mapper;
import org.apache.ibatis.annotations.Mapper;
import com.example.chapter3.entity.Article;
@Mapper
public interface ArticleMapper {
public int updateArticle(Article article);
public Article selectArticle(Integer id);
}
1.4 创建XML映射文件,编写对应的SQL语句
<?xml version="1.0" encoding="UTF-8" ?>
<!--指定xml的版本和编码格式-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 声明这个XML文件的文档类型,并指定了MyBatis Mapper的DTD(文档类型定义)。-->
<!-- 这个DTD定义了Mapper XML文件中允许使用的元素和属性。-->
<mapper namespace="com.example.chapter3.mapper.ArticleMapper">
<!-- 定义了这个Mapper文件对应的Java接口的命名空间。namespace属性指定了该接口的完整路径-->
<select id="selectArticle" resultMap="articleWithComment">
SELECT a.*
,c.id c_id,c.content c_content,c.author
FROM t_article a,t_comment c
WHERE a.id=c.a_id AND a.id = #{id}
</select>
<update id="updateArticle" parameterType="Article" >
UPDATE t_article
<set>
<if test="title !=null and title !=''">
title=#{title},
</if>
<if test="content !=null and content !=''">
content=#{content}
</if>
</set>
WHERE id=#{id}
</update>
<!-- resultMap是结果映射-->
<resultMap id="articleWithComment" type="Article">
<!-- id是映射的名字,type是指映射的类型-->
<id property="id" column="id" />
<result property="title" column="title" />
<result property="content" column="content" />
<!-- 将数据库中的id和content映射到article对象-->
<collection property="commentList" ofType="Comment">
<id property="id" column="c_id" />
<result property="content" column="c_content" />
<result property="author" column="author" />
</collection>
<!-- 将数据库中的查询结果的多行映射到article对象的commenlist属性中-->
</resultMap>
</mapper>
1.5 在全局文件中配置XML映射文件路径以及实体类别名映射路径
mybatis.mapper-locations=classpath:mapper/*xml
mybatis.type-aliases-package=com.example.chapter3.entity
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.configuration.map-underscore-to-camel-case=true
1.6 编写测试方法进行接口方法测试及整合测试
package com.example.chapter3;
import com.example.chapter3.entity.Article;
import com.example.chapter3.mapper.ArticleMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Chapter3ApplicationTests {
@Autowired
ArticleMapper articleMapper;
@Test
void testXml(){
Article selectArticle = articleMapper.selectArticle(1);
System.out.println(selectArticle);
}
@Test
void testxml(){
Article article=new Article();
article.setId(2);
article.setTitle("test_title");
article.setContent("test_content");
Integer integer=articleMapper.updateArticle(article);
System.out.println(integer);
}
}