如何快速掌握MyBatis映射器mapper使用技巧?

2026-05-25 20:351阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计2143个文字,预计阅读时间需要9分钟。

如何快速掌握MyBatis映射器mapper使用技巧?

目录+ 使用mapper简介+ 使用mapper快速入门(文档)+ 添加依赖及与Spring集成+ XML配置 - 1. 使用MapperScannerConfigurer - 2. XML配置使用Configuration - 实体类映射 - 创建Mapper接口 - 使用mapper简介 - 使用Mapper

目录
  • 通用mapper简介
  • 通用mapper快速入门(文档)
  • 添加依赖
  • 和Spring集成
    • XML 配置
      • 1.使用 MapperScannerConfigurer
      • 2.XML配置使用 Configuration
    • 实体类映射
      • 创建Mapper接口

      通用mapper简介

      通用 Mapper 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及Example相关的单表操作,与mybatisplus相似,对mybatis制作增强不做修改。为什么要用通用mapper?我们这里列举一下原生Mybatis的痛点:

      1、mapper.xml文件里有大量的sql,当数据库表字段变动,配置文件就要修改

      2、需要自己实现sql分页,select * from table where . . . limit 1,3

      自己手写分页,除了传参page、pageSize,还需要返回条目总数count。

      3、数据库可移植性差:如果项目更换数据库,比如oracle-->mysql,mapper.xml中的

      sql要重新写,因为Oracle的PLSQL 和mysql 支持的函数是不同的。

      4、生成的代码量过大。

      5、批量操作,批量插入,批量更新,需要自写。而这些,通过通用mapper就可以很轻松的解决了。

      通用mapper快速入门(文档)

      在线官方文档:gitee.com/free/Mapper/wikis/Home

      官方的文档中介绍了通用mapper的三种使用方式 ,纯java使用方式、与Spring集成方式、与SpringBoot集成方式。我们这里给大家介绍的是与Spring集成方式,其他方式可自行学习。

      添加依赖

      在开始配置前,先添加相关的依赖。

      正常情况下,Spring 和 MyBatis 的集成环境中,应该已经存在下面的依赖:

      <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>版本号</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>版本号</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>版本号</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>版本号</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>版本号</version> </dependency>

      通用 Mapper 支持 MyBatis 3.2.4+

      集成通用 Mapper 在上面的基础上添加下面的依赖:

      <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>最新版本</version> </dependency>

      tk.mybatis:mapper 依赖包含了通用 Mapper 的基础代码以及和 Spring 集成必须的代码

      和Spring集成

      和 Spring 进行集成时,分为 XML 和注解配置两种方式,每种方式又有不同的配置方式。

      这里提供了很多配置方式,使用时选择一种改动最小的方式即可!

      XML 配置

      1.使用 MapperScannerConfigurer

      和通用 Mapper 以前版本一样,可以直接使用 tk.mybatis 提供的 tk.mybatis.spring.mapper.MapperScannerConfigurer 进行配置,这个配置和 MyBatis 官方提供的 org.mybatis.spring.mapper.MapperScannerConfigurer 区别只是第一层的包名,tkorg。所以使用这种方式时,如果你项目已经使用 org. 进行了配置,只需要改成 tk. 即可。

      <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="扫描包名"/> </bean>

      如果你需要对通用 Mapper 进行特殊配置,可以按下面的方式进行配置:

      <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="tk.mybatis.mapper.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="properties"> <value> 参数名=值 参数名2=值2 ... </value> </property> </bean>

      可用配置的参数请看后续的配置文档,配置参数时一行写一个值。

      2.XML配置使用 Configuration

      如果某些第三方也需要特殊的 MapperScannerConfigurer 时,就不能用上面的方式进行配置了,此时可以选择下面这种方式,这种方式要求使用MyBatis (3.4.0+) 和 mybatis-spring (1.3.0+),配置方式如下:

      <!--使用 Configuration 方式进行配置--> <bean id="mybatisConfig" class="tk.mybatis.mapper.session.Configuration"> <!-- 配置通用 Mapper,有三种属性注入方式 --> <property name="mapperProperties"> <value> notEmpty=true </value> </property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configuration" ref="mybatisConfig"/> </bean> <!-- 不需要考虑下面这个,注意这里是 org 的 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="tk.mybatis.mapper.configuration"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>

      这里使用了 tk.mybatis.mapper.session.Configuration ,也就是不能通过读取 mybatis-config.xml 进行配置,上面这种配置更直接,使用 Spring setter 配置属性更方便。当需要配置通用 Mapper 时,使用 mapperProperties 属性配置即可,配置方式和前面的相同,一行一个配置即可。

      配置了一个 mybatisConfig 的 bean 后,在 SqlSessionFactoryBean 中注入即可。

      后面的 MapperScannerConfigurer 只是为了说明这里不需要使用 tk. 开头的类进行配置。

      这种配置方式基本上和任何第三方都不会冲突,如果你遇到了第三方重写 SqlSessionFactoryBean 的情况,就使用前一种方式配置即可。

      实体类映射

      @Table(name = "tb_brand") public class Brand implements Serializable { @Id private Integer id; private String name; private String image; private String letter; private Integer seq; //getter and setter .... }

      @Table 是指定实体类对应的数据库表 @Id 指的是主键映射。经过上面简单的配置后,相 当于就有了 MyBatis 中的关系映射了

      创建Mapper接口

      public interface BrandMapper extends Mapper<Brand> { }

      这里继承了 tk.mybatis.mapper.common.Mapper 接口,在接口上指定了泛型类 型 Brand 。当你继承了 Mapper 接口后,此时就已经有了针对 Brand 的大量方法,方 法如下:

      这些方法中和 MBG 生成的大部分方法都一致,还有一部分 MBG 之外的常用方法。 基础接口 select

      List<T> select(T record)

      根据 T 对象中的属性名称查询 , 类似于 select * from table where t.name=#{name} and t.password = #{password}

      T selectOne(T record)

      根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号

      T selectByPrimaryKey(Object key)

      根据主键查询 说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条 件使用等号

      int selectCount(T record);

      说明:根据实体中的属性查询总数,查询条件使用等号

      基础接口 insert

      int insert(T record);

      说明:保存一个实体, null 的属性也会保存,不会使用数据库默认值

      int insertSelective(T record);

      说明:保存一个实体, null 的属性不会保存,会使用数据库默认值

      基础接口 Update

      int updateByPrimaryKey(T record);

      说明:根据主键更新实体全部字段, null 值会被更新

      int updateByPrimaryKeySelective(T record);

      说明:根据主键更新属性不为 null 的值

      基础接口 delete

      如何快速掌握MyBatis映射器mapper使用技巧?

      int delete(T record);

      说明:根据实体属性作为条件进行删除,查询条件使用等号

      int deleteByPrimaryKey(Object key);

      说明:根据主键字段进行删除,方法参数必须包含完整的主键属性

      到此这篇关于MyBatis mapper快速入门教程的文章就介绍到这了,更多相关MyBatis mapper内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

      本文共计2143个文字,预计阅读时间需要9分钟。

      如何快速掌握MyBatis映射器mapper使用技巧?

      目录+ 使用mapper简介+ 使用mapper快速入门(文档)+ 添加依赖及与Spring集成+ XML配置 - 1. 使用MapperScannerConfigurer - 2. XML配置使用Configuration - 实体类映射 - 创建Mapper接口 - 使用mapper简介 - 使用Mapper

      目录
      • 通用mapper简介
      • 通用mapper快速入门(文档)
      • 添加依赖
      • 和Spring集成
        • XML 配置
          • 1.使用 MapperScannerConfigurer
          • 2.XML配置使用 Configuration
        • 实体类映射
          • 创建Mapper接口

          通用mapper简介

          通用 Mapper 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及Example相关的单表操作,与mybatisplus相似,对mybatis制作增强不做修改。为什么要用通用mapper?我们这里列举一下原生Mybatis的痛点:

          1、mapper.xml文件里有大量的sql,当数据库表字段变动,配置文件就要修改

          2、需要自己实现sql分页,select * from table where . . . limit 1,3

          自己手写分页,除了传参page、pageSize,还需要返回条目总数count。

          3、数据库可移植性差:如果项目更换数据库,比如oracle-->mysql,mapper.xml中的

          sql要重新写,因为Oracle的PLSQL 和mysql 支持的函数是不同的。

          4、生成的代码量过大。

          5、批量操作,批量插入,批量更新,需要自写。而这些,通过通用mapper就可以很轻松的解决了。

          通用mapper快速入门(文档)

          在线官方文档:gitee.com/free/Mapper/wikis/Home

          官方的文档中介绍了通用mapper的三种使用方式 ,纯java使用方式、与Spring集成方式、与SpringBoot集成方式。我们这里给大家介绍的是与Spring集成方式,其他方式可自行学习。

          添加依赖

          在开始配置前,先添加相关的依赖。

          正常情况下,Spring 和 MyBatis 的集成环境中,应该已经存在下面的依赖:

          <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>版本号</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>版本号</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>版本号</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>版本号</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>版本号</version> </dependency>

          通用 Mapper 支持 MyBatis 3.2.4+

          集成通用 Mapper 在上面的基础上添加下面的依赖:

          <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>最新版本</version> </dependency>

          tk.mybatis:mapper 依赖包含了通用 Mapper 的基础代码以及和 Spring 集成必须的代码

          和Spring集成

          和 Spring 进行集成时,分为 XML 和注解配置两种方式,每种方式又有不同的配置方式。

          这里提供了很多配置方式,使用时选择一种改动最小的方式即可!

          XML 配置

          1.使用 MapperScannerConfigurer

          和通用 Mapper 以前版本一样,可以直接使用 tk.mybatis 提供的 tk.mybatis.spring.mapper.MapperScannerConfigurer 进行配置,这个配置和 MyBatis 官方提供的 org.mybatis.spring.mapper.MapperScannerConfigurer 区别只是第一层的包名,tkorg。所以使用这种方式时,如果你项目已经使用 org. 进行了配置,只需要改成 tk. 即可。

          <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="扫描包名"/> </bean>

          如果你需要对通用 Mapper 进行特殊配置,可以按下面的方式进行配置:

          <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="tk.mybatis.mapper.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="properties"> <value> 参数名=值 参数名2=值2 ... </value> </property> </bean>

          可用配置的参数请看后续的配置文档,配置参数时一行写一个值。

          2.XML配置使用 Configuration

          如果某些第三方也需要特殊的 MapperScannerConfigurer 时,就不能用上面的方式进行配置了,此时可以选择下面这种方式,这种方式要求使用MyBatis (3.4.0+) 和 mybatis-spring (1.3.0+),配置方式如下:

          <!--使用 Configuration 方式进行配置--> <bean id="mybatisConfig" class="tk.mybatis.mapper.session.Configuration"> <!-- 配置通用 Mapper,有三种属性注入方式 --> <property name="mapperProperties"> <value> notEmpty=true </value> </property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configuration" ref="mybatisConfig"/> </bean> <!-- 不需要考虑下面这个,注意这里是 org 的 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="tk.mybatis.mapper.configuration"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>

          这里使用了 tk.mybatis.mapper.session.Configuration ,也就是不能通过读取 mybatis-config.xml 进行配置,上面这种配置更直接,使用 Spring setter 配置属性更方便。当需要配置通用 Mapper 时,使用 mapperProperties 属性配置即可,配置方式和前面的相同,一行一个配置即可。

          配置了一个 mybatisConfig 的 bean 后,在 SqlSessionFactoryBean 中注入即可。

          后面的 MapperScannerConfigurer 只是为了说明这里不需要使用 tk. 开头的类进行配置。

          这种配置方式基本上和任何第三方都不会冲突,如果你遇到了第三方重写 SqlSessionFactoryBean 的情况,就使用前一种方式配置即可。

          实体类映射

          @Table(name = "tb_brand") public class Brand implements Serializable { @Id private Integer id; private String name; private String image; private String letter; private Integer seq; //getter and setter .... }

          @Table 是指定实体类对应的数据库表 @Id 指的是主键映射。经过上面简单的配置后,相 当于就有了 MyBatis 中的关系映射了

          创建Mapper接口

          public interface BrandMapper extends Mapper<Brand> { }

          这里继承了 tk.mybatis.mapper.common.Mapper 接口,在接口上指定了泛型类 型 Brand 。当你继承了 Mapper 接口后,此时就已经有了针对 Brand 的大量方法,方 法如下:

          这些方法中和 MBG 生成的大部分方法都一致,还有一部分 MBG 之外的常用方法。 基础接口 select

          List<T> select(T record)

          根据 T 对象中的属性名称查询 , 类似于 select * from table where t.name=#{name} and t.password = #{password}

          T selectOne(T record)

          根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号

          T selectByPrimaryKey(Object key)

          根据主键查询 说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条 件使用等号

          int selectCount(T record);

          说明:根据实体中的属性查询总数,查询条件使用等号

          基础接口 insert

          int insert(T record);

          说明:保存一个实体, null 的属性也会保存,不会使用数据库默认值

          int insertSelective(T record);

          说明:保存一个实体, null 的属性不会保存,会使用数据库默认值

          基础接口 Update

          int updateByPrimaryKey(T record);

          说明:根据主键更新实体全部字段, null 值会被更新

          int updateByPrimaryKeySelective(T record);

          说明:根据主键更新属性不为 null 的值

          基础接口 delete

          如何快速掌握MyBatis映射器mapper使用技巧?

          int delete(T record);

          说明:根据实体属性作为条件进行删除,查询条件使用等号

          int deleteByPrimaryKey(Object key);

          说明:根据主键字段进行删除,方法参数必须包含完整的主键属性

          到此这篇关于MyBatis mapper快速入门教程的文章就介绍到这了,更多相关MyBatis mapper内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!