代碼自動生成
步驟
- 這裡給大家介紹一個代碼生成神器EasyCode插件,可減少重複業務代碼,提高工作效率,減少加班,該插件目前隻在IDEA有
- 安裝步驟如下 File --> Settings… --> 選擇Plugins --> 安裝Easy Code插件 --> 安裝之後注意重新開機 idea
- 接下來使用Easy Code插件,先建立資料表
USE backend_template; CREATE table goods ( id INT not null, name VARCHAR(255) not null, price int null, address VARCHAR(255) null, constraint goods_pk primary key (id) ); INSERT INTO `goods` VALUES ("1","oatmeal","38","Australia"); INSERT INTO `goods` VALUES ("2","Beef","98","Japan");
- 連接配接資料庫
- 配置使用者名和密碼
- 在要生成代碼的資料表上方右鍵
- 然後選擇 EasyCode --> Generate Code
- 包名填com.example.backend_template,路徑預設就好,選擇要代碼自動生成的6個代碼檔案,分别是entity.java、dao.java、service.java、serviceImpl.java、controller.java、mapper.xml,最後點選 OK 代碼即可自動生成 (因為之前的項目目錄結構和EasyCode的預設生成結構一緻,是以這裡不用再次建立目錄)
-
下面是自動生成的代碼
entity/Goods.java
dao/GoodsDaopackage com.example.backend_template.entity; import java.io.Serializable; /** * (Goods)實體類 * * @author makejava * @since 2020-07-04 12:22:03 */ public class Goods implements Serializable { private static final long serialVersionUID = -11102273161171127L; private Integer id; private String name; private Integer price; private String address; //get And set }
mapper/GoodsDao.xmlpackage com.example.backend_template.dao; import com.example.backend_template.entity.Goods; import org.apache.ibatis.annotations.Param; import java.util.List; /** * (Goods)表資料庫通路層 * * @author makejava * @since 2020-07-04 12:22:03 */ public interface GoodsDao { /** * 通過ID查詢單條資料 * * @param id 主鍵 * @return 執行個體對象 */ Goods queryById(Integer id); /** * 查詢指定行資料 * * @param offset 查詢起始位置 * @param limit 查詢條數 * @return 對象清單 */ List<Goods> queryAllByLimit(@Param("offset") int offset, @Param("limit") int limit); /** * 通過實體作為篩選條件查詢 * * @param goods 執行個體對象 * @return 對象清單 */ List<Goods> queryAll(Goods goods); /** * 新增資料 * * @param goods 執行個體對象 * @return 影響行數 */ int insert(Goods goods); /** * 修改資料 * * @param goods 執行個體對象 * @return 影響行數 */ int update(Goods goods); /** * 通過主鍵删除資料 * * @param id 主鍵 * @return 影響行數 */ int deleteById(Integer id); }
service/GoodsService<?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.example.backend_template.dao.GoodsDao"> <resultMap id="BaseResultMap" type="com.example.backend_template.entity.Goods"> <[email protected] goods--> <result property="id" column="id" jdbcType="INTEGER"/> <result property="name" column="name" jdbcType="VARCHAR"/> <result property="price" column="price" jdbcType="INTEGER"/> <result property="address" column="address" jdbcType="VARCHAR"/> </resultMap> <!--查詢單個--> <select id="queryById" resultMap="BaseResultMap"> select id, name, price, address from backend_template.goods where id = #{id} </select> <!--查詢指定行資料--> <select id="queryAllByLimit" resultMap="BaseResultMap"> select id, name, price, address from backend_template.goods limit #{offset}, #{limit} </select> <!--通過實體作為篩選條件查詢--> <select id="queryAll" resultMap="BaseResultMap"> select id, name, price, address from backend_template.goods <where> <if test="id != null"> and id = #{id} </if> <if test="name != null and name != ''"> and name = #{name} </if> <if test="price != null"> and price = #{price} </if> <if test="address != null and address != ''"> and address = #{address} </if> </where> </select> <!--新增所有列--> <insert id="insert" keyProperty="id" useGeneratedKeys="true"> insert into backend_template.goods(name, price, address) values (#{name}, #{price}, #{address}) </insert> <!--通過主鍵修改資料--> <update id="update"> update backend_template.goods <set> <if test="name != null and name != ''"> name = #{name}, </if> <if test="price != null"> price = #{price}, </if> <if test="address != null and address != ''"> address = #{address}, </if> </set> where id = #{id} </update> <!--通過主鍵删除--> <delete id="deleteById"> delete from backend_template.goods where id = #{id} </delete> </mapper>
service/impl/GoodsServiceImplpackage com.example.backend_template.service; import com.example.backend_template.entity.Goods; import java.util.List; /** * (Goods)表服務接口 * * @author makejava * @since 2020-07-04 12:22:03 */ public interface GoodsService { /** * 通過ID查詢單條資料 * * @param id 主鍵 * @return 執行個體對象 */ Goods queryById(Integer id); /** * 查詢多條資料 * * @param offset 查詢起始位置 * @param limit 查詢條數 * @return 對象清單 */ List<Goods> queryAllByLimit(int offset, int limit); /** * 新增資料 * * @param goods 執行個體對象 * @return 執行個體對象 */ Goods insert(Goods goods); /** * 修改資料 * * @param goods 執行個體對象 * @return 執行個體對象 */ Goods update(Goods goods); /** * 通過主鍵删除資料 * * @param id 主鍵 * @return 是否成功 */ boolean deleteById(Integer id); }
controller/GoodsControllerpackage com.example.backend_template.service.impl; import com.example.backend_template.entity.Goods; import com.example.backend_template.dao.GoodsDao; import com.example.backend_template.service.GoodsService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; /** * (Goods)表服務實作類 * * @author makejava * @since 2020-07-04 12:22:03 */ @Service("goodsService") public class GoodsServiceImpl implements GoodsService { @Resource private GoodsDao goodsDao; /** * 通過ID查詢單條資料 * * @param id 主鍵 * @return 執行個體對象 */ @Override public Goods queryById(Integer id) { return this.goodsDao.queryById(id); } /** * 查詢多條資料 * * @param offset 查詢起始位置 * @param limit 查詢條數 * @return 對象清單 */ @Override public List<Goods> queryAllByLimit(int offset, int limit) { return this.goodsDao.queryAllByLimit(offset, limit); } /** * 新增資料 * * @param goods 執行個體對象 * @return 執行個體對象 */ @Override public Goods insert(Goods goods) { this.goodsDao.insert(goods); return goods; } /** * 修改資料 * * @param goods 執行個體對象 * @return 執行個體對象 */ @Override public Goods update(Goods goods) { this.goodsDao.update(goods); return this.queryById(goods.getId()); } /** * 通過主鍵删除資料 * * @param id 主鍵 * @return 是否成功 */ @Override public boolean deleteById(Integer id) { return this.goodsDao.deleteById(id) > 0; } }
package com.example.backend_template.controller; import com.example.backend_template.entity.Goods; import com.example.backend_template.service.GoodsService; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; /** * (Goods)表控制層 * * @author makejava * @since 2020-07-04 12:22:03 */ @RestController @RequestMapping("goods") public class GoodsController { /** * 服務對象 */ @Resource private GoodsService goodsService; /** * 通過主鍵查詢單條資料 * * @param id 主鍵 * @return 單條資料 */ @GetMapping("selectOne") public Goods selectOne(Integer id) { return this.goodsService.queryById(id); } }
我們可以看到,EasyCode不但自動生成了一整套代碼,并且,代碼風格很好,注釋清晰,模闆的意義不隻減少我們重複的開發,更可以規範代碼風格,隻能說妙啊,又可以快樂的寫代碼了
測試
- 運作項目,給這個位址 http://localhost:8080/goods/selectOne/?id=1 發送GET請求,測試一下,如果出現以下結果,說明自動生成的代碼整合成功
- 如果不喜歡他的代碼生成模闆,或者想把代碼風格換成自己的,我們還可以自己定義模闆 File --> Settings… --> Other Settings --> EasyCode-MybatisCodeHelper,自行摸索吧,模闆功能很有意思 測試完後,就可把資料表和生成的六個檔案删除了,因為這些與最後模闆無關,隻是測試而已
項目位址
項目介紹:從零搭建 Spring Boot 後端項目
代碼位址:https://github.com/xiaoxiamo/backend-template
下一篇
八、全局統一異常處理