代码自动生成
步骤
-
这里给大家介绍一个代码生成神器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.javapackage 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 }
dao/GoodsDao
package 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); }
mapper/GoodsDao.xml
<?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"> <!--@Table 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/GoodsService
package 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); }
service/impl/GoodsServiceImpl
package 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; } }
controller/GoodsController
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