SpringBoot+Mybatis实现Mapper接口与Sql绑定几种姿势

通常我们在使用Mybatis进行开发时,会选择xml文件来写对应的sql,然后将Mapper接口与sql的xml文件建立绑定关系,然后在项目中调用mapper接口就可以执行对应的sql,感兴趣的可以学习一下

通常我们在使用Mybatis进行开发时,会选择xml文件来写对应的sql,然后将Mapper接口与sql的xml文件建立绑定关系,然后在项目中调用mapper接口就可以执行对应的sql

那么如何将Mapper接口与sql进行绑定呢?本文将介绍四种常见的姿势

  • 默认策略
  • SpringBoot配置参数mybatis.mapper-locations
  • 指定
  • SqlSessionFactory指定

I. 环境准备

1. 数据库准备

使用mysql作为本文的实例数据库,新增一张表

 CREATE TABLE `money` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL DEFAULT '' COMMENT '用户名', `money` int(26) NOT NULL DEFAULT '0' COMMENT '钱', `is_deleted` tinyint(1) NOT NULL DEFAULT '0', `create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `update_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (`id`), KEY `name` (`name`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4; 

2. 项目环境

本文借助 SpringBoot 2.2.1.RELEASE + maven 3.5.3 + IDEA进行开发

pom依赖如下

   org.mybatis.spring.bootmybatis-spring-boot-starter2.2.0 mysqlmysql-connector-java

db配置信息 application.yml

 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: 

II. 实例演示

环境搭建完毕,准备对应的实体类,Mapper接口

1. 实体类,Mapper接口

数据库实体类: MoneyPo

 @Data @NoArgsConstructor @AllArgsConstructor public class MoneyPo { private Integer id; private String name; private Long money; private Integer isDeleted; private Timestamp createAt; private Timestamp updateAt; }

一个基础的Mapper接口

 @Mapper public interface MoneyMapper { trueint savePo(@Param("po") MoneyPo po); } 

一个demo service

 @Repository public class MoneyRepository { private Random random = new Random(); public void testMapper() { MoneyPo po = new MoneyPo(); po.setName("mybatis user"); po.setMoney((long) random.nextInt(12343)); po.setIsDeleted(0); moneyMapper.savePo(po); System.out.println("add record: " + po); }

2. sql文件

写sql的xml文件内容如下

    INSERT INTO `money` (`name`, `money`, `is_deleted`) VALUES true  (#{po.name}, #{po.money}, #{po.isDeleted}); 

3. Mapper与Sql绑定

以上为代码层面实现CURD的基础知识,基本上就是mybatis操作的那些套路,没有什么需要特殊注意的;接下来我们进入本文主题

如何告诉mybatis,将上面的MoenyMapper接口与xml文件关联起来

3.1 默认方式

采用默认的绑定方式,不需要我们做额外的操作,重点是需要遵循规则

  • xml的目录结构,与Mapper接口的包路径完全一致
  • xml文件名与Mapper接口名完全一致(注意大小写都要完全一致)

请注意上面的另个完全一致

使用默认的方式进行绑定时,一个示例如上图;特别需要注意的是文件名的大小写,xml文件的目录层级都需要完全一致

如果使用上面这种方式,在执行时,依然提示有问题,排查的思路就是查看 target目录下生成的class文件与xml文件是否在一起,如下图就是正常的case

再次说明

基于上面的case,我们可以直接将xml文件,与mapper接口写在一起,不放在资源路径resources下面

3.2 SpringBoot配置

SpringBoot提供了一个简单的配置,来指定Mapper接口与sql的绑定,一行配置即可

 mybatis: mapper-locations: classpath:sqlmapper/*.xml 

使用这种方式就比较简单了,不要求xml文件与Mapper接口文件名一致;也没有指定路径层级一致

3.3 Mapper标签

mapper标签,需要放在mybatis的配置文件中,因此我们首先通过SpringBoot的配置参数指定文件路径

 mybatis: configuration: config-location: classpath:mybatis-config.xml 

在资源文件下,新建文件 mybatis-config.xml

    

通过上面的mapper标签来指定注册关系,也是可行的,详情可参考官方文档 !

https://mybatis.org/mybatis-3/configuration.html#mappers

3.4 SqlSessionFactory

在前面一篇介绍Mapper接口注册的博文中,就介绍了通过qlSessionFactory+ MapperScannerConfigurer来注册

这里也是可以通过SqlSessionFactory来指定xml文件的

 @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations( // 设置mybatis的xml所在位置,这里使用mybatis注解方式,没有配置xml文件 new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/*.xml")); // 注册typehandler,供全局使用 bean.setTypeHandlers(new Timestamp2LongHandler()); bean.setPlugins(new SqlStatInterceptor()); return bean.getObject(); } 

4. 小结

本文主要介绍了四种Mapper接口与sql文件关系绑定的姿势,了解几种不同的姿势的特点,在实际的项目开发中,选择一个即可

  • 默认:在resource资源目录下,xml文件的目录层级与Mapper接口的包层级完全一致,且xml文件名与mapper接口文件名也完全一致
    • 如mapper接口: com.git.hui.boot.mybatis.mapper.MoneyMapper
    • 对应的xml文件: com/git/hui/boot/mybatis/mapper/MoneyMapper.xml
  • springboot配置参数:
    • application.yml配置文件中,指定 mybatis.mapper-locations=classpath:sqlmapper/*.xml
  • mybatis-config配置文件
    • 这种姿势常见于非SpringBoot项目集成mybatis,通常将mybatis的相关配置放在 mybatis-config.xml 文件中
    • 首先在配置文件中,指定加载参数 mybatis.config-location=classpath:mybatis-config.xml
    • 然后指定映射器
  • SqlSessionFactory指定
    • 直接在SqlSessionFactory中指定即可Mapper文件
 // 设置mybatis的xml所在位置,这里使用mybatis注解方式,没有配置xml文件 bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/*.xml")); 

除了上面几种方式之外,mybatis还支持无xml的方式,完全依靠注解来实现sql的拼装,因此也就不存在映射关系绑定了,关于注解的case,可以参考博文【DB系列】Mybatis+注解整合篇

III. 不能错过的源码和相关知识点

项目

工程:https://github.com/liuyueyi/spring-boot-demo
源码:https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/104-mybatis-ano
源码:https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/103-mybatis-xml

到此这篇关于SpringBoot+Mybatis实现Mapper接口与Sql绑定几种姿势的文章就介绍到这了,更多相关SpringBoot Mybatis Mapper接口与Sql绑定内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是SpringBoot+Mybatis实现Mapper接口与Sql绑定几种姿势的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java