栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot2整合MyBatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot2整合MyBatis

SpringBoot2整合MyBatis

在整合MyBatis之前,我们要准备哪些工作:

  • 导入mysql场景:

    
    8.0.28
    
    
                mysql
                mysql-connector-java
                8.0.28 
    
    
    想要修改版本
    1、直接依赖引入具体版本(maven的就近原则)
    2、重新声明版本(maven的属性的就近原则)
    
        1.8
        8.0.28
    
    
  • 配置文件修改:

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/user 
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
    # 如果是mysql8以上版本,请使用下面配置:
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/hunnu?characterEncoding=utf-8&serverTimezone=UTC
        username: root
        password: 123456 
    
1.导入Mybatis的starter(场景启动器)
   

        org.springframework.boot
        spring-boot-starter-data-jdbc

   

        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.1.4

注:如果是使用 Spring Initilizr(项目初始化向导)来搭建的话,可在创建项目时就可以选中Mybatis的开发场景以及jdbc场景,系统会自动依赖引入。


导入了Mybatis的场景之后,我们会发现它给我们自动导入了:

2.Mybatis的自动配置(内含源码解析,初学者可跳过直接使用)

如果是以前使用SSM框架整合MyBatis时,避免不了要写一大推的代码编写Mybatis的核心配置文件啊,SqlSessionFactory以及SqlSession等等之类的东西。总体来说,相当地复杂。但是现在有了SpringBoot (博主的SpringBoot版本:2.6.6)的话,很多的配置都由SpringBoot已经封装好了,节省了很多编写程序的时间。

SpringBoot自动配置好了:

  • 全局配置文件 application.yml
  • 自动配置好了SqlSessionFactory
  • 底层自动配置了SqlSessionTemplate,SqlSessionTemplate组合了 SqlSession
  • 主要我们写的Mybatis的接口标注了@Mapper注解就会被SpringBoot自动扫描进来。

我们可以CTRL+N 查找类:MybatisAutoConfiguration (mybatis的自动配置类)可以看到以下内容:

@EnableConfigurationProperties({MybatisProperties.class}) //mybatis配置项绑定类
@AutoConfigureAfter({DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class})
public class MybatisAutoConfiguration implements InitializingBean {



//然后CTRL+左键进入到MybatisProperties.class中发现以下内容:
@ConfigurationProperties(
    prefix = "mybatis"
)
public class MybatisProperties {}

看到这里,我们发现了 prefix=“mybatis”,然后MybatisProperties又是mybatis配置项绑定类,也就是说我们在application.yml(配置文件)中写了以**“mybatis”**为前缀的配置项都是属于mybatis的配置!!!

3.XMl配置模式 (方式一)

我们可以在配置文件中修改mybatis的配置,来定义mybatis的全局配置文件的位置和sql映射文件位置:

# 配置mybatis规则
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml  #全局配置文件位置
  mapper-locations: classpath:mybatis/mapper/*.xml  #sql映射文件位置

然后在resource目录下创建mybatis/mapper/UserPapper.xml 并写入:



        



注:这里有个很重要问题,就是为什么博主没有写mybatis的全局配置文件呢?

其实在上面 Mybatis的自动配置 里面说的MybatisProperties类中有一个属性叫private Configuration configuration:

也就是说我们只需修改application.yml(配置文件)中以"mybatis.configuration"为前缀的下面的所有,就是相当于修改mybatis的全局配置文件中的值!!

# 配置mybatis规则
mybatis:
#  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true
    
 可以不写全局配置文件,所有全局配置文件的配置都放在mybatis.configuration配置项中即可

总体流程:

  • 导入mybatis官方starter

  • 编写mapper接口。标准@Mapper注解

  • 编写sql映射文件并绑定mapper接口

  • 在application.yml中指定Mapper配置文件的位置,以及指定全局配置文件的信息(推荐配置在mybatis.configuration)

4.注解方式(方式二)

使用注解可以省去mybatis的全局配置文件 mybatis-config.xml 和xml的sql映射文件。

示例代码如下:
pojo:

@Data
public class User {
    private int id;
    private String name;
    private int age;
    private String email;
    
}

mapper(dao):

@Mapper
public interface UserMapper {

    @Select("select * from user where id=#{id}")
    public Singer getUser(int id);
    
}

controller:

    @ResponseBody
    @RequestMapping("/getUser")
    public User getUser(@RequestParam("id")int id){
        User user =  userMapper.getUser(id);
        return user;
    }

结果:

5.混合模式 (最佳方式)

在实际生产开发中,最佳方式是xml配置方式和注解方式混合使用。

最佳实战:

  • 引入mybatis-starter的场景启动器
  • 配置application.yml中,指定mapper-location位置即可
  • 编写Mapper接口并标注 @Mapper 注解 (如果觉得每次写接口都要标注 @Mapper 这样很麻烦,在springboot主程序启动类中配置 @MapperScan(“指定要扫描的位置”) )
  • 简单方法直接注解方式
  • 复杂方法编写mapper.xml进行绑定映射
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/838875.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号