Spring Boot第二次总结

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的一些内容,由于是跟着老师的代码敲,基本没有出什么错误,但也不代表掌握了本次的知识,还需要继续学习。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值