最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis逆向生成使用擴(kuò)展類(lèi)的實(shí)例代碼詳解

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

1.背景介紹

用的mybatis自動(dòng)生成的插件,然而每次更改數(shù)據(jù)庫(kù)的時(shí)候重新生成需要替換原有的mapper.xml文件,都要把之前業(yè)務(wù)相關(guān)的sql重新寫(xiě)一遍,感覺(jué)十分麻煩,就想著把自動(dòng)生成的作為一個(gè)基礎(chǔ)文件,然后業(yè)務(wù)相關(guān)的寫(xiě)在擴(kuò)展文件里面,這樣更改數(shù)據(jù)庫(kù)后只需要把所有基礎(chǔ)文件替換掉就可以了

2.代碼

2.1 BaseMapper.java

把自動(dòng)生成的方法都抽到一個(gè)base類(lèi),然后可以寫(xiě)一些公共的方法

/**
 * @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

自動(dòng)生成的mapper文件,里面基本都是空的了

public interface UserMapper extends BaseMapper<User> { }

2.3 ExtUserMapper.java

mapper的擴(kuò)展類(lèi),業(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

自動(dòng)生成的mapper.xml文件,沒(méi)有改動(dòng),不同的生成器生成的可能不同

注意namespace要寫(xiě)正確

<?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,這里用不了自動(dòng)生成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)用的時(shí)候直接調(diào)用擴(kuò)展的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不在同一個(gè)目錄,需要在application.yml將所有mapper.xml文件都添加到掃描中

mybatis:
 #掃描sql.xml文件
 mapper-locations: classpath:mapping/**/*.xml
 #自動(dòng)掃描實(shí)體類(lèi)
 type-aliases-package: com.pikaqiu.barber.entity

至此,每次更改數(shù)據(jù)庫(kù)結(jié)構(gòu)后,直接重新生成文件對(duì)base文件進(jìn)行替換即可,不需要再去將業(yè)務(wù)代碼復(fù)制重新粘貼

總結(jié)

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

相關(guān)文章

  • Java?SE封裝、包、static關(guān)鍵字和代碼塊示例詳解

    Java?SE封裝、包、static關(guān)鍵字和代碼塊示例詳解

    這篇文章主要給大家介紹了關(guān)于Java?SE封裝、包、static關(guān)鍵字和代碼塊的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • JAVA異常信息Exception?e及e的相關(guān)方法解讀

    JAVA異常信息Exception?e及e的相關(guān)方法解讀

    這篇文章主要介紹了JAVA異常信息Exception?e及e的相關(guān)方法解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Java中的CompletableFuture基本用法

    Java中的CompletableFuture基本用法

    這篇文章主要介紹了Java中的CompletableFuture基本用法,CompletableFuture是java.util.concurrent庫(kù)在java 8中新增的主要工具,同傳統(tǒng)的Future相比,其支持流式計(jì)算、函數(shù)式編程、完成通知、自定義異常處理等很多新的特性,需要的朋友可以參考下
    2024-01-01
  • Java中的ArrayList底層源碼分析

    Java中的ArrayList底層源碼分析

    這篇文章主要介紹了Java中的ArrayList底層源碼分析,通過(guò)下標(biāo)讀取元素的速度很快,這是因?yàn)锳rrayList底層基于數(shù)組實(shí)現(xiàn),可以根據(jù)下標(biāo)快速的找到內(nèi)存地址,接著讀取內(nèi)存地址中存放的數(shù)據(jù),需要的朋友可以參考下
    2023-12-12
  • MybatisPlusInterceptor實(shí)現(xiàn)sql攔截器超詳細(xì)教程

    MybatisPlusInterceptor實(shí)現(xiàn)sql攔截器超詳細(xì)教程

    這篇文章主要給大家介紹了關(guān)于MybatisPlusInterceptor實(shí)現(xiàn)sql攔截器超詳細(xì)教程的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • Mybatis/Mybatis-Plus駝峰式命名映射的實(shí)現(xiàn)

    Mybatis/Mybatis-Plus駝峰式命名映射的實(shí)現(xiàn)

    本文主要介紹了Mybatis-Plus駝峰式命名映射的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Mybatis-plus與Mybatis依賴(lài)沖突問(wèn)題解決方法

    Mybatis-plus與Mybatis依賴(lài)沖突問(wèn)題解決方法

    ,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧這篇文章主要介紹了Mybatis-plus與Mybatis依賴(lài)沖突問(wèn)題解決方法
    2021-04-04
  • Java實(shí)現(xiàn)簡(jiǎn)易版猜燈謎游戲的示例代碼

    Java實(shí)現(xiàn)簡(jiǎn)易版猜燈謎游戲的示例代碼

    燈謎是中秋節(jié)傳統(tǒng)的活動(dòng)之一,而現(xiàn)代化的方式則是將其制作成一個(gè)小游戲,讓用戶(hù)在游戲的過(guò)程中猜燈謎,互動(dòng)體驗(yàn)更佳,所以本文小編就用Java制作一款猜燈謎小游戲吧
    2023-09-09
  • Java中的緩沖流詳細(xì)解析

    Java中的緩沖流詳細(xì)解析

    這篇文章主要介紹了Java中的緩沖流詳細(xì)解析,緩沖流可以分為字節(jié)緩沖流,字符緩沖流,字節(jié)緩沖流可分為字節(jié)輸?入緩沖流,字節(jié)輸出緩沖流,字符緩沖流可以分為字符輸入緩沖流,字符輸出緩沖流,需要的朋友可以參考下
    2023-11-11
  • spring容器初始化遇到的死鎖問(wèn)題解決

    spring容器初始化遇到的死鎖問(wèn)題解決

    這篇文章主要給大家介紹了關(guān)于spring容器初始化時(shí)候遇到的死鎖問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07

最新評(píng)論

永济市| 镇康县| 大安市| 资溪县| 怀安县| 游戏| 肥城市| 尼勒克县| 荣成市| 民县| 渭源县| 澄迈县| 汽车| 交口县| 罗城| 安图县| 茶陵县| 额济纳旗| 苍山县| 东山县| 连平县| 廊坊市| 松江区| 鹤峰县| 罗定市| 什邡市| 双鸭山市| 莎车县| 来凤县| 大石桥市| 平遥县| 会东县| 青田县| 嘉禾县| 泰宁县| 锦州市| 华宁县| 漳浦县| 谷城县| 清流县| 天镇县|