Mybatis逆向生成使用扩展类的实例代码详解
1.背景介绍
用的mybatis自动生成的插件,然而每次更改数据库的时候重新生成需要替换原有的mapper.xml文件,都要把之前业务相关的sql重新写一遍,感觉十分麻烦,就想着把自动生成的作为一个基础文件,然后业务相关的写在扩展文件里面,这样更改数据库后只需要把所有基础文件替换掉就可以了
2.代码
2.1BaseMapper.java
把自动生成的方法都抽到一个base类,然后可以写一些公共的方法
/** *@author吕梁山 *@date2019/4/23 */ publicinterfaceBaseMapper{ intdeleteByPrimaryKey(Integerid); intinsert(Tentity); intinsertSelective(Tentity); intupdateByPrimaryKeySelective(Tentity); intupdateByPrimaryKey(Tentity); TselectByPrimaryKey(Integerid); }
2.2UserMapper.java
自动生成的mapper文件,里面基本都是空的了
publicinterfaceUserMapperextendsBaseMapper{}
2.3ExtUserMapper.java
mapper的扩展类,业务相关的
/** *@author吕梁山 *@date2019/4/25 */ publicinterfaceExtUserMapperextendsUserMapper{ ExtUserselectUserByOpenId(StringopenId); intexistUserByOpenId(StringopenId); intupdateByOpenId(Useruser); }
2.4UserMapper.xml
自动生成的mapper.xml文件,没有改动,不同的生成器生成的可能不同
注意namespace要写正确
id,user_name,user_img,open_id,phone,sex,province,country,city,birth_date, subscribe_date,subscribe_scene,create_date select fromt_user whereid=#{id,jdbcType=INTEGER} deletefromt_user whereid=#{id,jdbcType=INTEGER} insertintot_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}) insertintot_user id, user_name, user_img, open_id, phone, sex, province, country, city, birth_date, subscribe_date, subscribe_scene, create_date, #{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}, updatet_user whereid=#{id,jdbcType=INTEGER} 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}, updatet_user setuser_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} whereid=#{id,jdbcType=INTEGER}
2.5ExtUserMapper.xml
业务相关的sql,这里用不了自动生成mapper.xml里面的BaseResultMap这些东西
updatet_user whereopen_id=#{openId,jdbcType=INTEGER} user_name=#{userName,jdbcType=VARCHAR}, user_img=#{userImg,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}, select* fromt_user whereopen_id=#{openId,jdbcType=VARCHAR} selectcount(0) fromt_user whereopen_id=#{openId,jdbcType=VARCHAR}
2.6UserServiceImpl.java
service层调用的时候直接调用扩展的mapper
/** *@author吕梁山 *@date2019/4/23 */ @Service("userService") publicclassUserServiceImplimplementsUserService{ @Resource privateExtUserMapperextUserMapper; @Override publicExtUsergetUserByOpenId(StringopenId){ returnextUserMapper.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逆向生成使用扩展类的实例代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。