1.背景介绍

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

2.代码

2.1 BaseMapper.java

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

  1. /**
  2. * @author 吕梁山
  3. * @date 2019/4/23
  4. */
  5. public interface BaseMapper<T> {
  6.  
  7. int deleteByPrimaryKey(Integer id);
  8.  
  9. int insert(T entity);
  10.  
  11. int insertSelective(T entity);
  12.  
  13. int updateByPrimaryKeySelective(T entity);
  14.  
  15. int updateByPrimaryKey(T entity);
  16.  
  17. T selectByPrimaryKey(Integer id);
  18. }

2.2 UserMapper.java

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

  1. public interface UserMapper extends BaseMapper<User> { }

2.3 ExtUserMapper.java

mapper的扩展类,业务相关的

  1. /**
  2. * @author 吕梁山
  3. * @date 2019/4/25
  4. */
  5. public interface ExtUserMapper extends UserMapper {
  6.  
  7. ExtUser selectUserByOpenId(String openId);
  8.  
  9. int existUserByOpenId(String openId);
  10.  
  11. int updateByOpenId(User user);
  12. }

2.4 UserMapper.xml

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

注意namespace要写正确

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.pikaqiu.barber.dao.base.UserMapper">
  4. <resultMap id="BaseResultMap" type="com.pikaqiu.barber.entity.base.User">
  5. <id column="id" property="id" jdbcType="INTEGER"/>
  6. <result column="user_name" property="userName" jdbcType="VARCHAR"/>
  7. <result column="user_img" property="userImg" jdbcType="VARCHAR"/>
  8. <result column="open_id" property="openId" jdbcType="VARCHAR"/>
  9. <result column="phone" property="phone" jdbcType="VARCHAR"/>
  10. <result column="sex" property="sex" jdbcType="INTEGER"/>
  11. <result column="province" property="province" jdbcType="VARCHAR"/>
  12. <result column="country" property="country" jdbcType="VARCHAR"/>
  13. <result column="city" property="city" jdbcType="VARCHAR"/>
  14. <result column="birth_date" property="birthDate" jdbcType="VARCHAR"/>
  15. <result column="subscribe_date" property="subscribeDate" jdbcType="TIMESTAMP"/>
  16. <result column="subscribe_scene" property="subscribeScene" jdbcType="VARCHAR"/>
  17. <result column="create_date" property="createDate" jdbcType="TIMESTAMP"/>
  18. </resultMap>
  19. <sql id="Base_Column_List">
  20. id, user_name, user_img, open_id, phone, sex, province, country, city, birth_date,
  21. subscribe_date, subscribe_scene, create_date
  22. </sql>
  23. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
  24. select
  25. <include refid="Base_Column_List"/>
  26. from t_user
  27. where id = #{id,jdbcType=INTEGER}
  28. </select>
  29. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
  30. delete from t_user
  31. where id = #{id,jdbcType=INTEGER}
  32. </delete>
  33. <insert id="insert" parameterType="com.pikaqiu.barber.entity.base.User">
  34. insert into t_user (id, user_name, user_img,
  35. open_id, phone, sex,
  36. province, country, city,
  37. birth_date, subscribe_date, subscribe_scene,
  38. create_date)
  39. values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{userImg,jdbcType=VARCHAR},
  40. #{openId,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{sex,jdbcType=INTEGER},
  41. #{province,jdbcType=VARCHAR}, #{country,jdbcType=VARCHAR},
  42. #{city,jdbcType=VARCHAR},
  43. #{birthDate,jdbcType=VARCHAR}, #{subscribeDate,jdbcType=TIMESTAMP},
  44. #{subscribeScene,jdbcType=VARCHAR},
  45. #{createDate,jdbcType=TIMESTAMP})
  46. </insert>
  47. <insert id="insertSelective" parameterType="com.pikaqiu.barber.entity.base.User">
  48. insert into t_user
  49. <trim prefix="(" suffix=")" suffixOverrides=",">
  50. <if test="id != null">
  51. id,
  52. </if>
  53. <if test="userName != null">
  54. user_name,
  55. </if>
  56. <if test="userImg != null">
  57. user_img,
  58. </if>
  59. <if test="openId != null">
  60. open_id,
  61. </if>
  62. <if test="phone != null">
  63. phone,
  64. </if>
  65. <if test="sex != null">
  66. sex,
  67. </if>
  68. <if test="province != null">
  69. province,
  70. </if>
  71. <if test="country != null">
  72. country,
  73. </if>
  74. <if test="city != null">
  75. city,
  76. </if>
  77. <if test="birthDate != null">
  78. birth_date,
  79. </if>
  80. <if test="subscribeDate != null">
  81. subscribe_date,
  82. </if>
  83. <if test="subscribeScene != null">
  84. subscribe_scene,
  85. </if>
  86. <if test="createDate != null">
  87. create_date,
  88. </if>
  89. </trim>
  90. <trim prefix="values (" suffix=")" suffixOverrides=",">
  91. <if test="id != null">
  92. #{id,jdbcType=INTEGER},
  93. </if>
  94. <if test="userName != null">
  95. #{userName,jdbcType=VARCHAR},
  96. </if>
  97. <if test="userImg != null">
  98. #{userImg,jdbcType=VARCHAR},
  99. </if>
  100. <if test="openId != null">
  101. #{openId,jdbcType=VARCHAR},
  102. </if>
  103. <if test="phone != null">
  104. #{phone,jdbcType=VARCHAR},
  105. </if>
  106. <if test="sex != null">
  107. #{sex,jdbcType=INTEGER},
  108. </if>
  109. <if test="province != null">
  110. #{province,jdbcType=VARCHAR},
  111. </if>
  112. <if test="country != null">
  113. #{country,jdbcType=VARCHAR},
  114. </if>
  115. <if test="city != null">
  116. #{city,jdbcType=VARCHAR},
  117. </if>
  118. <if test="birthDate != null">
  119. #{birthDate,jdbcType=VARCHAR},
  120. </if>
  121. <if test="subscribeDate != null">
  122. #{subscribeDate,jdbcType=TIMESTAMP},
  123. </if>
  124. <if test="subscribeScene != null">
  125. #{subscribeScene,jdbcType=VARCHAR},
  126. </if>
  127. <if test="createDate != null">
  128. #{createDate,jdbcType=TIMESTAMP},
  129. </if>
  130. </trim>
  131. </insert>
  132. <update id="updateByPrimaryKeySelective" parameterType="com.pikaqiu.barber.entity.base.User">
  133. update t_user
  134. <set>
  135. <if test="userName != null">
  136. user_name = #{userName,jdbcType=VARCHAR},
  137. </if>
  138. <if test="userImg != null">
  139. user_img = #{userImg,jdbcType=VARCHAR},
  140. </if>
  141. <if test="openId != null">
  142. open_id = #{openId,jdbcType=VARCHAR},
  143. </if>
  144. <if test="phone != null">
  145. phone = #{phone,jdbcType=VARCHAR},
  146. </if>
  147. <if test="sex != null">
  148. sex = #{sex,jdbcType=INTEGER},
  149. </if>
  150. <if test="province != null">
  151. province = #{province,jdbcType=VARCHAR},
  152. </if>
  153. <if test="country != null">
  154. country = #{country,jdbcType=VARCHAR},
  155. </if>
  156. <if test="city != null">
  157. city = #{city,jdbcType=VARCHAR},
  158. </if>
  159. <if test="birthDate != null">
  160. birth_date = #{birthDate,jdbcType=VARCHAR},
  161. </if>
  162. <if test="subscribeDate != null">
  163. subscribe_date = #{subscribeDate,jdbcType=TIMESTAMP},
  164. </if>
  165. <if test="subscribeScene != null">
  166. subscribe_scene = #{subscribeScene,jdbcType=VARCHAR},
  167. </if>
  168. <if test="createDate != null">
  169. create_date = #{createDate,jdbcType=TIMESTAMP},
  170. </if>
  171. </set>
  172. where id = #{id,jdbcType=INTEGER}
  173. </update>
  174. <update id="updateByPrimaryKey" parameterType="com.pikaqiu.barber.entity.base.User">
  175. update t_user
  176. set user_name = #{userName,jdbcType=VARCHAR},
  177. user_img = #{userImg,jdbcType=VARCHAR},
  178. open_id = #{openId,jdbcType=VARCHAR},
  179. phone = #{phone,jdbcType=VARCHAR},
  180. sex = #{sex,jdbcType=INTEGER},
  181. province = #{province,jdbcType=VARCHAR},
  182. country = #{country,jdbcType=VARCHAR},
  183. city = #{city,jdbcType=VARCHAR},
  184. birth_date = #{birthDate,jdbcType=VARCHAR},
  185. subscribe_date = #{subscribeDate,jdbcType=TIMESTAMP},
  186. subscribe_scene = #{subscribeScene,jdbcType=VARCHAR},
  187. create_date = #{createDate,jdbcType=TIMESTAMP}
  188. where id = #{id,jdbcType=INTEGER}
  189. </update>
  190. </mapper>

2.5 ExtUserMapper.xml

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

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.pikaqiu.barber.dao.ExtUserMapper">
  4.  
  5. <resultMap id="BaseResultMap" type="com.pikaqiu.barber.entity.ExtUser">
  6. <id column="id" property="id" jdbcType="INTEGER"/>
  7. <result column="user_name" property="userName" jdbcType="VARCHAR"/>
  8. <result column="user_img" property="userImg" jdbcType="VARCHAR"/>
  9. <result column="open_id" property="openId" jdbcType="VARCHAR"/>
  10. <result column="phone" property="phone" jdbcType="VARCHAR"/>
  11. <result column="sex" property="sex" jdbcType="INTEGER"/>
  12. <result column="province" property="province" jdbcType="VARCHAR"/>
  13. <result column="country" property="country" jdbcType="VARCHAR"/>
  14. <result column="city" property="city" jdbcType="VARCHAR"/>
  15. <result column="birth_date" property="birthDate" jdbcType="VARCHAR"/>
  16. <result column="subscribe_date" property="subscribeDate" jdbcType="TIMESTAMP"/>
  17. <result column="subscribe_scene" property="subscribeScene" jdbcType="VARCHAR"/>
  18. <result column="create_date" property="createDate" jdbcType="TIMESTAMP"/>
  19. </resultMap>
  20.  
  21. <update id="updateByOpenId" parameterType="com.pikaqiu.barber.entity.base.User" >
  22. update t_user
  23. <set>
  24. <if test="userName != null">
  25. user_name = #{userName,jdbcType=VARCHAR},
  26. </if>
  27. <if test="userImg != null">
  28. user_img = #{userImg,jdbcType=VARCHAR},
  29. </if>
  30. <if test="phone != null">
  31. phone = #{phone,jdbcType=VARCHAR},
  32. </if>
  33. <if test="sex != null">
  34. sex = #{sex,jdbcType=INTEGER},
  35. </if>
  36. <if test="province != null">
  37. province = #{province,jdbcType=VARCHAR},
  38. </if>
  39. <if test="country != null">
  40. country = #{country,jdbcType=VARCHAR},
  41. </if>
  42. <if test="city != null">
  43. city = #{city,jdbcType=VARCHAR},
  44. </if>
  45. <if test="birthDate != null">
  46. birth_date = #{birthDate,jdbcType=VARCHAR},
  47. </if>
  48. <if test="subscribeDate != null">
  49. subscribe_date = #{subscribeDate,jdbcType=TIMESTAMP},
  50. </if>
  51. <if test="subscribeScene != null">
  52. subscribe_scene = #{subscribeScene,jdbcType=VARCHAR},
  53. </if>
  54. <if test="createDate != null">
  55. create_date = #{createDate,jdbcType=TIMESTAMP},
  56. </if>
  57. </set>
  58. where open_id = #{openId,jdbcType=INTEGER}
  59. </update>
  60. <select id="selectUserByOpenId" parameterType="String" resultMap="BaseResultMap">
  61. select *
  62. from t_user
  63. where open_id = #{openId,jdbcType=VARCHAR}
  64. </select>
  65.  
  66. <select id="existUserByOpenId" parameterType="String" resultType="Integer">
  67. select count(0)
  68. from t_user
  69. where open_id = #{openId,jdbcType=VARCHAR}
  70. </select>
  71. </mapper>

2.6 UserServiceImpl.java

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

  1. /**
  2. * @author 吕梁山
  3. * @date 2019/4/23
  4. */
  5. @Service("userService")
  6. public class UserServiceImpl implements UserService {
  7.  
  8. @Resource
  9. private ExtUserMapper extUserMapper;
  10.  
  11. @Override
  12. public ExtUser getUserByOpenId(String openId) {
  13. return extUserMapper.selectUserByOpenId(openId);
  14. }
  15. }

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

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

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

Mybatis逆向生成使用扩展类的更多相关文章

  1. 「小程序JAVA实战」Springboot版mybatis逆向生成工具(32)

    转自:https://idig8.com/2018/08/29/xiaochengxujavashizhanspringbootbanmybatisnixiangshengchenggongju32/ ...

  2. myBatis逆向生成及使用

    引入数据库驱动 <!-- mybatis逆向生成包 --><dependency> <groupId>org.mybatis.generator</group ...

  3. eclipse从数据库逆向生成Hibernate实体类

    做项目必然要先进行数据库表设计,然后根据数据库设计建立实体类(VO),这是理所当然的,但是到公司里做项目后,让我认识到,没有说既进行完数据库设计后还要再“自己”建立一变VO.意思是,在项目设计时,要么 ...

  4. [转]eclipse借助hibernate tool从数据库逆向生成Hibernate实体类

    如何从数据库逆向生成Hibernate实体类呢??? 1. 首先,要在eclipse中采用自带的数据库管理器(Data Management),连通你的数据库: 然后选择数据库,这里用的oracle, ...

  5. (转) Eclipse通过HibernateTools实现逆向生成Hibernate实体类

    背景:工作中使用Hibernate进行持久化的开发工作,所以有必要详细了解这方面的知识. ps:这里有个问题就是刷新表的时候速度太慢了.还不如自己手动去创建.如果表太多倒是可以采取批量生成的策略. 在 ...

  6. MyBatis逆向工程生成的Example类的方法总结

    很早之前就在项目开发中多次使用MyBatis逆向工程生成的Example类,但一直没有对其下的方法做一个简单的总结,现总结如下:一.mapper接口中的方法解析mapper接口中的部分常用方法及功能如 ...

  7. 【SSM 4】Mybatis逆向生成工具

    在上一篇博客中说到,Mybatis是灵活的SQL语句应用,不想Hibernate一样有其封装好的方法,那么,当我们用Mybatis的时候(Hibernate),我们都需要编写其实体类,和配置文件.本篇 ...

  8. Springboot学习与mybatis逆向生成工具

    最近H2数据库越用越觉得方便,在不同办公处无缝继续demo的感觉就是爽.   今天接上一篇Springboot简洁整合mybatis,补上sts(即eclipse)使用mybatis generato ...

  9. Mybatis逆向生成

    在已经有了数据库的表的时候,为了方便起见,我们可以逆向生成javabean,xml,dao接口等,当然,下载mybaits-generation的工具,我这里用的是eclipse插件,然后准备一 个x ...

随机推荐

  1. SPOJ QTREE4 - Query on a tree IV 树分治

    题意: 给出一棵边带权的树,初始树上所有节点都是白色. 有两种操作: C x,改变节点x的颜色,即白变黑,黑变白 A,询问树中最远的两个白色节点的距离,这两个白色节点可以重合(此时距离为0). 分析: ...

  2. 在O(1)时间内删除链表结点 【微软面试100题 第六十题】

    题目要求: 给定链表的头指针和一个结点指针,在O(1)时间删除该结点. 参考资料:剑指offer第13题. 题目分析: 有几种情况: 1.删除的结点是头结点,且链表不止一个结点: 2.删除的结点是头结 ...

  3. Python学习——第一天

    https://www.runoob.com/python/python-chinese-encoding.html 第一个python程序 [root@mini1 ~]# vi python01.p ...

  4. python-高级编程-01

    [1] 列表推导 问题 我们需要一个[2,4,6,8] 这样的列表 传统写法 res = [] for i in range(10): if i %2 == 0:res.append(i) print ...

  5. CF878D D. Magic Breeding bitset

    D. Magic Breeding time limit per test 4 seconds memory limit per test 1024 megabytes input standard ...

  6. PDO 使用prepared statement 预处理LIMIT等非表字段参数

    由于一些驱动不支持原生的预处理语句,因此PDO可以完全模拟预处理.PDO的模拟预处理是默认打开的,即便MYSQL驱动本身支持预处理,在默认打开的状态下,PDO是不会用到MYSQL本身提供的预处理功能. ...

  7. PDO PDO_MYSQL MYSQLI MYSQL 的区别

    MYSQL,MYSQLI 这两个扩展本身就是访问MYSQL数据库的驱动 PDO则是一个抽象层接口 向程序员提供可调用的API是由,MYSQL驱动.MYSQLI驱动,以及PDO来提供. PDO_MYSQ ...

  8. WebService的简介, 原理, 使用,流程图

    WebService的简介, 原理, 使用   第一部分: 直观概述 WebService的几种概念: 以HTTP协议为基础,通过XML进行客户端和服务器端通信的框架/组件 两个关键点: 1.     ...

  9. CSS3的writing-mode属性

    writing-mode这个CSS属性以前是IE的独有属性,IE5.5浏览器就已经支持了.在很长一段时间里,FireFox, Chrome这些现代浏览器都不支持writing-mode,各大现代浏览器 ...

  10. BZOJ4916 神犇和蒟蒻 【欧拉函数 + 杜教筛】

    题目 很久很久以前,有一只神犇叫yzy; 很久很久之后,有一只蒟蒻叫lty; 输入格式 请你读入一个整数N;1<=N<=1E9,A.B模1E9+7; 输出格式 请你输出一个整数A=\sum ...