這篇文章給大家分享的是有關(guān)Mybatis如何逆向生成使用擴展類的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)建站專注于新城企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站制作。新城網(wǎng)站建設(shè)公司,為新城等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站制作,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)
1.背景介紹
用的mybatis自動生成的插件,然而每次更改數(shù)據(jù)庫的時候重新生成需要替換原有的mapper.xml文件,都要把之前業(yè)務(wù)相關(guān)的sql重新寫一遍,感覺十分麻煩,就想著把自動生成的作為一個基礎(chǔ)文件,然后業(yè)務(wù)相關(guān)的寫在擴展文件里面,這樣更改數(shù)據(jù)庫后只需要把所有基礎(chǔ)文件替換掉就可以了
2.代碼
2.1 BaseMapper.java
把自動生成的方法都抽到一個base類,然后可以寫一些公共的方法
/** * @author 呂梁山 * @date 2019/4/23 */ public interface BaseMapper{ 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{ }
2.3 ExtUserMapper.java
mapper的擴展類,業(yè)務(wù)相關(guān)的
/** * @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要寫正確
id, user_name, user_img, open_id, phone, sex, province, country, city, birth_date, subscribe_date, subscribe_scene, create_date delete from t_user where id = #{id,jdbcType=INTEGER} 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 into t_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}, update t_user where id = #{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}, 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}
2.5 ExtUserMapper.xml
業(yè)務(wù)相關(guān)的sql,這里用不了自動生成mapper.xml里面的BaseResultMap這些東西
update t_user where open_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},
2.6 UserServiceImpl.java
service層調(diào)用的時候直接調(diào)用擴展的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
至此,每次更改數(shù)據(jù)庫結(jié)構(gòu)后,直接重新生成文件對base文件進行替換即可,不需要再去將業(yè)務(wù)代碼復(fù)制重新粘貼
感謝各位的閱讀!關(guān)于“Mybatis如何逆向生成使用擴展類”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!