Mybatis逆向生成使用扩展类的实例代码详解

1.背景介绍

用的mybatis自动生成的插件,然而每次更改数据库的时候重新生成需要替换原有的mapper.xml文件,都要把之前业务相关的sql重新写一遍,感觉十分麻烦,就想着把自动生成的作为一个基础文件,然后业务相关的写在扩展文件里面,这样更改数据库后只需要把所有基础文件替换掉就可以了

2.代码

2.1 BaseMapper.java

把自动生成的方法都抽到一个base类,然后可以写一些公共的方法

/**
 * @author 吕梁山
 * @date 2019/4/23
 */
public interface BaseMapper<T> {
 int deleteByPrimaryKey(Integer id);
 int insert(T entity);
 int insertSelective(T entity);
 int updateByPrimaryKeySelective(T entity);
 int updateByPrimaryKey(T entity);
 T selectByPrimaryKey(Integer id);
}

2.2 UserMapper.java

自动生成的mapper文件,里面基本都是空的了

public interface UserMapper extends BaseMapper<User> { }

2.3 ExtUserMapper.java

mapper的扩展类,业务相关的

/**
 * @author 吕梁山
 * @date 2019/4/25
 */
public interface ExtUserMapper extends UserMapper {

 ExtUser selectUserByOpenId(String openId);

 int existUserByOpenId(String openId);

 int updateByOpenId(User user);
}

2.4 UserMapper.xml

自动生成的mapper.xml文件,没有改动,不同的生成器生成的可能不同

注意namespace要写正确

<?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.pikaqiu.barber.dao.base.UserMapper">
 <resultMap id="BaseResultMap" type="com.pikaqiu.barber.entity.base.User">
  <id column="id" property="id" jdbcType="INTEGER"/>
  <result column="user_name" property="userName" jdbcType="VARCHAR"/>
  <result column="user_img" property="userImg" jdbcType="VARCHAR"/>
  <result column="open_id" property="openId" jdbcType="VARCHAR"/>
  <result column="phone" property="phone" jdbcType="VARCHAR"/>
  <result column="sex" property="sex" jdbcType="INTEGER"/>
  <result column="province" property="province" jdbcType="VARCHAR"/>
  <result column="country" property="country" jdbcType="VARCHAR"/>
  <result column="city" property="city" jdbcType="VARCHAR"/>
  <result column="birth_date" property="birthDate" jdbcType="VARCHAR"/>
  <result column="subscribe_date" property="subscribeDate" jdbcType="TIMESTAMP"/>
  <result column="subscribe_scene" property="subscribeScene" jdbcType="VARCHAR"/>
  <result column="create_date" property="createDate" jdbcType="TIMESTAMP"/>
 </resultMap>
 <sql id="Base_Column_List">
  id, user_name, user_img, open_id, phone, sex, province, country, city, birth_date,
  subscribe_date, subscribe_scene, create_date
 </sql>
 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
  select
  <include refid="Base_Column_List"/>
  from t_user
  where id = #{id,jdbcType=INTEGER}
 </select>
 <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
  delete from t_user
  where id = #{id,jdbcType=INTEGER}
 </delete>
 <insert id="insert" parameterType="com.pikaqiu.barber.entity.base.User">
  insert into t_user (id, user_name, user_img,
       open_id, phone, sex,
       province, country, city,
       birth_date, subscribe_date, subscribe_scene,
       create_date)
  values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{userImg,jdbcType=VARCHAR},
          #{openId,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{sex,jdbcType=INTEGER},
          #{province,jdbcType=VARCHAR}, #{country,jdbcType=VARCHAR},
          #{city,jdbcType=VARCHAR},
          #{birthDate,jdbcType=VARCHAR}, #{subscribeDate,jdbcType=TIMESTAMP},
    #{subscribeScene,jdbcType=VARCHAR},
    #{createDate,jdbcType=TIMESTAMP})
 </insert>
 <insert id="insertSelective" parameterType="com.pikaqiu.barber.entity.base.User">
  insert into t_user
  <trim prefix="(" suffix=")" suffixOverrides=",">
   <if test="id != null">
    id,
   </if>
   <if test="userName != null">
    user_name,
   </if>
   <if test="userImg != null">
    user_img,
   </if>
   <if test="openId != null">
    open_id,
   </if>
   <if test="phone != null">
    phone,
   </if>
   <if test="sex != null">
    sex,
   </if>
   <if test="province != null">
    province,
   </if>
   <if test="country != null">
    country,
   </if>
   <if test="city != null">
    city,
   </if>
   <if test="birthDate != null">
    birth_date,
   </if>
   <if test="subscribeDate != null">
    subscribe_date,
   </if>
   <if test="subscribeScene != null">
    subscribe_scene,
   </if>
   <if test="createDate != null">
    create_date,
   </if>
  </trim>
  <trim prefix="values (" suffix=")" suffixOverrides=",">
   <if test="id != null">
    #{id,jdbcType=INTEGER},
   </if>
   <if test="userName != null">
    #{userName,jdbcType=VARCHAR},
   </if>
   <if test="userImg != null">
    #{userImg,jdbcType=VARCHAR},
   </if>
   <if test="openId != null">
    #{openId,jdbcType=VARCHAR},
   </if>
   <if test="phone != null">
    #{phone,jdbcType=VARCHAR},
   </if>
   <if test="sex != null">
    #{sex,jdbcType=INTEGER},
   </if>
   <if test="province != null">
    #{province,jdbcType=VARCHAR},
   </if>
   <if test="country != null">
    #{country,jdbcType=VARCHAR},
   </if>
   <if test="city != null">
    #{city,jdbcType=VARCHAR},
   </if>
   <if test="birthDate != null">
    #{birthDate,jdbcType=VARCHAR},
   </if>
   <if test="subscribeDate != null">
    #{subscribeDate,jdbcType=TIMESTAMP},
   </if>
   <if test="subscribeScene != null">
    #{subscribeScene,jdbcType=VARCHAR},
   </if>
   <if test="createDate != null">
    #{createDate,jdbcType=TIMESTAMP},
   </if>
  </trim>
 </insert>
 <update id="updateByPrimaryKeySelective" parameterType="com.pikaqiu.barber.entity.base.User">
  update t_user
  <set>
   <if test="userName != null">
    user_name = #{userName,jdbcType=VARCHAR},
   </if>
   <if test="userImg != null">
    user_img = #{userImg,jdbcType=VARCHAR},
   </if>
   <if test="openId != null">
    open_id = #{openId,jdbcType=VARCHAR},
   </if>
   <if test="phone != null">
    phone = #{phone,jdbcType=VARCHAR},
   </if>
   <if test="sex != null">
    sex = #{sex,jdbcType=INTEGER},
   </if>
   <if test="province != null">
    province = #{province,jdbcType=VARCHAR},
   </if>
   <if test="country != null">
    country = #{country,jdbcType=VARCHAR},
   </if>
   <if test="city != null">
    city = #{city,jdbcType=VARCHAR},
   </if>
   <if test="birthDate != null">
    birth_date = #{birthDate,jdbcType=VARCHAR},
   </if>
   <if test="subscribeDate != null">
    subscribe_date = #{subscribeDate,jdbcType=TIMESTAMP},
   </if>
   <if test="subscribeScene != null">
    subscribe_scene = #{subscribeScene,jdbcType=VARCHAR},
   </if>
   <if test="createDate != null">
    create_date = #{createDate,jdbcType=TIMESTAMP},
   </if>
  </set>
  where id = #{id,jdbcType=INTEGER}
 </update>
 <update id="updateByPrimaryKey" parameterType="com.pikaqiu.barber.entity.base.User">
  update t_user
  set user_name  = #{userName,jdbcType=VARCHAR},
   user_img  = #{userImg,jdbcType=VARCHAR},
   open_id   = #{openId,jdbcType=VARCHAR},
   phone   = #{phone,jdbcType=VARCHAR},
   sex    = #{sex,jdbcType=INTEGER},
   province  = #{province,jdbcType=VARCHAR},
   country   = #{country,jdbcType=VARCHAR},
   city   = #{city,jdbcType=VARCHAR},
   birth_date  = #{birthDate,jdbcType=VARCHAR},
   subscribe_date = #{subscribeDate,jdbcType=TIMESTAMP},
   subscribe_scene = #{subscribeScene,jdbcType=VARCHAR},
   create_date  = #{createDate,jdbcType=TIMESTAMP}
  where id = #{id,jdbcType=INTEGER}
 </update>
</mapper>

2.5 ExtUserMapper.xml

业务相关的sql,这里用不了自动生成mapper.xml里面的BaseResultMap这些东西

<?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.pikaqiu.barber.dao.ExtUserMapper">
 <resultMap id="BaseResultMap" type="com.pikaqiu.barber.entity.ExtUser">
  <id column="id" property="id" jdbcType="INTEGER"/>
  <result column="user_name" property="userName" jdbcType="VARCHAR"/>
  <result column="user_img" property="userImg" jdbcType="VARCHAR"/>
  <result column="open_id" property="openId" jdbcType="VARCHAR"/>
  <result column="phone" property="phone" jdbcType="VARCHAR"/>
  <result column="sex" property="sex" jdbcType="INTEGER"/>
  <result column="province" property="province" jdbcType="VARCHAR"/>
  <result column="country" property="country" jdbcType="VARCHAR"/>
  <result column="city" property="city" jdbcType="VARCHAR"/>
  <result column="birth_date" property="birthDate" jdbcType="VARCHAR"/>
  <result column="subscribe_date" property="subscribeDate" jdbcType="TIMESTAMP"/>
  <result column="subscribe_scene" property="subscribeScene" jdbcType="VARCHAR"/>
  <result column="create_date" property="createDate" jdbcType="TIMESTAMP"/>
 </resultMap>
 <update id="updateByOpenId" parameterType="com.pikaqiu.barber.entity.base.User" >
  update t_user
  <set>
   <if test="userName != null">
    user_name = #{userName,jdbcType=VARCHAR},
   </if>
   <if test="userImg != null">
    user_img = #{userImg,jdbcType=VARCHAR},
   </if>
   <if test="phone != null">
    phone = #{phone,jdbcType=VARCHAR},
   </if>
   <if test="sex != null">
    sex = #{sex,jdbcType=INTEGER},
   </if>
   <if test="province != null">
    province = #{province,jdbcType=VARCHAR},
   </if>
   <if test="country != null">
    country = #{country,jdbcType=VARCHAR},
   </if>
   <if test="city != null">
    city = #{city,jdbcType=VARCHAR},
   </if>
   <if test="birthDate != null">
    birth_date = #{birthDate,jdbcType=VARCHAR},
   </if>
   <if test="subscribeDate != null">
    subscribe_date = #{subscribeDate,jdbcType=TIMESTAMP},
   </if>
   <if test="subscribeScene != null">
    subscribe_scene = #{subscribeScene,jdbcType=VARCHAR},
   </if>
   <if test="createDate != null">
    create_date = #{createDate,jdbcType=TIMESTAMP},
   </if>
  </set>
  where open_id = #{openId,jdbcType=INTEGER}
 </update>
 <select id="selectUserByOpenId" parameterType="String" resultMap="BaseResultMap">
  select *
  from t_user
  where open_id = #{openId,jdbcType=VARCHAR}
 </select>

 <select id="existUserByOpenId" parameterType="String" resultType="Integer">
  select count(0)
  from t_user
  where open_id = #{openId,jdbcType=VARCHAR}
 </select>
</mapper>

2.6 UserServiceImpl.java

service层调用的时候直接调用扩展的mapper

/**
 * @author 吕梁山
 * @date 2019/4/23
 */
@Service("userService")
public class UserServiceImpl implements UserService {

 @Resource
 private ExtUserMapper extUserMapper;

 @Override
 public ExtUser getUserByOpenId(String openId) {
  return extUserMapper.selectUserByOpenId(openId);
 }
}

注:如果生成的mapper.xml和extmapper.xml不在同一个目录,需要在application.yml将所有mapper.xml文件都添加到扫描中

mybatis:
 #扫描sql.xml文件
 mapper-locations: classpath:mapping/**/*.xml
 #自动扫描实体类
 type-aliases-package: com.pikaqiu.barber.entity

至此,每次更改数据库结构后,直接重新生成文件对base文件进行替换即可,不需要再去将业务代码复制重新粘贴

总结

以上所述是小编给大家介绍的Mybatis逆向生成使用扩展类的实例代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

(0)

相关推荐

  • 详解在IDEA中使用MyBatis Generator逆向工程生成代码

    本文介绍一下用Maven工具如何生成Mybatis的代码及映射的文件. 一.配置Maven pom.xml 文件 在pom.xml增加以下插件: <build> <finalName>zsxt</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-pl

  • MyBatis框架之mybatis逆向工程自动生成代码

    Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件. 逆向工程 1.什么是逆向工程 mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml.po..) 企业实际开发中,常用的逆向工程方式: 由于数据库的表生成java代码. 2.下载逆向工程 my

  • IDEA mybatis-generator逆向工程生成代码

    1.在maven工程中的resource中创建generatorConfig.xml 2.配置generatorConfig.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "ht

  • Mybatis逆向生成使用扩展类的实例代码详解

    1.背景介绍 用的mybatis自动生成的插件,然而每次更改数据库的时候重新生成需要替换原有的mapper.xml文件,都要把之前业务相关的sql重新写一遍,感觉十分麻烦,就想着把自动生成的作为一个基础文件,然后业务相关的写在扩展文件里面,这样更改数据库后只需要把所有基础文件替换掉就可以了 2.代码 2.1 BaseMapper.java 把自动生成的方法都抽到一个base类,然后可以写一些公共的方法 /** * @author 吕梁山 * @date 2019/4/23 */ public i

  • SQL 使用 VALUES 生成带数据的临时表实例代码详解

    VALUES 是 INSER 命令的子句. INSERT INOT 表名(列名1,列名2,-) VALUES(值1,值2,-) --语法: --SELECT * FROM ( --VALUES -- (1,2,3,......) -- ,(1,2,3,......) -- ,(1,2,3,......) -- ,(1,2,3,......) -- ,(1,2,3,......) -- ,(1,2,3,......) --) AS t(c1,c2,c3......) SELECT * FROM (

  • Mybatis持久层框架入门之CRUD实例代码详解

    目录 1.MyBatis第一个程序 1.1.代码演示 1.2.问题说明 2.CRUD操作 2.1.namespace 2.2.select 2.3.insert 2.4.update 2.5.delete 2.6.拓展思维 1.MyBatis第一个程序 思路流程:搭建环境–>导入Mybatis—>编写代码—>测试 1.1.代码演示 ​ 1.搭建实验数据库 CREATE DATABASE `mybatis`; USE `mybatis`; DROP TABLE IF EXISTS `use

  • Mybatis结果生成键值对的实例代码

    下面给大家介绍下mybatis结果生成键值对的实例代码,具体内容如下所示: 在实际应用中我们经常会遇到这样的情况,需要给下拉框赋值,这个时候就需要键值对了,具体使用方法如下 1,在maper.xml文件中定义结果类型(resultType)定义为hashmap,如下所示 <select id="selectSuperUnitInfo" resultType="hashmap"> SELECT unit_id ,unit_name from unit_in

  • Android快速开发系列 10个常用工具类实例代码详解

    打开大家手上的项目,基本都会有一大批的辅助类,今天特此整理出10个基本每个项目中都会使用的工具类,用于快速开发~~在此感谢群里给我发项目中工具类的兄弟/姐妹~ 1.日志工具类L.java package com.zhy.utils; import android.util.Log; /** * Log统一管理类 * * * */ public class L { private L() { /* cannot be instantiated */ throw new UnsupportedOpe

  • Python生成验证码、计算具体日期是一年中的第几天实例代码详解

    1.约瑟夫环问题 <幸运的基督徒> 有15个基督徒和15个非基督徒在海上遇险,为了能让一部分人活下来不得不将其中15个人扔到海里面去,有个人想了个办法就是大家围成一个圈,由某个人开始从1报数,报到9的人就扔到海里面,他后面的人接着从1开始报数,报到9的人继续扔到海里面,直到扔掉15个人.由于上帝的保佑,15个基督徒都幸免于难,问这些人最开始是怎么站的,哪些位置是基督徒哪些位置是非基督徒. def main(): ''' 先用列表中每个数字代表每个人,然后通过循环替换列表中的数字 用@代表基督徒

  • 使用 Opentype.js 生成字体子集的实例代码详解

    字体子集是将字体文件中部分多余的字符删除,来减小文件大小,从而在 Web 端使用或嵌入到其他应用或系统中,在网上可以找到不少这方面的工具. Opentype.js是一套可以支持浏览器环境和 Node.js 环境的开源 OpenType 字体读写库,利用这个库可以很轻松实现浏览器环境和 Node.js 环境的字体子集功能. 在浏览器环境创建字体子集工具 首先创建一个简单的 HTML界面,包括一个选取字体文件按钮,一个输入框用于输入保留的字符,和一个保存下载按钮. HTML <!DOCTYPE ht

  • Java回调函数实例代码详解

    首先说说什么叫回调函数? 在WINDOWS中,程序员想让系统DLL调用自己编写的一个方法,于是利用DLL当中回调函数(CALLBACK)的接口来编写程序,使它调用,这个就 称为回调.在调用接口时,需要严格的按照定义的参数和方法调用,并且需要处理函数的异步,否则会导致程序的崩溃. 这样的解释似乎还是比较难懂,这里举个简 单的例子: 程序员A写了一段程序(程序a),其中预留有回调函数接口,并封装好了该程序.程序员B要让a调用自己的程序b中的一个方法,于是,他通过a中的接口回调自己b中的方法.目的达到

  • java使用RSA与AES加密解密的实例代码详解

    首先了解下,什么是堆成加密,什么是非对称加密? 对称加密:加密与解密的密钥是相同的,加解密速度很快,比如AES 非对称加密:加密与解密的秘钥是不同的,速度较慢,比如RSA •先看代码(先会用在研究) 相关依赖: <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.58</versio

  • vue动态注册组件实例代码详解

    写本篇文章之前其实也关注过vue中的一个关于加载动态组件is的API,最开始研究它只是用来实现一个tab切换的功能,使用起来也蛮不错的. is 预期:string | Object (组件的选项对象) 用于动态组件且基于 DOM 内模板的限制来工作. 示例: <!-- 当 `currentView` 改变时,组件也跟着改变 --> <component v-bind:is="currentView"></component> 详见vue API中关于

随机推荐