controller.java.vm
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Controller"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/controller"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}controller;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import com.liberty.androidtools.server.result.ServiceResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表控制层
*
* @author $!author
* @since $!time.currTime()
*/
@Api(tags = "$!{tableInfo.comment}")
@RestController
@RequestMapping("$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
@Autowired
$!{tableInfo.name}Service service;
/**
* 新增记录
*
* @param entity 数据实体
* @return 新增记录
*/
@ApiOperation(value = "新增记录", notes = "新增记录")
@ApiOperationSupport(order = 1, author = "FXR")
@PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
public ServiceResult<$!{tableInfo.name}> add(@RequestBody @ApiParam("json格式对象") $!{tableInfo.name} entity) {
service.save(entity);
return ServiceResult.success(entity);
}
/**
* 删除记录
*
* @param ids ids
* @return 删除结果
*/
@ApiOperation(value = "删除记录", notes = "删除记录")
@ApiOperationSupport(order = 2, author = "FXR")
@DeleteMapping(value = "/{ids}", produces = MediaType.APPLICATION_JSON_VALUE)
public ServiceResult<Boolean> delete(@PathVariable("ids") @ApiParam(value = "删除的id", required = true) String... ids) {
return ServiceResult.success(service.removeByIds(Arrays.asList(ids)));
}
/**
* 修改记录
*
* @param entity 数据实体
* @return 新增结果
*/
@ApiOperation(value = "修改记录", notes = "修改记录")
@ApiOperationSupport(order = 3, author = "FXR")
@PutMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
public ServiceResult<Boolean> update(@RequestBody @ApiParam(value = "json格式对象", required = true) $!{tableInfo.name} entity) {
return ServiceResult.success(service.updateById(entity));
}
/**
* 分页查询
*
* @param pageQuery
* @param query
* @return
*/
@ApiOperation(value = "分页查询", notes = "分页查询")
@ApiOperationSupport(order = 4, author = "FXR")
@GetMapping(value = "page", produces = MediaType.APPLICATION_JSON_VALUE)
public ServiceResult<Page<$!{tableInfo.name}>> page(@ApiParam("分页查询的分页数据模型") Page<$!{tableInfo.name}> pageQuery,@ApiParam("json格式对象") $!{tableInfo.name} query) {
return ServiceResult.success(service.page(pageQuery, Wrappers.lambdaQuery(query).orderByDesc($!{tableInfo.name}::getCreateTime)));
}
/**
* 查询列表
*
* @param query 查询搜索条件
* @return 搜索结果集
*/
@ApiOperation(value = "查询列表", notes = "查询列表")
@ApiOperationSupport(order = 5, author = "FXR")
@GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
public ServiceResult<List<$!{tableInfo.name}>> list(@ApiParam("查询的搜索条件") $!{tableInfo.name} query) {
return ServiceResult.success(service.list(Wrappers.lambdaQuery(query).orderByDesc($!{tableInfo.name}::getCreateTime)));
}
/**
* 查询详情
*
* @param id id
* @return 查询结果
*/
@ApiOperation(value = "查询详情", notes = "查询详情")
@ApiOperationSupport(order = 6, author = "FXR")
@GetMapping(value = "/info/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ServiceResult<$!{tableInfo.name}> info(@PathVariable("id") @ApiParam(value = "查询的ID", required = true) String id) {
return ServiceResult.success(service.getById(id));
}
/**
* 根据id集合查询
*
* @param ids
* @return
*/
@ApiOperation(value = "根据id集合查询", notes = "根据id集合查询")
@ApiOperationSupport(order = 7, author = "FXR")
@GetMapping(value = "/listByIds/{ids}", produces = MediaType.APPLICATION_JSON_VALUE)
public ServiceResult<List<$!{tableInfo.name}>> listByIds(@PathVariable("ids") @ApiParam(value = "查询的ID", required = true) String... ids) {
return ServiceResult.success(service.listByIds(Arrays.asList(ids)));
}
}
dao.java.vm
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Dao"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/dao"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层
*
* @author $!author
* @since $!time.currTime()
*/
@Mapper
public interface $!{tableName} extends BaseMapper<$!{tableInfo.name}> {
}
entity.java.vm
##引入宏定义
$!{define.vm}
##使用宏定义设置回调(保存位置与文件后缀)
#save("/entity", ".java")
##使用宏定义设置包后缀
#setPackageSuffix("entity")
##使用全局变量实现默认包导入
$!{autoImport.vm}
import java.io.Serializable;
##使用全局变量实现默认包导入
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.liberty.androidtools.server.modules.base.AbstractEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;
$!autoImport
import java.io.Serializable;
##使用宏定义实现类注释信息
#tableComment("实体类")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@TableName("$!tableInfo.obj.name")
public class $!{tableInfo.name} extends AbstractEntity implements Serializable {
private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)
#if(${column.obj.name} != "ID")
#if(${column.obj.name} != "CREATE_BY")
#if(${column.obj.name} != "CREATE_TIME")
#if(${column.obj.name} != "UPDATE_BY")
#if(${column.obj.name} != "UPDATE_TIME")
#if(${column.obj.name} != "DEL_FLAG")
#if(${column.obj.name} != "REMARK")
#if(${column.comment})/**
* ${column.comment}
*/#end
@ApiModelProperty(value = "${column.comment}")
@TableField(value = "${column.obj.name}")
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
#end
#end
#end
#end
#end
#end
#end
}
mapper.xml.vm
##引入mybatis支持
$!{mybatisSupport.vm}
##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Dao.xml"))
$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
<?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="$!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao">
<resultMap type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" id="$!{tableInfo.name}Map">
#foreach($column in $tableInfo.fullColumn)
<result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/>
#end
</resultMap>
<!--查询单个-->
<select id="queryById" resultMap="$!{tableInfo.name}Map">
select
#allSqlColumn()
from $!tableInfo.obj.name
where $!pk.obj.name = #{$!pk.name}
</select>
<!--查询指定行数据-->
<select id="queryAllByLimit" resultMap="$!{tableInfo.name}Map">
select
#allSqlColumn()
from $!tableInfo.obj.name
<where>
#foreach($column in $tableInfo.fullColumn)
<if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
and $!column.obj.name = #{$!column.name}
</if>
#end
</where>
limit #{pageable.offset}, #{pageable.pageSize}
</select>
<!--统计总行数-->
<select id="count" resultType="java.lang.Long">
select count(1)
from $!tableInfo.obj.name
<where>
#foreach($column in $tableInfo.fullColumn)
<if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
and $!column.obj.name = #{$!column.name}
</if>
#end
</where>
</select>
<!--新增所有列-->
<insert id="insert" keyProperty="$!pk.name" useGeneratedKeys="true">
insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
values (#foreach($column in $tableInfo.otherColumn)#{$!{column.name}}#if($velocityHasNext), #end#end)
</insert>
<insert id="insertBatch" keyProperty="$!pk.name" useGeneratedKeys="true">
insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
values
<foreach collection="entities" item="entity" separator=",">
(#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)
</foreach>
</insert>
<insert id="insertOrUpdateBatch" keyProperty="$!pk.name" useGeneratedKeys="true">
insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
values
<foreach collection="entities" item="entity" separator=",">
(#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)
</foreach>
on duplicate key update
#foreach($column in $tableInfo.otherColumn)$!column.obj.name = values($!column.obj.name)#if($velocityHasNext),
#end#end
</insert>
<!--通过主键修改数据-->
<update id="update">
update $!{tableInfo.obj.name}
<set>
#foreach($column in $tableInfo.otherColumn)
<if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
$!column.obj.name = #{$!column.name},
</if>
#end
</set>
where $!pk.obj.name = #{$!pk.name}
</update>
<!--通过主键删除-->
<delete id="deleteById">
delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name}
</delete>
</mapper>
service.java.vm
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Service"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service;
import com.baomidou.mybatisplus.extension.service.IService;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服务接口
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableName} extends IService<$!{tableInfo.name}> {
}
serviceImpl.java.vm
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服务实现类
*
* @author $!author
* @since $!time.currTime()
*/
@Service
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Dao, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
}
controller模板修改:
##导入宏定义
$!{define.vm}
##设置表后缀(宏定义)
#setTableSuffix("Controller")
##保存文件(宏定义)
#save("/controller", "Controller.java")
##包路径(宏定义)
#setPackageSuffix("controller")
##定义服务名
#set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service"))
##定义实体对象名
#set($entityName = $!tool.firstLowerCase($!tableInfo.name))
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.web.bind.annotation.*;
import com.hongma.code_common.base.ResponseUtil;
import com.hongma.code_common.base.BaseResponse;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.Serializable;
import java.util.Arrays;
##表注释(宏定义)
#tableComment("表控制层")
@Api(value = " ", tags = {" "})
@RestController
@RequestMapping("$!tool.firstLowerCase($!tableInfo.name)")
public class $!{tableName}{
/**
* 服务对象
*/
@Autowired
private $!{tableInfo.name}Service $!{serviceName};
/**
* 分页查询所有数据
*
* @param page 分页对象
* @param $!entityName 查询实体
* @return 所有数据
*/
@GetMapping
@ApiOperation(value = "查询列表", notes = "查询列表")
public BaseResponse<Page<$!{tableInfo.name}>> selectAll(Page<$!tableInfo.name> page, $!tableInfo.name $!entityName) {
return ResponseUtil.success(this.$!{serviceName}.page(page, new QueryWrapper<>($!entityName)));
}
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("{id}")
@ApiOperation(value = "查询单个", notes = "查询单个")
public BaseResponse<$!{tableInfo.name}> selectOne(@PathVariable Serializable id) {
return ResponseUtil.success(this.$!{serviceName}.getById(id));
}
/**
* 新增数据
*
* @param $!entityName 实体对象
* @return 新增结果
*/
@PostMapping
@ApiOperation(value = "新增数据", notes = "新增数据")
public BaseResponse<Boolean> insert(@RequestBody $!tableInfo.name $!entityName) {
return ResponseUtil.success(this.$!{serviceName}.save($!entityName));
}
/**
* 修改数据
*
* @param $!entityName 实体对象
* @return 修改结果
*/
@PutMapping
@ApiOperation(value = "修改数据", notes = "修改数据")
public BaseResponse<Boolean> update(@RequestBody $!tableInfo.name $!entityName) {
return ResponseUtil.success(this.$!{serviceName}.updateById($!entityName));
}
/**
* 删除数据
*
* @param idList 主键结合
* @return 删除结果
*/
@DeleteMapping
@ApiOperation(value = "删除数据", notes = "删除数据")
public BaseResponse<Boolean> delete(@RequestParam("idList") Long... idList) {
return ResponseUtil.success(this.$!{serviceName}.removeByIds(Arrays.asList(idList)));
}
}