前言
经过半个月的时间,终于把我自己写的java代码生成工具,录制成了视频教程。视频地址
接下来我们简单介绍下生成的项目结构.
视频教程
地址:https://www.bilibili.com/video/BV1EN4y1c7sL/
表结构
CREATE TABLE `tb_product_info` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`company_id` varchar(30) DEFAULT NULL COMMENT '公司ID',
`code` varchar(11) DEFAULT NULL COMMENT '商品编号',
`product_name` varchar(200) DEFAULT NULL COMMENT '商品名称',
`price` decimal(15,2) DEFAULT NULL COMMENT '价格',
`sku_type` tinyint(4) DEFAULT NULL COMMENT 'sku类型',
`color_type` tinyint(4) DEFAULT NULL COMMENT '颜色类型',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`create_date` date DEFAULT NULL COMMENT '创建日期',
`stock` bigint(20) DEFAULT NULL COMMENT '库存',
`status` tinyint(4) DEFAULT NULL COMMENT '状态:0 未上架 1:已上架',
`is_del` tinyint(4) DEFAULT NULL COMMENT '0:删除 1:正常',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_code` (`code`) USING BTREE,
UNIQUE KEY `idx_sku_color` (`sku_type`,`color_type`),
KEY `idx_name` (`product_name`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8 COMMENT='商品信息';
这个表有 一个自增ID,一个唯一索引 code,还有一个 联合唯一索引,我们会根据这些关键的表信息生成相关的方法
生成Java代码
项目结构
生成的controller
@RestController
@RequestMapping("/productInfo")
public class ProductInfoController extends ABaseController {
@Resource
private ProductInfoService productInfoService;
@RequestMapping("loadDataList")
public ResponseVO loadDataList(ProductInfoQuery query) {
return getSuccessResponseVO(productInfoService.findListByPage(query));
}
/**
* 新增
*/
@RequestMapping("add")
public ResponseVO add(ProductInfo bean) {
this.productInfoService.add(bean);
return getSuccessResponseVO(null);
}
/**
* 批量新增
*/
@RequestMapping("addBatch")
public ResponseVO addBatch(@RequestBody List<ProductInfo> listBean) {
this.productInfoService.addBatch(listBean);
return getSuccessResponseVO(null);
}
/**
* 批量新增或修改
*/
@RequestMapping("addOrUpdateBatch")
public ResponseVO addOrUpdateBatch(@RequestBody List<ProductInfo> listBean) {
this.productInfoService.addOrUpdateBatch(listBean);
return getSuccessResponseVO(null);
}
/**
* 根据Id查询
*/
@RequestMapping("getProductInfoById")
public ResponseVO getProductInfoById(Integer id) {
return getSuccessResponseVO(this.productInfoService.getProductInfoById(id));
}
/**
* 根据Id更新
*/
@RequestMapping("updateProductInfoById")
public ResponseVO updateProductInfoById(ProductInfo bean, Integer id) {
this.productInfoService.updateProductInfoById(bean,id);
return getSuccessResponseVO(null);
}
/**
* 根据Id删除
*/
@RequestMapping("deleteProductInfoById")
public ResponseVO deleteProductInfoById(Integer id) {
this.productInfoService.deleteProductInfoById(id);
return getSuccessResponseVO(null);
}
/**
* 根据Code查询
*/
@RequestMapping("getProductInfoByCode")
public ResponseVO getProductInfoByCode(String code) {
return getSuccessResponseVO(this.productInfoService.getProductInfoByCode(code));
}
/**
* 根据Code更新
*/
@RequestMapping("updateProductInfoByCode")
public ResponseVO updateProductInfoByCode(ProductInfo bean, String code) {
this.productInfoService.updateProductInfoByCode(bean,code);
return getSuccessResponseVO(null);
}
/**
* 根据Code删除
*/
@RequestMapping("deleteProductInfoByCode")
public ResponseVO deleteProductInfoByCode(String code) {
this.productInfoService.deleteProductInfoByCode(code);
return getSuccessResponseVO(null);
}
/**
* 根据SkuTypeAndColorType查询
*/
@RequestMapping("getProductInfoBySkuTypeAndColorType")
public ResponseVO getProductInfoBySkuTypeAndColorType(Integer skuType, Integer colorType) {
return getSuccessResponseVO(this.productInfoService.getProductInfoBySkuTypeAndColorType(skuType, colorType));
}
/**
* 根据SkuTypeAndColorType更新
*/
@RequestMapping("updateProductInfoBySkuTypeAndColorType")
public ResponseVO updateProductInfoBySkuTypeAndColorType(ProductInfo bean, Integer skuType, Integer colorType) {
this.productInfoService.updateProductInfoBySkuTypeAndColorType(bean,skuType, colorType);
return getSuccessResponseVO(null);
}
/**
* 根据SkuTypeAndColorType删除
*/
@RequestMapping("deleteProductInfoBySkuTypeAndColorType")
public ResponseVO deleteProductInfoBySkuTypeAndColorType(Integer skuType, Integer colorType) {
this.productInfoService.deleteProductInfoBySkuTypeAndColorType(skuType, colorType);
return getSuccessResponseVO(null);
}
}
我就不把所有类都列举出来了,重点看下mapper.xml
Mapper.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.easyjava.mappers.ProductInfoMapper">
<!--实体映射-->
<resultMap id="base_result_map" type="com.easyjava.entity.po.ProductInfo">
<!-- 自增ID-->
<id column="id" property="id"/>
<!-- 公司ID-->
<result column="company_id" property="companyId"/>
<!-- 商品编号-->
<result column="code" property="code"/>
<!-- 商品名称-->
<result column="product_name" property="productName"/>
<!-- 价格-->
<result column="price" property="price"/>
<!-- sku类型-->
<result column="sku_type" property="skuType"/>
<!-- 颜色类型-->
<result column="color_type" property="colorType"/>
<!-- 创建时间-->
<result column="create_time" property="createTime"/>
<!-- 创建日期-->
<result column="create_date" property="createDate"/>
<!-- 库存-->
<result column="stock" property="stock"/>
<!-- 状态:0 未上架 1:已上架-->
<result column="status" property="status"/>
<!-- 0:删除 1:正常-->
<result column="is_del" property="isDel"/>
</resultMap>
<!--通用查询结果列-->
<sql id="base_column_list">
id,company_id,code,product_name,price,sku_type,color_type,create_time,create_date,stock,status,is_del
</sql>
<!--基础查询条件-->
<sql id="base_query_condition">
<if test="query.id != null">
and id = #{query.id}
</if>
<if test="query.companyId != null and query.companyId!=''">
and id = #{query.companyId}
</if>
<if test="query.code != null and query.code!=''">
and id = #{query.code}
</if>
<if test="query.productName != null and query.productName!=''">
and id = #{query.productName}
</if>
<if test="query.price != null">
and id = #{query.price}
</if>
<if test="query.skuType != null">
and id = #{query.skuType}
</if>
<if test="query.colorType != null">
and id = #{query.colorType}
</if>
<if test="query.createTime != null">
and id = #{query.createTime}
</if>
<if test="query.createDate != null">
and id = #{query.createDate}
</if>
<if test="query.stock != null">
and id = #{query.stock}
</if>
<if test="query.status != null">
and id = #{query.status}
</if>
<if test="query.isDel != null">
and id = #{query.isDel}
</if>
</sql>
<!--扩展的查询条件-->
<sql id="base_query_condition_extend">
<if test="query.companyIdFuzzy != null and query.companyIdFuzzy !=''">
and company_id like concat('%', #{query.companyIdFuzzy}, '%')
</if>
<if test="query.codeFuzzy != null and query.codeFuzzy !=''">
and code like concat('%', #{query.codeFuzzy}, '%')
</if>
<if test="query.productNameFuzzy != null and query.productNameFuzzy !=''">
and product_name like concat('%', #{query.productNameFuzzy}, '%')
</if>
<if test="query.createTimeStart != null and query.createTimeStart !=''">
<![CDATA[ and create_time >= str_to_date(#{query.createTimeStart}, '%Y-%m-%d') ]]>
</if>
<if test="query.createTimeEnd != null and query.createTimeEnd !=''">
<![CDATA[ and create_time < date_sub(str_to_date(#{query.createTimeEnd},'%Y-%m-%d'),interval -1 day) ]]>
</if>
<if test="query.createDateStart != null and query.createDateStart !=''">
<![CDATA[ and create_date >= str_to_date(#{query.createDateStart}, '%Y-%m-%d') ]]>
</if>
<if test="query.createDateEnd != null and query.createDateEnd !=''">
<![CDATA[ and create_date < date_sub(str_to_date(#{query.createDateEnd},'%Y-%m-%d'),interval -1 day) ]]>
</if>
</sql>
<!--扩展的查询条件-->
<sql id="query_condition">
<where>
<include refid="base_query_condition"/>
<include refid="base_query_condition_extend"/>
</where>
</sql>
<!--查询列表-->
<select id="selectList" resultMap="base_result_map">
SELECT
<include refid="base_column_list"/>
FROM tb_product_info
<include refid="query_condition"/>
<if test="query.orderBy!=null">order by ${query.orderBy}</if>
<if test="query.simplePage!=null">limit #{query.simplePage.start},#{query.simplePage.end}</if>
</select>
<!--查询数量-->
<select id="selectCount" resultType="java.lang.Integer">
SELECT count(1) FROM tb_product_info
<include refid="query_condition"/>
</select>
<!--插入 (匹配有值的字段)-->
<insert id="insert" parameterType="com.easyjava.entity.po.ProductInfo">
<selectKey keyProperty="bean.id" resultType="Integer" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO tb_product_info
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="bean.id != null">
id,
</if>
<if test="bean.companyId != null">
company_id,
</if>
<if test="bean.code != null">
code,
</if>
<if test="bean.productName != null">
product_name,
</if>
<if test="bean.price != null">
price,
</if>
<if test="bean.skuType != null">
sku_type,
</if>
<if test="bean.colorType != null">
color_type,
</if>
<if test="bean.createTime != null">
create_time,
</if>
<if test="bean.createDate != null">
create_date,
</if>
<if test="bean.stock != null">
stock,
</if>
<if test="bean.status != null">
status,
</if>
<if test="bean.isDel != null">
is_del,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="bean.id!=null">
#{bean.id},
</if>
<if test="bean.companyId!=null">
#{bean.companyId},
</if>
<if test="bean.code!=null">
#{bean.code},
</if>
<if test="bean.productName!=null">
#{bean.productName},
</if>
<if test="bean.price!=null">
#{bean.price},
</if>
<if test="bean.skuType!=null">
#{bean.skuType},
</if>
<if test="bean.colorType!=null">
#{bean.colorType},
</if>
<if test="bean.createTime!=null">
#{bean.createTime},
</if>
<if test="bean.createDate!=null">
#{bean.createDate},
</if>
<if test="bean.stock!=null">
#{bean.stock},
</if>
<if test="bean.status!=null">
#{bean.status},
</if>
<if test="bean.isDel!=null">
#{bean.isDel},
</if>
</trim>
</insert>
<!-- 插入或者更新 (匹配有值的字段)-->
<insert id="insertOrUpdate" parameterType="com.easyjava.entity.po.ProductInfo">
INSERT INTO tb_product_info
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="bean.id != null">
id,
</if>
<if test="bean.companyId != null">
company_id,
</if>
<if test="bean.code != null">
code,
</if>
<if test="bean.productName != null">
product_name,
</if>
<if test="bean.price != null">
price,
</if>
<if test="bean.skuType != null">
sku_type,
</if>
<if test="bean.colorType != null">
color_type,
</if>
<if test="bean.createTime != null">
create_time,
</if>
<if test="bean.createDate != null">
create_date,
</if>
<if test="bean.stock != null">
stock,
</if>
<if test="bean.status != null">
status,
</if>
<if test="bean.isDel != null">
is_del,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="bean.id!=null">
#{bean.id},
</if>
<if test="bean.companyId!=null">
#{bean.companyId},
</if>
<if test="bean.code!=null">
#{bean.code},
</if>
<if test="bean.productName!=null">
#{bean.productName},
</if>
<if test="bean.price!=null">
#{bean.price},
</if>
<if test="bean.skuType!=null">
#{bean.skuType},
</if>
<if test="bean.colorType!=null">
#{bean.colorType},
</if>
<if test="bean.createTime!=null">
#{bean.createTime},
</if>
<if test="bean.createDate!=null">
#{bean.createDate},
</if>
<if test="bean.stock!=null">
#{bean.stock},
</if>
<if test="bean.status!=null">
#{bean.status},
</if>
<if test="bean.isDel!=null">
#{bean.isDel},
</if>
</trim>
on DUPLICATE key update
<trim prefix="" suffix="" suffixOverrides=",">
<if test="bean.companyId!=null">
company_id = VALUES(company_id),
</if>
<if test="bean.productName!=null">
product_name = VALUES(product_name),
</if>
<if test="bean.price!=null">
price = VALUES(price),
</if>
<if test="bean.createTime!=null">
create_time = VALUES(create_time),
</if>
<if test="bean.createDate!=null">
create_date = VALUES(create_date),
</if>
<if test="bean.stock!=null">
stock = VALUES(stock),
</if>
<if test="bean.status!=null">
status = VALUES(status),
</if>
<if test="bean.isDel!=null">
is_del = VALUES(is_del),
</if>
</trim>
</insert>
<!-- 添加 (批量插入)-->
<insert id="insertBatch" parameterType="com.easyjava.entity.po.ProductInfo">
INSERT INTO tb_product_info(
company_id,
code,
product_name,
price,
sku_type,
color_type,
create_time,
create_date,
stock,
status,
is_del
)
values
<foreach collection="list" item="item" separator=",">
(
#{item.companyId},
#{item.code},
#{item.productName},
#{item.price},
#{item.skuType},
#{item.colorType},
#{item.createTime},
#{item.createDate},
#{item.stock},
#{item.status},
#{item.isDel}
)
</foreach>
</insert>
<!-- 批量插入或更新 (批量插入)-->
<insert id="insertOrUpdateBatch" parameterType="com.easyjava.entity.po.ProductInfo">
INSERT INTO tb_product_info(
company_id,
code,
product_name,
price,
sku_type,
color_type,
create_time,
create_date,
stock,
status,
is_del
)
values
<foreach collection="list" item="item" separator=",">
(
#{item.companyId},
#{item.code},
#{item.productName},
#{item.price},
#{item.skuType},
#{item.colorType},
#{item.createTime},
#{item.createDate},
#{item.stock},
#{item.status},
#{item.isDel}
)
</foreach>
on DUPLICATE key update
id = VALUES(id),
company_id = VALUES(company_id),
code = VALUES(code),
product_name = VALUES(product_name),
price = VALUES(price),
sku_type = VALUES(sku_type),
color_type = VALUES(color_type),
create_time = VALUES(create_time),
create_date = VALUES(create_date),
stock = VALUES(stock),
status = VALUES(status),
is_del = VALUES(is_del)
</insert>
<!-- 根据Id查询-->
<select id="selectById" resultMap="base_result_map">
select
<include refid="base_column_list"/>
from tb_product_info where id=#{id}
</select>
<!-- 根据Id更新-->
<update id="updateById" parameterType="com.easyjava.entity.po.ProductInfo">
update tb_product_info
<set>
<if test="bean.id!= null">
id = #{bean.id},
</if>
<if test="bean.companyId!= null">
company_id = #{bean.companyId},
</if>
<if test="bean.code!= null">
code = #{bean.code},
</if>
<if test="bean.productName!= null">
product_name = #{bean.productName},
</if>
<if test="bean.price!= null">
price = #{bean.price},
</if>
<if test="bean.skuType!= null">
sku_type = #{bean.skuType},
</if>
<if test="bean.colorType!= null">
color_type = #{bean.colorType},
</if>
<if test="bean.createTime!= null">
create_time = #{bean.createTime},
</if>
<if test="bean.createDate!= null">
create_date = #{bean.createDate},
</if>
<if test="bean.stock!= null">
stock = #{bean.stock},
</if>
<if test="bean.status!= null">
status = #{bean.status},
</if>
<if test="bean.isDel!= null">
is_del = #{bean.isDel},
</if>
</set>
where id=#{id}
</update>
<!-- 根据Id删除-->
<delete id="deleteById">
delete from tb_product_info where id=#{id}
</delete>
<!-- 根据Code查询-->
<select id="selectByCode" resultMap="base_result_map">
select
<include refid="base_column_list"/>
from tb_product_info where code=#{code}
</select>
<!-- 根据Code更新-->
<update id="updateByCode" parameterType="com.easyjava.entity.po.ProductInfo">
update tb_product_info
<set>
<if test="bean.id!= null">
id = #{bean.id},
</if>
<if test="bean.companyId!= null">
company_id = #{bean.companyId},
</if>
<if test="bean.code!= null">
code = #{bean.code},
</if>
<if test="bean.productName!= null">
product_name = #{bean.productName},
</if>
<if test="bean.price!= null">
price = #{bean.price},
</if>
<if test="bean.skuType!= null">
sku_type = #{bean.skuType},
</if>
<if test="bean.colorType!= null">
color_type = #{bean.colorType},
</if>
<if test="bean.createTime!= null">
create_time = #{bean.createTime},
</if>
<if test="bean.createDate!= null">
create_date = #{bean.createDate},
</if>
<if test="bean.stock!= null">
stock = #{bean.stock},
</if>
<if test="bean.status!= null">
status = #{bean.status},
</if>
<if test="bean.isDel!= null">
is_del = #{bean.isDel},
</if>
</set>
where code=#{code}
</update>
<!-- 根据Code删除-->
<delete id="deleteByCode">
delete from tb_product_info where code=#{code}
</delete>
<!-- 根据SkuTypeAndColorType查询-->
<select id="selectBySkuTypeAndColorType" resultMap="base_result_map">
select
<include refid="base_column_list"/>
from tb_product_info where sku_type=#{skuType} and color_type=#{colorType}
</select>
<!-- 根据SkuTypeAndColorType更新-->
<update id="updateBySkuTypeAndColorType" parameterType="com.easyjava.entity.po.ProductInfo">
update tb_product_info
<set>
<if test="bean.id!= null">
id = #{bean.id},
</if>
<if test="bean.companyId!= null">
company_id = #{bean.companyId},
</if>
<if test="bean.code!= null">
code = #{bean.code},
</if>
<if test="bean.productName!= null">
product_name = #{bean.productName},
</if>
<if test="bean.price!= null">
price = #{bean.price},
</if>
<if test="bean.skuType!= null">
sku_type = #{bean.skuType},
</if>
<if test="bean.colorType!= null">
color_type = #{bean.colorType},
</if>
<if test="bean.createTime!= null">
create_time = #{bean.createTime},
</if>
<if test="bean.createDate!= null">
create_date = #{bean.createDate},
</if>
<if test="bean.stock!= null">
stock = #{bean.stock},
</if>
<if test="bean.status!= null">
status = #{bean.status},
</if>
<if test="bean.isDel!= null">
is_del = #{bean.isDel},
</if>
</set>
where sku_type=#{skuType} and color_type=#{colorType}
</update>
<!-- 根据SkuTypeAndColorType删除-->
<delete id="deleteBySkuTypeAndColorType">
delete from tb_product_info where sku_type=#{skuType} and color_type=#{colorType}
</delete>
</mapper>
可以生成 基本的 分页查询、单条插入、单条插入或者更新、批量插入、批量插入或者更新、根据主键、唯一索引 单挑查询、修改、删除
具体有哪些功能,大家看视频吧,视频第一张有演示项目功能,如果你厌倦了CRUD,那就快学起来吧