SpringBoot 自动代码生成三层

谁说胖子不能爱 提交于 2020-10-03 03:06:34

 

前言

虽然mybatis已经有了代码生成,但是对于SpringBoot 项目来说生成的还是需要改动,而且也没得逻辑层,和控制层。但是这些东西是逃避不了,所以我就针对单表,做了一个代码生成器。

mybatis-dsc-generator

根据完善的数据库表结构,一键生成dao.java,mapper.xml,service.java,serviceImpl.java,controller.java,完成单表的增删改查、组合条件集合查询,组合条件分页查询。

源码地址

MAVEN地址

<dependency>
    <groupId>com.github.flying-cattle</groupId> <artifactId>mybatis-dsc-generator</artifactId> <version>1.0.0.RELEASE</version> </dependency>

数据表结构样式

CREATE TABLE `order` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID', `order_no` varchar(50) NOT NULL COMMENT '订单编号', `uid` bigint(20) NOT NULL COMMENT '用户ID', `source` varchar(50) NOT NULL COMMENT '来源', `product_id` bigint(20) NOT NULL COMMENT '产品ID', `product_name` varchar(100) NOT NULL COMMENT '产品名字', `unit_price` int(10) unsigned NOT NULL COMMENT '单价', `number` int(10) unsigned NOT NULL COMMENT '数量', `selling_price` int(11) DEFAULT NULL COMMENT '卖价', `state` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '0等待支付,1支付成功,2支付失败,3撤销', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `update_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '交易变化时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='订单信息';

要求必须有表注释,要求必须有主键为id,切为bigint,所有字段必须有注释(便于生成java注释)。

生成的实体类

生成方法参考源码中的:https://github.com/flying-cattle/mybatis-dsc-generator/blob/master/src/main/java/com/github/mybatis/test/TestMain.java

生成的实体类

/**
 * @filename:Order 2018年7月5日
 * @project deal-center  V1.0
 * Copyright(c) 2018 BianP Co. Ltd. 
 * All right reserved. 
 */
package com.xin.dealcenter.entity; import java.io.Serializable; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.ToString; import lombok.NoArgsConstructor; import lombok.AllArgsConstructor; /** * * @Description: 订单 * @Author: BianP * @CreateDate: 2018年7月5日 * @Version: V1.0 * */ @Data @ToString @AllArgsConstructor @NoArgsConstructor public class Order implements Serializable { private static final long serialVersionUID = 1531104207412L; @ApiModelProperty(name = "id" , value = "ID") private Long id; @ApiModelProperty(name = "orderNo" , value = "订单编号") private String orderNo; @ApiModelProperty(name = "uid" , value = "用户ID") private Long uid; @ApiModelProperty(name = "source" , value = "来源") private String source; @ApiModelProperty(name = "productId" , value = "产品ID") private Long productId; @ApiModelProperty(name = "productName" , value = "产品名字") private String productName; @ApiModelProperty(name = "unitPrice" , value = "单价") private Integer unitPrice; @ApiModelProperty(name = "number" , value = "数量") private Integer number; @ApiModelProperty(name = "sellingPrice" , value = "卖价") private Integer sellingPrice; @ApiModelProperty(name = "state" , value = "0等待支付,1支付成功,2支付失败,3撤销") private Integer state; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") @ApiModelProperty(name = "createTime" , value = "创建时间") private Date createTime; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") @ApiModelProperty(name = "updateTime" , value = "交易变化时间") private Date updateTime; }

生成的DAO

/**
 * @filename:OrderDao 2018年7月5日
 * @project deal-center  V1.0
 * Copyright(c) 2018 BianP Co. Ltd. 
 * All right reserved. 
 */
package com.xin.dealcenter.dao; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.xin.dealcenter.entity.Order; /** * * @Description: 订单——DAO * @Author: BianP * @CreateDate: 2018年7月5日 * @Version: V1.0 * */ @Mapper public interface OrderDao { public Order selectByPrimaryKey(Long id); public int deleteByPrimaryKey(Long id); public int insertSelective(Order order); public int updateByPrimaryKeySelective(Order order); public List<Order> queryOrderList(Order order); }

生成的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.xin.dealcenter.dao.OrderDao"> <resultMap id="BaseResultMap" type="com.xin.dealcenter.entity.Order"> <id column="id" jdbcType="BIGINT" property="id" /> <id column="order_no" jdbcType="VARCHAR" property="orderNo" /> <id column="uid" jdbcType="BIGINT" property="uid" /> <id column="source" jdbcType="VARCHAR" property="source" /> <id column="product_id" jdbcType="BIGINT" property="productId" /> <id column="product_name" jdbcType="VARCHAR" property="productName" /> <id column="unit_price" jdbcType="INTEGER" property="unitPrice" /> <id column="number" jdbcType="INTEGER" property="number" /> <id column="selling_price" jdbcType="INTEGER" property="sellingPrice" /> <id column="state" jdbcType="INTEGER" property="state" /> <id column="create_time" jdbcType="TIMESTAMP" property="createTime" /> <id column="update_time" jdbcType="TIMESTAMP" property="updateTime" /> </resultMap> <sql id="Base_Column_List"> id, order_no, uid, source, product_id, product_name, unit_price, number, selling_price, state, create_time, update_time </sql> <!-- 查询 --> <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from order where id = #{id,jdbcType=BIGINT} </select> <!-- 删除 --> <delete id="deleteByPrimaryKey" parameterType="java.lang.Long"> delete from order where id = #{id,jdbcType=BIGINT} </delete> <!-- 选择添加 --> <insert id="insertSelective" parameterType="com.xin.dealcenter.entity.Order"> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long"> SELECT LAST_INSERT_ID() </selectKey> insert into order <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="orderNo != null"> order_no
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!