欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis逆向生成使用擴展類的實例代碼詳解

 更新時間:2019年05月27日 11:36:45   作者:一個寫爛代碼的  
這篇文章主要介紹了Mybatis逆向生成使用擴展類的實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下

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<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的擴展類,業(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要寫正確

<?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

業(yè)務(wù)相關(guān)的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層調(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ù)制重新粘貼

總結(jié)

以上所述是小編給大家介紹的Mybatis逆向生成使用擴展類的實例代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • 詳解springboot項目啟動時如何排除用不到的bean

    詳解springboot項目啟動時如何排除用不到的bean

    使用springboot開發(fā)項目,我們有時候會排除一些項目里面用不到的bean,不然的話項目啟動會報錯,這種情況通常是發(fā)生在什么場景里呢,以及如何解決呢,今天咱們就聊一聊
    2024-01-01
  • Java之實現(xiàn)十進制與十六進制轉(zhuǎn)換案例講解

    Java之實現(xiàn)十進制與十六進制轉(zhuǎn)換案例講解

    這篇文章主要介紹了Java之實現(xiàn)十進制與十六進制轉(zhuǎn)換案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Java 讀取PDF中的文本和圖片的方法

    Java 讀取PDF中的文本和圖片的方法

    本文將介紹通過Java程序來讀取PDF文檔中的文本和圖片的方法。分別調(diào)用方法extractText()和extractImages()來讀取,需要的朋友可以參考下
    2019-07-07
  • Java實現(xiàn)AOP代理的三種方式詳解

    Java實現(xiàn)AOP代理的三種方式詳解

    AOP是一種設(shè)計思想,是軟件設(shè)計領(lǐng)域中的面向切面編程,它是面向?qū)ο缶幊痰囊环N補充和完善。本文將用Java實現(xiàn)AOP代理的三種方式,需要的可以參考一下
    2022-07-07
  • Java線程讓步_動力節(jié)點Java學院整理

    Java線程讓步_動力節(jié)點Java學院整理

    yield()的作用是讓步。它能讓當前線程由“運行狀態(tài)”進入到“就緒狀態(tài)”,從而讓其它具有相同優(yōu)先級的等待線程獲取執(zhí)行權(quán)。下面通過本文給大家介紹Java線程讓步的相關(guān)知識,需要的朋友參考下吧
    2017-05-05
  • 深入淺析springboot中static和templates區(qū)別

    深入淺析springboot中static和templates區(qū)別

    這篇文章主要介紹了springboot中static和templates區(qū)別,本文通過圖文實例代碼相結(jié)合給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • 使用IDEA搭建SSM框架的詳細教程(spring + springMVC +MyBatis)

    使用IDEA搭建SSM框架的詳細教程(spring + springMVC +MyBatis)

    這篇文章主要介紹了使用IDEA搭建SSM框架的詳細教程 spring + springMVC +MyBatis,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • SpringBoot 集成 Nebula的操作過程

    SpringBoot 集成 Nebula的操作過程

    這篇文章主要介紹了SpringBoot 集成 Nebula的操作過程,通過示例代碼介紹了java 環(huán)境下如何對 Nebula Graph 進行操作,感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • JavaWeb Maven詳解相關(guān)配置

    JavaWeb Maven詳解相關(guān)配置

    這篇文章主要介紹了使用maven架構(gòu)管理開發(fā)的相關(guān)配置,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 文件上傳SpringBoot后端MultipartFile參數(shù)報空問題的解決辦法

    文件上傳SpringBoot后端MultipartFile參數(shù)報空問題的解決辦法

    這篇文章主要介紹了文件上傳SpringBoot后端MultipartFile參數(shù)報空問題的解決辦法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11

最新評論