springboot数据访问之整合Mybaits(注解方式,Druid数据源)

一、概述

Mybaits作为操作数据库的流行框架,springboot没有提供Mybaits场景依赖,但是Mybaits团队自己适配了SpringBoot,提供了mybaits-spring-boot-starter依赖启动器实现数据访问操作。

二、整合mybaits

1.基础环境搭建

1.1 数据准备

即首先应该要准备有数据库,我这里准备的数据如下图所示
数据库名:springbootdata
数据库表:t_article、t_comment
t_article表
t_comment

1.2 创建项目

在这里插入图片描述

在这里插入图片描述
创建完成后项目目录如下
在这里插入图片描述
查看pom.xml文件,可以看到有mybatis的依赖启动器了,以及也有了mysql的驱动
在这里插入图片描述

2.编写实体类

新建一个包,在包下编写t_comment和t_article的对应实体类

//Comment类对应t_comment数据表
package pojo;

public class Comment {
    private Integer id;
    private String content;
    private String author;
    private Integer aId;

    public Integer getId() {
        return id;
    }

    public String getContent() {
        return content;
    }

    public String getAuthor() {
        return author;
    }

    public Integer getaId() {
        return aId;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setaId(Integer aId) {
        this.aId = aId;
    }

    @Override
    public String toString() {
        return "Comment{" +
                "id=" + id +
                ", content='" + content + '\'' +
                ", author='" + author + '\'' +
                ", aId=" + aId +
                '}';
    }
}
//对应t_article数据表的实体类
package pojo;

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 String getTitle() {
        return title;
    }

    public String getContent() {
        return content;
    }

    public List<Comment> getCommentList() {
        return commentList;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setCommentList(List<Comment> commentList) {
        this.commentList = commentList;
    }

    @Override
    public String toString() {
        return "Article{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", commentList=" + commentList +
                '}';
    }
}

3.编写配置文件

在application.properties文件中对mysql数据库连接配置

# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?serverTimezone=UTC
# 数据库用户名&密码:
spring.datasource.username=root
spring.datasource.password=root
3.1 配置Druid第三方数据源

在pom.xml文件中加入druid数据源启动器,启动之后刷新加载一下

        <!--        添加阿里的druid德鲁伊数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>

在application.properties中添加并配置第三方数据源druid

#添加并配置第三方的数据源druid
#1数据源类型
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#2初始化连接数
spring.datasource.druid.initial-size=20
#最小空闲树
spring.datasource.druid.min-idle=10
#最大连接数
spring.datasource.druid.max-active=100
#开启驼峰命名映射
mybatis.configuration.map-underscore-to-camel-case=true

新建一个config包,在该包下新建一个自定义配置类对Druid数据源属性值进行注入

package config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @Classname DataSourceConfig
 * @Description 自定义数据源配置类
   完成第三方数据源参数值的注入
 */
@Configuration  //通过这个注解标识了一个自定义配置类DataSourceConfig
public class DataSourceConfig {
    @Bean  //通过该注解注入了一个DataSource实例化对象
    @ConfigurationProperties(prefix = "spring.datasource")
    //上面ConfigurationProperties注解将全局配置文件application.properties中以spring.datasource开头的属性值注入到getDruid方法返回的datasource类对象属性值中
    public DataSource getDruid(){
        return new DruidDataSource();
    }
}

至此简单的druid整合就配置好了

4.使用注解开始整合

创建Mapper接口文件。新建一个包mapper,并在该包下创建一个用于对数据库表t_comment数据操作的接口CommentMapper

package mapper;

import pojo.*;
import org.apache.ibatis.annotations.*;

/*
* 请注意,注解后面不能加分号
* */
@Mapper  //该注解说明了下面这个类是一个mybaits接口文件,并保证能被springboot自动扫描到spring接口中
public interface CommentMapper {
    @Select("select * from t_comment where id = #{id}")
    public Comment findById(Integer id);
    @Insert("Insert into t_comment(content,author,a_id) values(#{content},#{author},#{aId})")
    public int insertComment(Comment comment);
    @Update("update t_comment set content=#{content} where id=#{id}")
    public int updateComment(Comment comment);
    @Delete("delete from t_comment where id=#{id}")
    public int deleteComment(Integer id);

}

5.测试类中进行测试

打开系统自动生成的测试类
在这里插入图片描述

在此之前要先在主类中加入@MapperScan(“mapper")
对mapper包下的文件进行扫描加载
在这里插入图片描述
测试增删改查各个测试代码

package com.example;

import mapper.CommentMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import pojo.Comment;

@SpringBootTest
class TestBlogMybaitsZhujieApplicationTests {
    @Autowired //使用此注解将CommentMapper接口自动装配为spring容器中的bean
    private CommentMapper commentMapper;
    @Test
    void findByid() {
//  查询
        System.out.println("根据id进行查询");
        Comment comment = commentMapper.findById(1);
        System.out.println(comment);
    }
    @Test
    void insertComment(){
//   添加
        System.out.println("进行添加数据");
        Comment comment = new Comment();
        comment.setContent("很好");
        comment.setAuthor("战三");
        comment.setaId(6);
        int res = commentMapper.insertComment(comment);
        System.out.println(res);
    }
    @Test
    void updateComment(){
//   根据id对评论进行修改
        System.out.println("对评论进行修改");
        Comment comment = new Comment();
        comment.setContent("非常好呀");
        comment.setId(6);
        int res = commentMapper.updateComment(comment);
        System.out.println(res);
    }
    @Test
    void delComment(){
//   根据id对评论进行删除
        System.out.println("对评论进行删除");
        int res = commentMapper.deleteComment(6);
        System.out.println(res);
    }
}

一个个方法进行测试
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三、写在最后

以上是自己在《spring boot企业级开发教程》中学习的一点小总结,如有需要数据以及源码的读者,可点击下载支持谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-希冀-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值