雖然mybatis已經有了代碼生成,可是對於SpringBoot 項目來講生成的仍是須要改動,並且也沒得邏輯層,和控制層。可是這些東西是逃避不了,因此我就針對單表,作了一個代碼生成器。java
根據完善的數據庫表結構,一鍵生成dao.java,mapper.xml,service.java,serviceImpl.java,controller.java,完成單表的增刪改查、組合條件集合查詢,組合條件分頁查詢。
<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註釋)。git
生成方法參考源碼中的:https://github.com/flying-cattle/mybatis-dsc-generator/blob/master/src/main/java/com/github/mybatis/test/TestMain.javagithub
/** * @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; }
/** * @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 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, </if> <if test="uid != null"> uid, </if> <if test="source != null"> source, </if> <if test="productId != null"> product_id, </if> <if test="productName != null"> product_name, </if> <if test="unitPrice != null"> unit_price, </if> <if test="number != null"> number, </if> <if test="sellingPrice != null"> selling_price, </if> <if test="state != null"> state, </if> <if test="createTime != null"> create_time, </if> <if test="updateTime != null"> update_time, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=BIGINT}, </if> <if test="orderNo != null"> #{orderNo,jdbcType=VARCHAR}, </if> <if test="uid != null"> #{uid,jdbcType=BIGINT}, </if> <if test="source != null"> #{source,jdbcType=VARCHAR}, </if> <if test="productId != null"> #{productId,jdbcType=BIGINT}, </if> <if test="productName != null"> #{productName,jdbcType=VARCHAR}, </if> <if test="unitPrice != null"> #{unitPrice,jdbcType=INTEGER}, </if> <if test="number != null"> #{number,jdbcType=INTEGER}, </if> <if test="sellingPrice != null"> #{sellingPrice,jdbcType=INTEGER}, </if> <if test="state != null"> #{state,jdbcType=INTEGER}, </if> <if test="createTime != null"> #{createTime,jdbcType=TIMESTAMP}, </if> <if test="updateTime != null"> #{updateTime,jdbcType=TIMESTAMP}, </if> </trim> </insert> <!-- 選擇修改 --> <update id="updateByPrimaryKeySelective" parameterType="com.xin.dealcenter.entity.Order"> update order <set> <if test="id != null"> id = #{id,jdbcType=BIGINT}, </if> <if test="orderNo != null"> order_no = #{orderNo,jdbcType=VARCHAR}, </if> <if test="uid != null"> uid = #{uid,jdbcType=BIGINT}, </if> <if test="source != null"> source = #{source,jdbcType=VARCHAR}, </if> <if test="productId != null"> product_id = #{productId,jdbcType=BIGINT}, </if> <if test="productName != null"> product_name = #{productName,jdbcType=VARCHAR}, </if> <if test="unitPrice != null"> unit_price = #{unitPrice,jdbcType=INTEGER}, </if> <if test="number != null"> number = #{number,jdbcType=INTEGER}, </if> <if test="sellingPrice != null"> selling_price = #{sellingPrice,jdbcType=INTEGER}, </if> <if test="state != null"> state = #{state,jdbcType=INTEGER}, </if> <if test="createTime != null"> create_time = #{createTime,jdbcType=TIMESTAMP}, </if> <if test="updateTime != null"> update_time = #{updateTime,jdbcType=TIMESTAMP}, </if> </set> where id = #{id,jdbcType=BIGINT} </update> <!-- 組合條件查詢 --> <select id="queryOrderList" parameterType="com.xin.dealcenter.entity.Order" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from order <where> <if test="id != null"> id = #{id,jdbcType=BIGINT} </if> <if test="orderNo != null"> AND order_no = #{orderNo ,jdbcType=VARCHAR} </if> <if test="uid != null"> AND uid = #{uid ,jdbcType=BIGINT} </if> <if test="source != null"> AND source = #{source ,jdbcType=VARCHAR} </if> <if test="productId != null"> AND product_id = #{productId ,jdbcType=BIGINT} </if> <if test="productName != null"> AND product_name = #{productName ,jdbcType=VARCHAR} </if> <if test="unitPrice != null"> AND unit_price = #{unitPrice ,jdbcType=INTEGER} </if> <if test="number != null"> AND number = #{number ,jdbcType=INTEGER} </if> <if test="sellingPrice != null"> AND selling_price = #{sellingPrice ,jdbcType=INTEGER} </if> <if test="state != null"> AND state = #{state ,jdbcType=INTEGER} </if> <if test="createTime != null"> AND create_time = #{createTime ,jdbcType=TIMESTAMP} </if> <if test="updateTime != null"> AND update_time = #{updateTime ,jdbcType=TIMESTAMP} </if> </where> </select> </mapper>
/** * @filename:OrderService 2018年7月5日 * @project deal-center V1.0 * Copyright(c) 2018 BianP Co. Ltd. * All right reserved. */ package com.xin.dealcenter.service; import java.util.List; import com.github.pagehelper.PageInfo; import com.item.util.AppPage; import com.xin.dealcenter.entity.Order; /** * * @Description: 訂單——SERVICE * @Author: BianP * @CreateDate: 2018年7月5日 * @Version: V1.0 * */ public interface OrderService { /** * @explain 查詢訂單對象 * @param 對象參數:id * @return Order * @author BianP */ public Order selectByPrimaryKey(Long id); /** * @explain 刪除訂單對象 * @param 對象參數:id * @return int * @author BianP */ public int deleteByPrimaryKey(Long id); /** * @explain 添加訂單對象 * @param 對象參數:Order * @return int * @author BianP */ public int insertSelective(Order order); /** * @explain 修改訂單對象 * @param 對象參數:Order * @return int * @author BianP */ public int updateByPrimaryKeySelective(Order order); /** * @explain 查詢訂單集合 * @param 對象參數:Order * @return List<Order> * @author BianP */ public List<Order> queryOrderList(Order order); /** * @explain 分頁查詢訂單 * @param 對象參數:Order * @return PageInfo<Order> * @author BianP */ public PageInfo<Order> getOrderBySearch(AppPage<Order> page); }
/** * @filename:OrderServiceImpl 2018年7月5日 * @project deal-center V1.0 * Copyright(c) 2018 BianP Co. Ltd. * All right reserved. */ package com.xin.dealcenter.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.item.util.AppPage; import com.xin.dealcenter.entity.Order; import com.xin.dealcenter.dao.OrderDao; import com.xin.dealcenter.service.OrderService; /** * * @Description: 訂單——SERVICEIMPL * @Author: BianP * @CreateDate: 2018年7月5日 * @Version: V1.0 * */ @Service public class OrderServiceImpl implements OrderService { @Autowired public OrderDao orderDao; //查詢對象 @Override public Order selectByPrimaryKey(Long id) { return orderDao.selectByPrimaryKey(id); } //刪除對象 @Override public int deleteByPrimaryKey(Long id) { return orderDao.deleteByPrimaryKey(id); } //添加對象 @Override public int insertSelective(Order order) { return orderDao.insertSelective(order); } //修改對象 @Override public int updateByPrimaryKeySelective(Order order) { return orderDao.updateByPrimaryKeySelective(order); } //查詢集合 @Override public List<Order> queryOrderList(Order order) { return orderDao.queryOrderList(order); } //分頁查詢 @Override public PageInfo<Order> getOrderBySearch(AppPage<Order> page) { // TODO Auto-generated method stub PageHelper.startPage(page.getPageNum(),page.getPageSize()); List<Order> list=orderDao.queryOrderList(page.getParam()); PageInfo<Order> pageInfo = new PageInfo<Order>(list); return pageInfo; } }
/** * @filename:OrderController 2018年7月5日 * @project deal-center V1.0 * Copyright(c) 2018 BianP Co. Ltd. * All right reserved. */ package com.xin.dealcenter.webApi; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.github.pagehelper.PageInfo; import com.item.util.AppPage; import com.item.util.JsonResult; import com.xin.dealcenter.entity.Order; import com.xin.dealcenter.service.OrderService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; /** * * @Description: 訂單接口層 * @Author: BianP * @CreateDate: 2018年7月5日 * @Version: V1.0 * */ @Api(description = "訂單",value="訂單" ) @RestController @RequestMapping("/order") public class OrderController { Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired public OrderService orderServiceImpl; /** * @explain 查詢訂單對象 <swagger GET請求> * @param 對象參數:id * @return order * @author BianP * @time 2018年7月5日 */ @GetMapping("/getOrderById/{id}") @ApiOperation(value = "獲取訂單信息", notes = "獲取訂單信息[order],做者:BianP") @ApiImplicitParam(paramType="path", name = "id", value = "訂單id", required = true, dataType = "Long") public JsonResult getOrderById(@PathVariable("id")Long id){ JsonResult result=new JsonResult(); try { Order order=orderServiceImpl.selectByPrimaryKey(id); if (order!=null) { result.setCode(1); result.setMessage("成功"); result.setData(order); } else { logger.error("獲取訂單失敗ID:"+id); result.setCode(-1); result.setMessage("你獲取的訂單不存在"); } } catch (Exception e) { logger.error("獲取訂單執行異常:"+e.getMessage()); result.setCode(-1); result.setMessage("執行異常,請稍後重試"); } return result; } /** * @explain 添加訂單對象 * @param 對象參數:order * @return int * @author BianP * @time 2018年7月5日 */ @PostMapping("/insertSelective") @ApiOperation(value = "添加訂單", notes = "添加訂單[order],做者:BianP") public JsonResult insertSelective(Order order){ JsonResult result=new JsonResult(); try { int rg=orderServiceImpl.insertSelective(order); if (rg>0) { result.setCode(1); result.setMessage("成功"); result.setData(order); } else { logger.error("添加訂單執行失敗:"+order.toString()); result.setCode(-1); result.setMessage("執行失敗,請稍後重試"); } } catch (Exception e) { logger.error("添加訂單執行異常:"+e.getMessage()); result.setCode(-1); result.setMessage("執行異常,請稍後重試"); } return result; } /** * @explain 刪除訂單對象 * @param 對象參數:id * @return int * @author BianP * @time 2018年7月5日 */ @PostMapping("/deleteByPrimaryKey") @ApiOperation(value = "刪除訂單", notes = "刪除訂單,做者:BianP") @ApiImplicitParam(paramType="query", name = "id", value = "訂單id", required = true, dataType = "Long") public JsonResult deleteByPrimaryKey(Long id){ JsonResult result=new JsonResult(); try { int reg=orderServiceImpl.deleteByPrimaryKey(id); if (reg>0) { result.setCode(1); result.setMessage("成功"); result.setData(id); } else { logger.error("刪除訂單失敗ID:"+id); result.setCode(-1); result.setMessage("執行錯誤,請稍後重試"); } } catch (Exception e) { logger.error("刪除訂單執行異常:"+e.getMessage()); result.setCode(-1); result.setMessage("執行異常,請稍後重試"); } return result; } /** * @explain 修改訂單對象 * @param 對象參數:order * @return order * @author BianP * @time 2018年7月5日 */ @ApiOperation(value = "修改訂單", notes = "修改訂單[order],做者:BianP") @PostMapping("/updateByPrimaryKeySelective") public JsonResult updateByPrimaryKeySelective(Order order){ JsonResult result=new JsonResult(); try { int reg = orderServiceImpl.updateByPrimaryKeySelective(order); if (reg>0) { result.setCode(1); result.setMessage("成功"); result.setData(order); } else { logger.error("修改訂單失敗ID:"+order.toString()); result.setCode(-1); result.setMessage("執行錯誤,請稍後重試"); } } catch (Exception e) { logger.error("修改訂單執行異常:"+e.getMessage()); result.setCode(-1); result.setMessage("執行異常,請稍後重試"); } return result; } /** * @explain 獲取匹配訂單 * @param 對象參數:order * @return List<Order> * @author BianP * @time 2018年7月5日 */ @ApiOperation(value = "條件查詢訂單", notes = "條件查詢[order],做者:BianP") @PostMapping("/queryOrderList") public JsonResult queryOrderList(Order order){ JsonResult result=new JsonResult(); try { List<Order> list = orderServiceImpl.queryOrderList(order); result.setCode(1); result.setMessage("成功"); result.setData(list); } catch (Exception e) { logger.error("獲取訂單執行異常:"+e.getMessage()); result.setCode(-1); result.setMessage("執行異常,請稍後重試"); } return result; } /** * @explain 分頁條件查詢訂單 * @param 對象參數:AppPage<Order> * @return PageInfo<Order> * @author BianP * @time 2018年7月5日 */ @GetMapping("/getPageOrder") @ApiOperation(value = "分頁查詢", notes = "分頁查詢返回對象[PageInfo<Order>],做者:邊鵬") @ApiImplicitParams({ @ApiImplicitParam(paramType="query", name = "pageNum", value = "當前頁", required = true, dataType = "int"), @ApiImplicitParam(paramType="query", name = "pageSize", value = "頁行數", required = true, dataType = "int") }) public JsonResult getOrderBySearch(Integer pageNum,Integer pageSize){ JsonResult result=new JsonResult(); AppPage<Order> page =new AppPage<Order>(); page.setPageNum(pageNum); page.setPageSize(pageSize); //其餘參數 Order order=new Order(); page.setParam(order); //分頁數據 try { PageInfo<Order> pageInfo = orderServiceImpl.getOrderBySearch(page); result.setCode(1); result.setMessage("成功"); result.setData(pageInfo); } catch (Exception e) { logger.error("分頁查詢訂單執行異常:"+e.getMessage()); result.setCode(-1); result.setMessage("執行異常,請稍後重試"); } return result; } }
看到這裏,你們應該能看出,這個代碼生成只適合一些特定的項目,首先是springboot,其次是使用的mybatis和pagehelper,並使用了swagger與lombok。web