SpringBoot使用篇---SpringBoot整合MyBatis

在这里插入图片描述

目录:

   前言
   SpringBoot整合MyBatis具体步骤
   总结
   分享与交流html

前言

   上一篇总结了SpringBoot整合Thymeleaf,它是针对于表现层–业务层来讲的,一个完整的项目须要表现层–业务层–持久层,它只实现了一半。这一篇总结一下SpringBoot整合MyBatis,它是业务层–持久层。MyBatis做为一款优秀的ORM框架,对于spring来讲它虽然是第三方组件,但MyBatis官方作了一个很好的接口提供给spring,使得spring整合MyBatis很是方便。java

SpringBoot整合MyBatis具体步骤

   因为MyBatis是第三方组件,所以须要引入相关依赖才能在spring中使用。
(1)建立spring Boot工程,具体建立步骤可参考SpringBoot—搭建boot框架的三种方式
(2)在pom.xml中引入相关依赖:mysql

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository --> </parent>

	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.yaml</groupId>
        <artifactId>snakeyaml</artifactId>
        <version>1.23</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.8</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version>
    </dependency>

(3)建立数据表web

create table user(
id int primary key auto_increment,
name varchar(11),
age int
);

在这里插入图片描述
(4)建立数据表对应的实体类spring

@Data //自动建立getter,setter,toString等方法
@AllArgsConstructor //自动建立构造函数
public class User {
    private Integer id;
    private String name;
    private Integer age;
}

(5)建立UserRepository,定义CRUD接口。sql

public interface UserRepository {
    public List<User> findAll();
    public User findById(Integer id);
    public int add(User user);
    public int update(User user);
    public int deleteById(Integer id);
}

(6)在/resources/mapping路径下建立UserRepository.xml,做为UserRepository的配套Mapper文件,定义每一个接口方法对应的SQL语句以及结果集等。缓存

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.lxy.spring_boot1.repository.UserRepository">
    <select id="findAll" resultType="User">
        select * from user;
    </select>

    <select id="findById" parameterType="java.lang.Integer" resultType="User">
        select * from user where id=#{id};
    </select>

    <insert id="add" parameterType="User">
        insert into user(id,name,age) values(#{id},#{name},#{age})
    </insert>
    
    <update id="update" parameterType="User">
        update user set id=#{id},name=#{name},age=#{age}
    </update>
    
    <delete id="deleteById" parameterType="java.lang.Integer">
        delete from user where id=#{id}
    </delete>
</mapper>

(7)建立UserHandler,注入UserRepository。mybatis

@RestController
public class UserController {
    
    @Autowired
    private UserRepository userRepository;
    
    @GetMapping("/findAll")
    public List<User> findAll(){
        return userRepository.findAll();
    }
    
    @GetMapping("/findById/{id}")
    public User get(@PathVariable("id") Integer id){
        return userRepository.findById(id);
    }
    
    @PostMapping("/add")
    @ResponseBody
    public int add(@RequestBody User user){
        return userRepository.add(user);
    }
    
    @PostMapping("/update")
    @ResponseBody
    public int update(User user){
        return userRepository.update(user);
    }
    
    @GetMapping("/deleteById/{id}")
    public int deleteById(@PathVariable("id") Integer id){
        return userRepository.deleteById(id);
    }
}

(8)在/resources/static路径下建立配置文件application.yml,添加MySQL数据源信息以及MyBatis的相关配置。app

#注意每一个属性名和属性值之间至少一个空格,注释使用#不是//
spring:
  thymeleaf:
    prefix: classpath:/templates/ #前缀
    suffix: .html #后缀
    servlet:
      content-type: text/html #在请求中,客户端告诉服务端实际请求的内容
                              #在响应中,服务端告诉请求端实际响应的内容
    encoding: utf-8 #设置编码格式
    mode: HTML  #校验HTML格式
    cache: false #关闭缓存,在开发过程当中可当即看到修改后的结果

  datasource:
    url: jdbc:mysql://localhost:3306/summer03?useUnicode=true&characterEncoding=UTF-8
    username: root
    password:
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  mapper-locations: classpath:/mapping/*.xml
  type-aliases-package: cn.lxy.spring_boot1.entity

(9)建立启动类Application,这里须要多加一个注解,由于MyBatis是第三方组件,spring不会自动管理MyBatis相关对象的生命周期,所以须要咱们本身手动配置。框架

@SpringBootApplication
@MapperScan("cn.lxy.spring_boot1.repository")
public class SpringBoot1Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringBoot1Application.class, args);
    }
}

   @MapperScan注解的做用就是将目标包下的类所有扫描到Spring容器中。
(10)启动Application,使用Postman工具来测试相关接口,结果如图所示:

   •findAll
在这里插入图片描述
   •findById
在这里插入图片描述
   •deleteById
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
   •add
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
   •update

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

总结

   MyBatis做为当前主流的ORM框架,成为了不少Java开发者的首选,Spring Boot很好的集成了MyBatis,开发者无需额外进行配置,开箱即用。

分享与交流

   因为能力有限,博客总结不免有不足,还请大佬们不吝赐教😄