Mybatis如何逆向生成使用扩展类-成都快上网建站

Mybatis如何逆向生成使用扩展类

这篇文章给大家分享的是有关Mybatis如何逆向生成使用扩展类的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

创新互联建站专注于新城企业网站建设,成都响应式网站建设公司,商城网站制作。新城网站建设公司,为新城等地区提供建站服务。全流程定制网站制作,专业设计,全程项目跟踪,创新互联建站专业和态度为您提供的服务

1.背景介绍

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

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的扩展类,业务相关的

/**
 * @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
 
 
  select
  
  from t_user
  where id = #{id,jdbcType=INTEGER}
 
 
  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
  
   
    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 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

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




 
  
  
  
  
  
  
  
  
  
  
  
  
  
 
 
  update t_user
  
   
    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},
   
  
  where open_id = #{openId,jdbcType=INTEGER}
 
 
  select *
  from t_user
  where open_id = #{openId,jdbcType=VARCHAR}
 

 
  select count(0)
  from t_user
  where open_id = #{openId,jdbcType=VARCHAR}
 

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如何逆向生成使用扩展类”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!


文章名称:Mybatis如何逆向生成使用扩展类
URL标题:http://kswjz.com/article/gocces.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流