Mybatis逆向生成使用扩展类的实例代码详解
程序员文章站
2023-11-21 09:34:52
1.背景介绍
用的mybatis自动生成的插件,然而每次更改数据库的时候重新生成需要替换原有的mapper.xml文件,都要把之前业务相关的sql重新写一遍,感觉十分麻烦...
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逆向生成使用扩展类的实例代码详解,希望对大家有所帮助