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逆向生成使用扩展类的更多相关文章
- 「小程序JAVA实战」Springboot版mybatis逆向生成工具(32)
转自:https://idig8.com/2018/08/29/xiaochengxujavashizhanspringbootbanmybatisnixiangshengchenggongju32/ ...
- myBatis逆向生成及使用
引入数据库驱动 <!-- mybatis逆向生成包 --><dependency> <groupId>org.mybatis.generator</group ...
- eclipse从数据库逆向生成Hibernate实体类
做项目必然要先进行数据库表设计,然后根据数据库设计建立实体类(VO),这是理所当然的,但是到公司里做项目后,让我认识到,没有说既进行完数据库设计后还要再“自己”建立一变VO.意思是,在项目设计时,要么 ...
- [转]eclipse借助hibernate tool从数据库逆向生成Hibernate实体类
如何从数据库逆向生成Hibernate实体类呢??? 1. 首先,要在eclipse中采用自带的数据库管理器(Data Management),连通你的数据库: 然后选择数据库,这里用的oracle, ...
- (转) Eclipse通过HibernateTools实现逆向生成Hibernate实体类
背景:工作中使用Hibernate进行持久化的开发工作,所以有必要详细了解这方面的知识. ps:这里有个问题就是刷新表的时候速度太慢了.还不如自己手动去创建.如果表太多倒是可以采取批量生成的策略. 在 ...
- MyBatis逆向工程生成的Example类的方法总结
很早之前就在项目开发中多次使用MyBatis逆向工程生成的Example类,但一直没有对其下的方法做一个简单的总结,现总结如下:一.mapper接口中的方法解析mapper接口中的部分常用方法及功能如 ...
- 【SSM 4】Mybatis逆向生成工具
在上一篇博客中说到,Mybatis是灵活的SQL语句应用,不想Hibernate一样有其封装好的方法,那么,当我们用Mybatis的时候(Hibernate),我们都需要编写其实体类,和配置文件.本篇 ...
- Springboot学习与mybatis逆向生成工具
最近H2数据库越用越觉得方便,在不同办公处无缝继续demo的感觉就是爽. 今天接上一篇Springboot简洁整合mybatis,补上sts(即eclipse)使用mybatis generato ...
- Mybatis逆向生成
在已经有了数据库的表的时候,为了方便起见,我们可以逆向生成javabean,xml,dao接口等,当然,下载mybaits-generation的工具,我这里用的是eclipse插件,然后准备一 个x ...
随机推荐
- SPOJ QTREE4 - Query on a tree IV 树分治
题意: 给出一棵边带权的树,初始树上所有节点都是白色. 有两种操作: C x,改变节点x的颜色,即白变黑,黑变白 A,询问树中最远的两个白色节点的距离,这两个白色节点可以重合(此时距离为0). 分析: ...
- 在O(1)时间内删除链表结点 【微软面试100题 第六十题】
题目要求: 给定链表的头指针和一个结点指针,在O(1)时间删除该结点. 参考资料:剑指offer第13题. 题目分析: 有几种情况: 1.删除的结点是头结点,且链表不止一个结点: 2.删除的结点是头结 ...
- Python学习——第一天
https://www.runoob.com/python/python-chinese-encoding.html 第一个python程序 [root@mini1 ~]# vi python01.p ...
- python-高级编程-01
[1] 列表推导 问题 我们需要一个[2,4,6,8] 这样的列表 传统写法 res = [] for i in range(10): if i %2 == 0:res.append(i) print ...
- CF878D D. Magic Breeding bitset
D. Magic Breeding time limit per test 4 seconds memory limit per test 1024 megabytes input standard ...
- PDO 使用prepared statement 预处理LIMIT等非表字段参数
由于一些驱动不支持原生的预处理语句,因此PDO可以完全模拟预处理.PDO的模拟预处理是默认打开的,即便MYSQL驱动本身支持预处理,在默认打开的状态下,PDO是不会用到MYSQL本身提供的预处理功能. ...
- PDO PDO_MYSQL MYSQLI MYSQL 的区别
MYSQL,MYSQLI 这两个扩展本身就是访问MYSQL数据库的驱动 PDO则是一个抽象层接口 向程序员提供可调用的API是由,MYSQL驱动.MYSQLI驱动,以及PDO来提供. PDO_MYSQ ...
- WebService的简介, 原理, 使用,流程图
WebService的简介, 原理, 使用 第一部分: 直观概述 WebService的几种概念: 以HTTP协议为基础,通过XML进行客户端和服务器端通信的框架/组件 两个关键点: 1. ...
- CSS3的writing-mode属性
writing-mode这个CSS属性以前是IE的独有属性,IE5.5浏览器就已经支持了.在很长一段时间里,FireFox, Chrome这些现代浏览器都不支持writing-mode,各大现代浏览器 ...
- BZOJ4916 神犇和蒟蒻 【欧拉函数 + 杜教筛】
题目 很久很久以前,有一只神犇叫yzy; 很久很久之后,有一只蒟蒻叫lty; 输入格式 请你读入一个整数N;1<=N<=1E9,A.B模1E9+7; 输出格式 请你输出一个整数A=\sum ...