後端idea插件

前言

   入職3個月的時間,通過這3個月品管系統的開發,發現我50%的時間都浪費在複製粘貼代碼上,以及在各個文件之間添接口和文件跳轉上,我認爲後端應該把主要的時間花在業務實現上,而且我認爲公司應該在項目開發上結構保持統一。我對項目進行了簡單總結,發現大部分的工作都是類似模板式的方法,這種代碼不應該一行一行出來,所以我開發了一個針對公司項目的定製idea插件,通過插件生成項目的50%的代碼,而且出錯的概率能夠減小一些,希望可以減少項目開發的週期。
   公司有很多的中間件,比如發號器,wmonitor等組件,這些組件我希望可以通過idea插件一鍵式添加,直接把代碼添加帶項目中,包括很多工具類等。這部分功能暫時還沒開發。
   浪費了好n個週六週日,加了n天的班,踩過無數個坑,看了很多插件開發的api,看了mybatis generator的源碼。完成了插件的初版,寫一篇博客記錄一下,主要是介紹一下插件的功能,插件主要分爲3部分,mybatis文件的生成、文件創建、文件修改。
   旨在一切需要複製粘貼的地方全部用插件生成

一、mybatis文件的生成

1.1 介紹

   看了mybatis generator源碼,對生成文件進行了深度定製,對產生文件不需要修改,可以直接使用。而且生成的文件符合公司開發的標準,包括繼承baseBean,實現序列化等。
在這裏插入圖片描述
   紅框中的是對產生文件修改的代碼,如果想按自己的意願生成代碼,重寫上面的類即可。

1.2 使用說明

   要使用插件首先要在項目中加一個名爲“blibee.properties”配置文件,提前設置好裏面的參數,文件可以放在項目中的任意位置,但儘量保證項目中只有一個配置文件。配置文件內容如下

# 數據庫連接信息
jdbc.url=jdbc:mysql://localhost:3306/team5
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123456

# 主要字段,意思是根據code查詢還是id,默認是id
BeeGenPlugin.mainColumn=code

# 是否是邏輯刪除,true代表是
BeeGenPlugin.isLogicallyDeleted=true

# mapper.xml的後綴名
RenameSqlMapperPlugin.mapperXmlSuffix=Mapper

# 接口的的後綴名
RenameJavaMapperPlugin.interfaceMapperSuffix=Dao

# Query類後綴名
BeeGenPlugin.queryClassSuffix=Param

# Query類(查詢類)的包名
BeeGenPlugin.queryPackage=com.bianlifeng.tempoon.aladdin.qcs.core.api.baseinfo.bean.param

# Query類java包的在磁盤中的絕對路徑
BeeGenPlugin.queryProject=/Users/zk/Desktop/pin2/qcs-core-api/src/main/java

# 實體類的包名
javaModelGenerator.modelPackage=com.bianlifeng.tempoon.aladdin.qcs.core.model.baseinfo

# 實體類java包的在磁盤中的絕對路徑
javaModelGenerator.modelProject=/Users/zk/Desktop/pin2/qcs-core/dao/src/main/java

# 接口類的包名
javaClientGenerator.interfaceMapperPackage=com.bianlifeng.tempoon.aladdin.qcs.core.dao.baseinfo

# 接口類java包的在磁盤中的絕對路徑
javaClientGenerator.interfaceMapperProject=/Users/zk/Desktop/pin2/qcs-core/dao/src/main/java

# xml文件的包名
sqlMapGenerator.mapperXmlPackage=mappers

# 上面包的在磁盤上的絕對路徑
sqlMapGenerator.mapperXmlProject=/Users/zk/Desktop/pin2/qcs-core/provider/src/main/resources/mybatis

1.2.1 Mybatis Generator

   生成實體類,dao接口,映射文件,查詢類。S
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
生成結果

package com.bianlifeng.tempoon.aladdin.qcs.core.model.baseinfo;

import com.wormpex.api.pojo.BaseBean;
import java.util.Date;

/**
 * @table_name user
 * @author ke.zhang02 
 * @date 2018-11-04
 */
public class User extends BaseBean {
    private Integer userId;

    /**
     * 用戶名
     */
    private String username;

    /**
     * 密碼
     */
    private String password;

    /**
     * 金額
     */
    private Integer money;

    private Date createTime;

    private String code;

    private String content;

    private static final long serialVersionUID = -6623057708469335114L;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getMoney() {
        return money;
    }

    public void setMoney(Integer money) {
        this.money = money;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
package com.bianlifeng.tempoon.aladdin.qcs.core.dao.baseinfo;

import com.bianlifeng.tempoon.aladdin.qcs.core.api.baseinfo.bean.param.UserParam;
import com.bianlifeng.tempoon.aladdin.qcs.core.model.baseinfo.User;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface UserDao {
    User selectByUserId(Integer record);

    List<User> selectList(UserParam record);

    List<User> selectPage(UserParam record);

    int count(UserParam record);

    int insert(User record);

    int insertList(@Param("list") List<User> list);

    int update(User record);

    int deleteByUserId(Integer record);

    int deleteList(@Param("list") List<User> list);
}
<?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.bianlifeng.tempoon.aladdin.qcs.core.dao.baseinfo.UserDao">
  <resultMap id="BaseResultMap" type="com.bianlifeng.tempoon.aladdin.qcs.core.model.baseinfo.User">
    <id column="user_id" jdbcType="INTEGER" property="userId" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="money" jdbcType="INTEGER" property="money" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="code" jdbcType="VARCHAR" property="code" />
    <result column="content" jdbcType="LONGVARCHAR" property="content" />
  </resultMap>
  <sql id="Base_Column_List">
    user_id,username,password,money,create_time,code,content
  </sql>
  <sql id="sql_where">
    <where>
      <if test="userId != null and userId != ''">  user_id = #{userId,jdbcType=INTEGER} </if>
      <if test="username != null and username != ''"> and username = #{username,jdbcType=VARCHAR} </if>
      <if test="password != null and password != ''"> and password = #{password,jdbcType=VARCHAR} </if>
      <if test="money != null and money != ''"> and money = #{money,jdbcType=INTEGER} </if>
      <if test="createTime != null and createTime != ''"> and create_time = #{createTime,jdbcType=TIMESTAMP} </if>
      <if test="code != null and code != ''"> and code = #{code,jdbcType=VARCHAR} </if>
      <if test="content != null and content != ''"> and content = #{content,jdbcType=LONGVARCHAR} </if>
	</where>
  </sql>
  <sql id="limit">
    <if test='offset != 0 and limit != 0'>
      limit #{offset}, #{limit}
    </if>
    <if test='offset == 0 and limit != 0'>
      limit #{limit}
    </if>
  </sql>
  <select id="selectByUserId" resultMap="BaseResultMap">
    select <include refid="Base_Column_List" /> from user where user_id = #{userId,jdbcType=INTEGER}
  </select>
  <select id="selectList" resultMap="BaseResultMap">
    select <include refid="Base_Column_List" /> from user
    <include refid="sql_where"/>
    ORDER BY create_time DESC
  </select>
  <select id="selectPage" resultMap="BaseResultMap">
    select <include refid="Base_Column_List" /> from user
    <include refid="sql_where"/>
    ORDER BY create_time DESC
    <include refid="limit"/>
  </select>
  <select id="count" resultType="java.lang.Integer">
    SELECT COUNT(0) FROM user
    <include refid="sql_where"/>
  </select>
  <insert id="insert" keyProperty="userId" parameterType="com.bianlifeng.tempoon.aladdin.qcs.core.model.baseinfo.User" useGeneratedKeys="true">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="username != null">username, </if>
      <if test="password != null">password, </if>
      <if test="money != null">money, </if>
      <if test="createTime != null">create_time, </if>
      <if test="code != null">code, </if>
      <if test="content != null">content, </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="username != null"> #{username,jdbcType=VARCHAR}, </if>
      <if test="password != null"> #{password,jdbcType=VARCHAR}, </if>
      <if test="money != null"> #{money,jdbcType=INTEGER}, </if>
      <if test="createTime != null"> #{createTime,jdbcType=TIMESTAMP}, </if>
      <if test="code != null"> #{code,jdbcType=VARCHAR}, </if>
      <if test="content != null"> #{content,jdbcType=LONGVARCHAR}, </if>
    </trim>
  </insert>
  <insert id="insertList" parameterType="java.util.List">
    insert into user
      (username,password,money,create_time,code,content)    
    values
    <foreach collection ="list" item="item" index= "index" separator =",">
      (#{item.username},#{item.password},#{item.money},#{item.createTime},#{item.code},#{item.content})
    </foreach>
  </insert>
  <update id="update" parameterType="com.bianlifeng.tempoon.aladdin.qcs.core.model.baseinfo.User">
    update user
    <set>
      <if test="username != null">username = #{username,jdbcType=VARCHAR},</if>
      <if test="password != null">password = #{password,jdbcType=VARCHAR},</if>
      <if test="money != null">money = #{money,jdbcType=INTEGER},</if>
      <if test="createTime != null">create_time = #{createTime,jdbcType=TIMESTAMP},</if>
      <if test="code != null">code = #{code,jdbcType=VARCHAR},</if>
      <if test="content != null">content = #{content,jdbcType=LONGVARCHAR},</if>
    </set>
    where user_id = #{userId,jdbcType=INTEGER}
  </update>
  <delete id="deleteByUserId">
    update user set del_status = 1  where user_id = #{userId}
  </delete>
  <delete id="deleteList">
    update user set del_status = 1  where user_id in
	<foreach collection="list" index="index" item="item" open="(" separator="," close=")">#{item}</foreach>
  </delete>
</mapper>
package com.bianlifeng.tempoon.aladdin.qcs.core.api.baseinfo.bean.param;

import com.wormpex.api.pojo.page.PageRequest;
import java.util.Date;

public class UserParam extends PageRequest {
    private static final long serialVersionUID = -6706500633706438330L;

    private Integer userId;

    /**
     * 用戶名
     */
    private String username;

    /**
     * 密碼
     */
    private String password;

    /**
     * 金額
     */
    private Integer money;

    private Date createTime;

    private String code;

    private String content;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getMoney() {
        return money;
    }

    public void setMoney(Integer money) {
        this.money = money;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

1.2.2 Add Sql Field

   在.xml中添加數據庫字段,右鍵xml文件,選擇在數據庫中新添加的字段,點擊ok,會在xml中,補充字段,在實體類中補充字段,在Ao類中補充字段,在Vo中補充字段。
在這裏插入圖片描述
在這裏插入圖片描述
生產結果

?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.bianlifeng.tempoon.aladdin.qcs.core.dao.baseinfo.UserDao">
  <resultMap id="BaseResultMap" type="com.bianlifeng.tempoon.aladdin.qcs.core.model.baseinfo.User">
    <id column="user_id" jdbcType="INTEGER" property="userId" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="money" jdbcType="INTEGER" property="money" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="code" jdbcType="VARCHAR" property="code" />
    <result column="content" jdbcType="LONGVARCHAR" property="content" />
    <result column="new_column" jdbcType="INTEGER" property="newColumn"/>
  </resultMap>
  <sql id="Base_Column_List">
    user_id,username,password,money,create_time,code,content,new_column
  </sql>
  <sql id="sql_where">
    <where>
      <if test="userId != null and userId != ''">  user_id = #{userId,jdbcType=INTEGER} </if>
      <if test="username != null and username != ''"> and username = #{username,jdbcType=VARCHAR} </if>
      <if test="password != null and password != ''"> and password = #{password,jdbcType=VARCHAR} </if>
      <if test="money != null and money != ''"> and money = #{money,jdbcType=INTEGER} </if>
      <if test="createTime != null and createTime != ''"> and create_time = #{createTime,jdbcType=TIMESTAMP} </if>
      <if test="code != null and code != ''"> and code = #{code,jdbcType=VARCHAR} </if>
      <if test="content != null and content != ''"> and content = #{content,jdbcType=LONGVARCHAR} </if>
      <if test="newColumn != null and newColumn != ''">and new_column = #{newColumn,jdbcType=INTEGER}</if>
    </where>
  </sql>
  <sql id="limit">
    <if test='offset != 0 and limit != 0'>
      limit #{offset}, #{limit}
    </if>
    <if test='offset == 0 and limit != 0'>
      limit #{limit}
    </if>
  </sql>
  <select id="selectByUserId" resultMap="BaseResultMap">
    select <include refid="Base_Column_List" /> from user where user_id = #{userId,jdbcType=INTEGER}
  </select>
  <select id="selectList" resultMap="BaseResultMap">
    select <include refid="Base_Column_List" /> from user
    <include refid="sql_where"/>
    ORDER BY create_time DESC
  </select>
  <select id="selectPage" resultMap="BaseResultMap">
    select <include refid="Base_Column_List" /> from user
    <include refid="sql_where"/>
    ORDER BY create_time DESC
    <include refid="limit"/>
  </select>
  <select id="count" resultType="java.lang.Integer">
    SELECT COUNT(0) FROM user
    <include refid="sql_where"/>
  </select>
  <insert id="insert" keyProperty="userId" parameterType="com.bianlifeng.tempoon.aladdin.qcs.core.model.baseinfo.User" useGeneratedKeys="true">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="username != null">username, </if>
      <if test="password != null">password, </if>
      <if test="money != null">money, </if>
      <if test="createTime != null">create_time, </if>
      <if test="code != null">code, </if>
      <if test="content != null">content, </if>
      <if test="newColumn != null">new_column,</if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="username != null"> #{username,jdbcType=VARCHAR}, </if>
      <if test="password != null"> #{password,jdbcType=VARCHAR}, </if>
      <if test="money != null"> #{money,jdbcType=INTEGER}, </if>
      <if test="createTime != null"> #{createTime,jdbcType=TIMESTAMP}, </if>
      <if test="code != null"> #{code,jdbcType=VARCHAR}, </if>
      <if test="content != null"> #{content,jdbcType=LONGVARCHAR}, </if>
      <if test="newColumn != null">#{newColumn,jdbcType=INTEGER},</if>
    </trim>
  </insert>
  <insert id="insertList" parameterType="java.util.List">
    insert into user
    (username,password,money,create_time,code,content,new_column)
    values
    <foreach collection="list" item="item" index="index" separator=",">
      (#{item.username},#{item.password},#{item.money},#{item.createTime},#{item.code},#{item.content},#{item.newColumn})
    </foreach>
  </insert>
  <update id="update" parameterType="com.bianlifeng.tempoon.aladdin.qcs.core.model.baseinfo.User">
    update user
    <set>
      <if test="username != null">username = #{username,jdbcType=VARCHAR},</if>
      <if test="password != null">password = #{password,jdbcType=VARCHAR},</if>
      <if test="money != null">money = #{money,jdbcType=INTEGER},</if>
      <if test="createTime != null">create_time = #{createTime,jdbcType=TIMESTAMP},</if>
      <if test="code != null">code = #{code,jdbcType=VARCHAR},</if>
      <if test="content != null">content = #{content,jdbcType=LONGVARCHAR},</if>
      <if test="newColumn != null">new_column = #{newColumn,jdbcType=INTEGER},</if>
    </set>
    where user_id = #{userId,jdbcType=INTEGER}
  </update>
  <delete id="deleteByUserId">
    update user set del_status = 1  where user_id = #{userId}
  </delete>
  <delete id="deleteList">
    update user set del_status = 1  where user_id in
	<foreach collection="list" index="index" item="item" open="(" separator="," close=")">#{item}</foreach>
  </delete>
</mapper>
    /**
     * null
     */
    private Integer newColumn;

二、項目文件的生成

2.1 介紹

   這部分插件主要是生成一些項目文件,包括Service文件及其實現,biz文件及其實現,remote文件及其實現,controller文件及其實現,單元測試文件,adaptor文件等,Aobean文件,VoBean文件 及其實現等。
在這裏插入圖片描述

2.2 使用說明

2.2.1 New Service File

   右鍵項目包路徑,文件會生成到這個包裏面。選擇Dao層的接口文件。
在這裏插入圖片描述
生成結果:
在這裏插入圖片描述

2.2.2 New ServiceImpl File

   右鍵項目包路徑,文件會生成到這個包裏面。選擇service接口文件。
在這裏插入圖片描述
生成結果:
在這裏插入圖片描述

2.2.3 New AoBean File

   右鍵項目包路徑,文件會生成到這個包裏面。選擇Bean文件。
在這裏插入圖片描述
生成結果:
在這裏插入圖片描述

2.2.4 New VoBean File

   右鍵項目包路徑,文件會生成到這個包裏面。選擇Bean文件。
在這裏插入圖片描述
生成結果
在這裏插入圖片描述

2.2.5 New Adaptor File

   右鍵項目包路徑,文件會生成到這個包裏面。填寫類名,選擇類型,這裏要注意,類名一定要以Adaptor結尾,前面的內容是實體類的名稱,一定要寫對,否到找不到類,導不進去報名。
在這裏插入圖片描述
生成結果:
在這裏插入圖片描述
在這裏插入圖片描述

2.2.6 New Biz Interface File

   右鍵項目包路徑,文件會生成到這個包裏面。填寫類名,一定要以Biz結尾。改生成裏面並沒有什麼方法。需要手動寫,但只要把,把結果方法定義好就行,通過New Biz Impl File,插件可以給你生成實現類,不需要自己創建。
在這裏插入圖片描述
生成結果:
在這裏插入圖片描述

2.2.7 New Biz Impl File

   右鍵項目包路徑,文件會生成到這個包裏面。選擇Biz接口文件。先在接口中創建兩個方法。
在這裏插入圖片描述
在這裏插入圖片描述
生成結果:
在這裏插入圖片描述

2.2.8 New Remote File

   右鍵項目包路徑,文件會生成到這個包裏面。選擇Dao層的Biz接口文件:
在這裏插入圖片描述
生成結果:
在這裏插入圖片描述

2.2.9 New RemoteImpl File

   右鍵項目包路徑,文件會生成到這個包裏面。選擇Romote接口文件:
在這裏插入圖片描述
生成結果:
在這裏插入圖片描述

2.2.10 New Test File

   生成單元測試文件,右鍵項目包路徑,文件會生成到這個包裏面。改該功能是根據接口文件生成單測,既可以生成service單測,也可以生成biz單測。
在這裏插入圖片描述
生成結果:
在這裏插入圖片描述

2.2.11 New Controller File

   右鍵項目包路徑,文件會生成到這個包裏面。選擇man層biz接口文件件:
在這裏插入圖片描述
生成結果:
在這裏插入圖片描述

三、編輯頁面代碼生成

3.1 介紹

   這部分插件主要是選擇編輯頁文本,在文件中添加代碼。
在這裏插入圖片描述

3.2 使用說明

3.2.1 Add Method Impl

   添加方法實現,這要是指文件都已經創建完畢,想在接口文件中新加一個方法。選中接口文件中新加的方法,右鍵方法名。方法名一定要是選中狀態。
在這裏插入圖片描述
結果:
在這裏插入圖片描述
在這裏插入圖片描述

在Biz層使用,會在Biz層往上生成
在這裏插入圖片描述

3.2.2 Add Method Test

   根據接口方法,在測試文件中添加單元測試。
在這裏插入圖片描述
結果:
在這裏插入圖片描述

3.2.3 Add Resource Bean

   添加@Resource的參數。比如在service中添加Dao類,在biz中添加service和remote等。編輯頁中右鍵
在這裏插入圖片描述
在這裏插入圖片描述
結果:
在這裏插入圖片描述

3.2.4 Add Annotion

   批量在方法上添加註解。例如添加如下兩個註解:

//如果添加全類名,會自動導包,否則需要手動導包
@com.bianlifeng.tempoon.aladdin.qcs.core.service.common.wmonitor.RecordTime(parent = STATISTICS_QUERY_EFFICIENCY)
@Transactional(rollbackFor = StatusCodeException.class)

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
結果:
在這裏插入圖片描述

3.2.5 Bean Assignment

   快捷對new的實體類賦值,選中實體類名稱
在這裏插入圖片描述
結果:
在這裏插入圖片描述

3.2.6 Add CheckParams

   快速添加bizTemplate參數校驗,選中bizImpl文件中的方法名:
在這裏插入圖片描述
在這裏插入圖片描述
結果:
在這裏插入圖片描述

3.2.7 Add Pager Impl

   快速完成pager方法,選中類名稱,選則需要的service接口
在這裏插入圖片描述
在這裏插入圖片描述
結果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章