SpringBoot整合mybatis的方法详解

 更新时间:2022年03月13日 17:58:09   作者:小徐也要努力鸭  
这篇文章主要为大家详细介绍了SpringBoot整合mybatis的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助

1 依赖配置

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.4</version>
</parent>
<dependencies>
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <version>1.18.22</version>
     </dependency>
	 <dependency>
	     <groupId>org.mybatis.spring.boot</groupId>
	     <artifactId>mybatis-spring-boot-starter</artifactId>
	     <version>1.3.2</version>
	 </dependency>
	 <!--     spring连接驱动时,如com.mysql.cj.jdbc.Driver使用   -->
     <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

找到mybatis-spring-boot-starter配置的依赖,即autotoconfigure包,SpringBoot的自动配置,会找到META-INF下的spring.factories,找到EnableAutoConfiguration对应的类:

在这里插入图片描述

在这里插入图片描述

可知自动配置类为MybatisAutoConfiguration:

在这里插入图片描述

在这里插入图片描述

查看配置绑定类MybatisProperties,可知yml中前缀配置为mybatis:

在这里插入图片描述

可知,mybatis前缀的yml配置,可以配置属性,比如:configLocationmapperLocationstypeAliasesPackage等等。

mybatis下,又具有@NestedConfigurationProperty成员变量,故而前缀是mybatis.configuration,其中具有如下属性:

在这里插入图片描述

其中有非常熟悉的属性:mapUnderscoreToCamelCase,也就是数据库字段下划线转驼峰的配置。

然后,数据源有如下配置:

在这里插入图片描述

数据源的配置绑定是DataSourceProperties

在这里插入图片描述

可知,数据源在yml中以spring.datasource为前缀,可配置连接数据源的driverClassNameurlusernamepassword等参数。

在这里插入图片描述

2 使用

2.1 SpringBoot配置整合mybatis:

建表:

在这里插入图片描述

实体类(mybatis本质上将数据库表的数据和实体类对应,就是依靠的getter和setter,所以@Data是必须有的):

package com.xiaoxu.boot.dto;
import lombok.Data;
import java.util.Date;
/**
 * @author xiaoxu
 * @date 2022-03-08
 * spring_boot:com.xiaoxu.boot.dto.PeopleDTO
 */
@Data
public class PeopleDTO {
    // 人的id编号
    long id;
    // 人的名字
    String myName;
    // 年龄
    int myAge;
    // 出生日期
    Date birthday;
}

新建Mapper接口:

在这里插入图片描述

注意mapper接口必须要有@Mapper注解:

package com.xiaoxu.boot.mapper;
import com.xiaoxu.boot.dto.PeopleDTO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface PeopleMapper {
    List<PeopleDTO> queryPeopleByAge(int age);
}

在resources目录下准备mybatis的配置文件,以及Mapper文件:

在这里插入图片描述

mybatis-confi.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>

在这里插入图片描述

<?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="com.xiaoxu.boot.mapper.PeopleMapper">
    <select id="queryPeopleByAge" resultType="com.xiaoxu.boot.dto.PeopleDTO">
        select * from my_people where my_age = #{age}
    </select>
</mapper>

在application.yml中配置如下:

spring:
  datasource:
    username: root
    password: ******
    url: jdbc:mysql://localhost:3306/xiaoxu?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver

#mybatis的相关配置
mybatis:
  #mapper配置文件
  mapper-locations: classpath:mapper/*.xml
#  #mybatis配置文件
#  config-location: classpath:mybatis-config.xml
#  config-location和configuration不能同时存在
  #开启驼峰命名
  configuration:
    map-underscore-to-camel-case: true

插入测试数据:

在这里插入图片描述

service层实现:

package com.xiaoxu.service;
import com.xiaoxu.boot.dto.PeopleDTO;
import com.xiaoxu.boot.mapper.PeopleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * @author xiaoxu
 * @date 2022-03-08
 * spring_boot:com.xiaoxu.service.PeopleService
 */
@Service
public class PeopleService {
    @Autowired
    PeopleMapper peopleMapper;
    public List<PeopleDTO> getPeoples(int Age){
        return peopleMapper.queryPeopleByAge(Age);
    }
}

controller层实现:

package com.xiaoxu.boot.controller;
import com.xiaoxu.boot.dto.PeopleDTO;
import com.xiaoxu.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
 * @author xiaoxu
 * @date 2022-03-08
 * spring_boot:com.xiaoxu.boot.controller.PeopleController
 */
@RestController
public class PeopleController {
    @Autowired
    PeopleService peopleService;
    @GetMapping("/people")
    public List<PeopleDTO> queryPeople(@RequestParam(value = "ag") int age){
        return peopleService.getPeoples(age);
    }
}

执行结果无误:

在这里插入图片描述

2.2 SpringBoot注解整合mybatis:

修改Mapper接口文件:

package com.xiaoxu.boot.mapper;
import com.xiaoxu.boot.dto.PeopleDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface PeopleMapper {
    List<PeopleDTO> queryPeopleByAge(int age);
    @Select("select * from my_people")
    List<PeopleDTO> queryAllPeople();
}

修改服务层:

package com.xiaoxu.service;
import com.xiaoxu.boot.dto.PeopleDTO;
import com.xiaoxu.boot.mapper.PeopleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * @author xiaoxu
 * @date 2022-03-08
 * spring_boot:com.xiaoxu.service.PeopleService
 */
@Service
public class PeopleService {
    @Autowired
    PeopleMapper peopleMapper;
    public List<PeopleDTO> getPeoples(int Age){
        return peopleMapper.queryPeopleByAge(Age);
    }
    public List<PeopleDTO> getAllPeople(){
        return peopleMapper.queryAllPeople();
    }
}

增加controller:

package com.xiaoxu.boot.controller;
import com.xiaoxu.boot.dto.PeopleDTO;
import com.xiaoxu.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
 * @author xiaoxu
 * @date 2022-03-08
 * spring_boot:com.xiaoxu.boot.controller.PeopleController
 */
@RestController
public class PeopleController {
    @Autowired
    PeopleService peopleService;
    @GetMapping("/people")
    public List<PeopleDTO> queryPeople(@RequestParam(value = "ag") int age){
        return peopleService.getPeoples(age);
    }
    @GetMapping("/allPeople")
    public List<PeopleDTO> queryAllPeople(){
        return peopleService.getAllPeople();
    }
}

结果返回无误:

在这里插入图片描述

2.3 在配置类上增加@MapperScan注解,扫描某个包下的全部Mapper文件:

如果每个Mapper接口文件上增加@Mapper比较麻烦,那么可以在配置类,如主程序类上,增加@MapperScan注解,以及扫描路径,效果和@Mapper一致:

在这里插入图片描述

主程序类增加@MapperScan注解:

在这里插入图片描述

重新执行效果一致:

在这里插入图片描述

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!  

相关文章

  • FastJSON字段智能匹配踩坑的解决

    FastJSON字段智能匹配踩坑的解决

    这篇文章主要介绍了FastJSON字段智能匹配踩坑的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • mybatis-plus更新字段为null的处理方式

    mybatis-plus更新字段为null的处理方式

    这篇文章主要介绍了mybatis-plus更新字段为null的处理方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • 使用springboot制作博客管理系统

    使用springboot制作博客管理系统

    这篇文章主要介绍了使用springboot制作博客管理系统,文中有非常详细的代码示例,对正在学习springboot的小伙伴有很大的帮助,感兴趣的小伙伴可以参考一下
    2021-08-08
  • Java命名规则详细总结

    Java命名规则详细总结

    Class名应是首字母大写的名词。命名时应该使其简洁而又具有描述性。异常类的命名,应以Exception结尾。Interface的命名规则与Class相同
    2013-10-10
  • Java 实战项目之在线点餐系统的实现流程

    Java 实战项目之在线点餐系统的实现流程

    读万卷书不如行万里路,只学书上的理论是远远不够的,只有在实战中才能获得能力的提升,本篇文章手把手带你用java+SSM+jsp+mysql+maven实现一个在线点餐系统,大家可以在过程中查缺补漏,提升水平
    2021-11-11
  • spring boot如何使用POI读取Excel文件

    spring boot如何使用POI读取Excel文件

    本文主要介绍使用POI进行Excel文件的相关操作,涉及读取文件,获取sheet表格,对单元格内容进行读写操作,以及合并单元格的操作
    2021-08-08
  • java HttpURLConnection类的disconnect方法与http长连接详解

    java HttpURLConnection类的disconnect方法与http长连接详解

    这篇文章主要介绍了java HttpURLConnection类的disconnect方法与http长连接,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-04-04
  • Java如何判断整数溢出,溢出后怎么得到提示

    Java如何判断整数溢出,溢出后怎么得到提示

    这篇文章主要介绍了Java如何判断整数溢出,溢出后怎么得到提示,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • java如何获取指定文件夹下的所有文件名

    java如何获取指定文件夹下的所有文件名

    这篇文章主要介绍了java如何获取指定文件夹下的所有文件名问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • 对SpringMVC的@RequestParam的解释

    对SpringMVC的@RequestParam的解释

    下面小编就为大家带来一篇对SpringMVC的@RequestParam的解释。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09

最新评论