Spring Boot整合MyBatis
一、基础环境搭建
1、创建博客数据库blog和相关数据表
t_article表
t_comment表
2、创建Spring Boot项目MyBatisDemo并引入MySQL和MyBatis的依赖启动器
3、在net.lxt.lesson06.mybatisdemo目录下创建子目录bean,并在里面创建评论实体类-Comment
package net.lxt.lesson06.mybatisdemo;
public class Comment {
private Integer id;
private String content;
private String author;
private Integer aId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getaId() {
return aId;
}
public void setaId(Integer aId) {
this.aId = aId;
}
@Override
public String toString() {
return "Comment{" +
"id=" + id +
", content='" + content + '\'' +
", author='" + author + '\'' +
", aId=" + aId +
'}';
}
}
4、创建文章实体类
package net.lxt.lesson06.mybatisdemo.bean;
import java.util.List;
public class Article {
private Integer id;
private String title;
private String content;
private List<Comment> 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;
}
@Override
public String toString() {
return "Article{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
", commentList=" + commentList +
'}';
}
}
5、编写配置文件
1)将全局配置文件application.properties更名为application.yaml
2)在全局配置文件中进行数据库连接配置
3)在pom.xml文件里添加druid依赖
4)在全局配置文件里覆盖默认参数
二、使用注解方式整合MyBatis
1、在net.lxt.lesson06.mybatisdemo目录下创建mapper子目录,并在里面创建评论映射器接口 - CommentMapper
package net.lxt.lesson06.mybatisdemo.mapper;
import net.lxt.lesson06.mybatisdemo.bean.Comment;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper // 交给Spring容器管理
public interface CommentMapper {
// 按照编号查询记录
@Select("select * from t_comment where id = #{id}")
Comment findById(Integer id);
// 查找全部记录
@Select("select * from t_comment")
List<Comment> findAll();
// 插入记录
@Insert("insert into t_comment values(#{id}, #{content}, #{author}, #{aId})")
int insertComment(Comment comment);
// 更新记录
@Update("update t_comment set content = #{content}, author = #{author} where id = #{id}")
int updateComment(Comment comment);
// 删除记录
@Delete("delete from t_comment where id = #{id}")
int deleteComment(Integer id);
}
2、在测试类编写测试方法,测试评论映射器接口
注入评论映射器,如果不设置required = false,变量commentMapper会出现红色波浪线报错
1)创建测试方法testFindById()
测试结果:
注意:想要aId的值不为null,就要在需要在全局配置文件里对MyBatis进行一个转换配置:
2)创建测试方法testFindAll()
测试结果:
3)创建测试方法testInsertComment()
测试结果:
三、使用配置文件方式整合MyBatis
1、创建文章映射接口 - ArticleMapper
package net.lxt.lesson06.mybatisdemo;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ArticleMapper {
Article findArticleById(Integer id);
int updateArticle(Article article);
}
2、在resources目录里创建mapper目录,在mapper目录里创建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="net.lxt.lesson06.mapper.ArticleMapper">
<!--按id查询记录,文章表与评论表关联查询-->
<select id="findArticleById" resultMap="articleWithComment">
SELECT a.*, c.id c_id, c.content c_content, c.author, c.a_id
FROM t_article a, t_comment c
WHERE a.id = c.a_id AND a.id = #{id}
</select>
<!--结果集,一篇文章对应多个评论构成的集合-->
<resultMap id="articleWithComment" type="Article">
<id property="id" column="id"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<collection property="commentList" ofType="Comment">
<id property="id" column="c_id"/>
<result property="content" column="c_content"/>
<result property="author" column="author"/>
<result property="aId" column="a_id"/>
</collection>
</resultMap>
<!--更新记录-->
<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>
</mapper>
3、在全局配置文件里配置映射器配置文件路径
4、在测试类编写测试方法,测试文章映射器
注入文章映射器
1)创建测试方法testFindArticleById()
测试结果:
2)创建测试方法testUpdateArticle()
测试结果:
总结:
本次课了解了springboot数据访问、使用注解的方式整合mybatis和使用配置文件的方式整合mybatis。在第一次课学习过mybatis的一些内容,由于是跟着老师的代码敲,基本没有出什么错误,但也不代表掌握了本次的知识,还需要继续学习。