controller.java.vm

##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Controller"))
##定义固定路径变量
#set($baseProjectImportPath = $tool.append("com.xxx.common.core",".web")) 
##设置回调
$!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 $!{baseProjectImportPath}.controller.BaseController;
import $!{baseProjectImportPath}.domain.AjaxResult;
import $!{baseProjectImportPath}.page.TableDataInfo;
//
import $!{tableInfo.savePackageName}.domain.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.I$!{tableInfo.name}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * $!{tableInfo.comment}($!{tableInfo.name})表控制层
 *
 * @author $!author
 * @since $!time.currTime("yyyy-MM-dd")
 */
@RestController
@RequestMapping("$!tool.firstLowerCase($tableInfo.name)")
@Api(value = "$!{tableInfo.comment}", tags = {"$!{tableInfo.comment}"})
public class $!{tableName}   extends BaseController {

    @Autowired
    private I$!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
	
	/**
     * 查询$!{tableInfo.comment}列表
     */
    // @PreAuthorize(hasPermi = "$!tool.firstLowerCase($tableInfo.name):list")
    @ApiOperation(value = "查询$!{tableInfo.comment}列表")
    @GetMapping("/list")
    public TableDataInfo list($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name))
    {
        startPage();
        List<$!{tableInfo.name}> list = $!{tool.firstLowerCase($tableInfo.name)}Service.select$!{tableInfo.name}List($!tool.firstLowerCase($tableInfo.name));
        return getDataTable(list);
    }

     /**
     * 通过主键查询$!{tableInfo.comment}单个
     */
    //@PreAuthorize(hasPermi = "$!tool.firstLowerCase($tableInfo.name):add")
    @ApiOperation(value = "单个查询$!{tableInfo.comment}")
    @GetMapping("/one/{id}")
    public AjaxResult oneById(    @PathVariable("id") $!pk.shortType id){    
        return AjaxResult.success($!tool.firstLowerCase($tableInfo.name)Service. select$!{tableInfo.name}OneById(id));
    }
    
     /**
     * count$!{tableInfo.comment}列表
     */
    // @PreAuthorize(hasPermi = "$!tool.firstLowerCase($tableInfo.name):count")
    @ApiOperation(value = "count$!{tableInfo.comment}列表")
    @GetMapping("/count")
    public AjaxResult count($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)){
        return AjaxResult.success($!tool.firstLowerCase($tableInfo.name)Service. select$!{tableInfo.name}Count( $!tool.firstLowerCase($tableInfo.name)));
    }
	
	    
     /**
     * 单个条件查询$!{tableInfo.comment}列表
     */
    // @PreAuthorize(hasPermi = "$!tool.firstLowerCase($tableInfo.name):one")
    @ApiOperation(value = "单个条件查询$!{tableInfo.comment}对象")
    @GetMapping("/detail/one")
    public AjaxResult detailOne($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)){
        return AjaxResult.success($!tool.firstLowerCase($tableInfo.name)Service. select$!{tableInfo.name}One( $!tool.firstLowerCase($tableInfo.name)));
    }
    
        
     /**
     * 新增$!{tableInfo.comment}
     */
    //@PreAuthorize(hasPermi = "$!tool.firstLowerCase($tableInfo.name):add")
    @ApiOperation(value = "新增$!{tableInfo.comment}")
    //@Log(title = "新增$!{tableInfo.comment}", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody $!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)){
        return toAjax($!tool.firstLowerCase($tableInfo.name)Service. insert$!{tableInfo.name}($!tool.firstLowerCase($tableInfo.name)));
    }
    
    /**
     * 修改$!{tableInfo.comment}
     */
    //@PreAuthorize(hasPermi = "$!tool.firstLowerCase($tableInfo.name):edit")
    @ApiOperation(value = "修改$!{tableInfo.comment}")
    //@Log(title = "修改$!{tableInfo.comment}", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody $!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)){
        return toAjax($!tool.firstLowerCase($tableInfo.name)Service. update$!{tableInfo.name}($!tool.firstLowerCase($tableInfo.name)));
    }
    
     /**
     * 通过主键删除$!{tableInfo.comment}单个
     */
    //@PreAuthorize(hasPermi = "$!tool.firstLowerCase($tableInfo.name):remove")
    //@Log(title = "单个删除$!{tableInfo.comment}", businessType = BusinessType.DELETE)
    @ApiOperation(value = "单个删除$!{tableInfo.comment}")
    @DeleteMapping("/remove/one/{$!pk.name}")
    public AjaxResult removeOneById(    @PathVariable("$!pk.name") $!pk.shortType $!pk.name){    
        return AjaxResult.success($!tool.firstLowerCase($tableInfo.name)Service. delete$!{tableInfo.name}OneById($!pk.name));
    }
    
    
     /**
     * 通过主键批量删除$!{tableInfo.comment}
     */
    //@PreAuthorize(hasPermi = "$!tool.firstLowerCase($tableInfo.name):remove")
    //@Log(title = "批量删除$!{tableInfo.comment}", businessType = BusinessType.DELETE)
    @ApiOperation(value = "批量删除$!{tableInfo.comment}")
    @DeleteMapping("/remove/batch/{$tool.append($!pk.name,'s')}")
    public AjaxResult batchRemove(    @PathVariable("$tool.append($!pk.name,'s')") $!pk.shortType$tool.append('[',']') $tool.append($!pk.name,'s')){    
        return AjaxResult.success($!tool.firstLowerCase($tableInfo.name)Service. batchRemove$!{tableInfo.name}($tool.append($!pk.name,'s')));
    }
}

domain.java.vm

##引入宏定义
$!{define.vm}

##使用宏定义设置回调(保存位置与文件后缀)
#save("/domain", ".java")

##使用宏定义设置包后缀
#setPackageSuffix("domain")

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

##使用全局变量实现默认包导入
$!{autoImport.vm}
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

##使用宏定义实现类注释信息
#tableComment("实体类")
@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@ApiModel(value = "$!{tableInfo.comment}-实体类", description = "$!{tableInfo.comment}-实体类")
public class $!{tableInfo.name} implements Serializable {
     private static final long serialVersionUID = $!tool.serial();
    
#foreach($column in $tableInfo.fullColumn)
     #if(${column.comment})/** ${column.comment}  */#end 
     
     @ApiModelProperty(value = "$!{column.comment}", name = "$!{column.name}")
#if(${column.type}=="java.util.Date")
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
    #end
#if(${column.type}!="java.util.Date")
     private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
    #end

#end

}

mapper.java.vm

##导入宏定义
$!{define.vm}

##设置表后缀(宏定义)
#setTableSuffix("Mapper")

##保存文件(宏定义)
#save("/mapper", "Mapper.java")

##包路径(宏定义)
#setPackageSuffix("mapper")

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

import $!{tableInfo.savePackageName}.domain.$!tableInfo.name;
import java.util.List;
import org.apache.ibatis.annotations.Param;

##表注释(宏定义)
#tableComment("表数据库访问层")

public interface $!{tableName}  {
     /**
     * 查询$!{tableInfo.comment}列表
     */
    List<$!{tableInfo.name}> select$!{tableInfo.name}List($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name));
     /**
     * 通过id单个查询$!{tableInfo.comment}
     */    
    $!{tableInfo.name} select$!{tableInfo.name}OneById($!pk.shortType $!pk.name);
     /**
     * 条件单个查询$!{tableInfo.comment}
     */    
    $!{tableInfo.name} select$!{tableInfo.name}One($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name));
     /**
     * 单个新增$!{tableInfo.comment}
     */                
    int insert$!{tableInfo.name}  ($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name));      
     /**
     * 单个更新$!{tableInfo.comment}
     */    
    int update$!{tableInfo.name}  ($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name));
     /**
     * 条件数据统计$!{tableInfo.comment}
     */    
    Long select$!{tableInfo.name}Count($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name));
     /**
     * 通过id单个删除$!{tableInfo.comment}
     */     
    int delete$!{tableInfo.name}ById($!pk.shortType $!pk.name);
     /**
     * 通过id批量删除$!{tableInfo.comment}
     */    
    int batchRemove$!{tableInfo.name}( $!pk.shortType$tool.append('[',']') $tool.append($!pk.name,'s'));
        /**
     * 批量新增数据(MyBatis原生foreach方法)
     *
     * @param entities List<$!{tableInfo.name}> 实例对象列表
     * @return 影响行数
     */
    int batchInsert(@Param("entities") List<$!{tableInfo.name}> entities);
}

service.java.vm

##导入宏定义
$!{define.vm}

##设置表后缀(宏定义)
#setTableSuffix("Service")

##保存文件(宏定义)
#saveI("/service", "Service.java")

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

##包路径(宏定义)
#setPackageSuffix("service")
import java.util.List;
import $!{tableInfo.savePackageName}.domain.$!tableInfo.name;

##表注释(宏定义)
#tableComment("表服务接口")
public interface I$!{tableName} {
     /**
     * 查询$!{tableInfo.comment}列表
     */
    List<$!{tableInfo.name}> select$!{tableInfo.name}List($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name));
     /**
     * 通过id单个查询$!{tableInfo.comment}
     */     
    $!{tableInfo.name} select$!{tableInfo.name}OneById($!pk.shortType $!pk.name);
     /**
     * 条件单个查询$!{tableInfo.comment}
     */    
    $!{tableInfo.name} select$!{tableInfo.name}One($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name));
     /**
     * 单个新增$!{tableInfo.comment}
     */    
    int insert$!{tableInfo.name}  ($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name));
     /**
     * 单个更新$!{tableInfo.comment}
     */
    int update$!{tableInfo.name}  ($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name));
     /**
     * 条件数据统计$!{tableInfo.comment}
     */      
    Long select$!{tableInfo.name}Count($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name));
     /**
     * 通过id单个删除$!{tableInfo.comment}
     */     
    int delete$!{tableInfo.name}OneById($!pk.shortType $!pk.name);
     /**
     * 通过id批量删除$!{tableInfo.comment}
     */    
    int batchRemove$!{tableInfo.name}( $!pk.shortType$tool.append('[',']') $tool.append($!pk.name,'s'));
}

serviceImpl.java.vm

##导入宏定义
$!{define.vm}

##设置表后缀(宏定义)
#setTableSuffix("ServiceImpl")

##保存文件(宏定义)
#save("/service/impl", "ServiceImpl.java")

##包路径(宏定义)
#setPackageSuffix("service.impl")

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.domain.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.I$!{tableInfo.name}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

##表注释(宏定义)
#tableComment("表服务实现类")
@Service
public class $!{tableName} implements I$!{tableInfo.name}Service {

    @Autowired
    private $!{tableInfo.name}Mapper $!tool.firstLowerCase($tableInfo.name)Mapper;
    
     /**
     * 查询$!{tableInfo.comment}列表
     */
    @Override
    public List<$!{tableInfo.name}> select$!{tableInfo.name}List($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)) {
        return $!tool.firstLowerCase($tableInfo.name)Mapper. select$!{tableInfo.name}List($!tool.firstLowerCase($tableInfo.name));
    }
    
     /**
     * 通过id单个查询$!{tableInfo.comment}
     */ 
    @Override
    public $!{tableInfo.name} select$!{tableInfo.name}OneById($!pk.shortType $!pk.name) {
        return $!tool.firstLowerCase($tableInfo.name)Mapper. select$!{tableInfo.name}OneById( $!pk.name);
    }
    
     /**
     * 单个条件查询$!{tableInfo.comment}列表
     */        
    @Override
    public $!{tableInfo.name} select$!{tableInfo.name}One($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)) {
        return $!tool.firstLowerCase($tableInfo.name)Mapper. select$!{tableInfo.name}One( $!tool.firstLowerCase($tableInfo.name));
    }
         
     /**
     * 新增$!{tableInfo.comment}
     */    
    @Override
    public int insert$!{tableInfo.name}($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)) {
        return $!tool.firstLowerCase($tableInfo.name)Mapper. insert$!{tableInfo.name}($!tool.firstLowerCase($tableInfo.name));  
    }
    
    /**
     * 修改$!{tableInfo.comment}
     */        
    @Override
    public int update$!{tableInfo.name}($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)) {
        return $!tool.firstLowerCase($tableInfo.name)Mapper. update$!{tableInfo.name}($!tool.firstLowerCase($tableInfo.name));  
    }
    
     /**
     * count$!{tableInfo.comment}列表
     */    
    @Override
    public Long select$!{tableInfo.name}Count($!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name)) {
        return $!tool.firstLowerCase($tableInfo.name)Mapper. select$!{tableInfo.name}Count($!tool.firstLowerCase($tableInfo.name));  
    }
    
    
     /**
     * 通过id单个删除$!{tableInfo.comment}
     */ 
    @Override
    public int delete$!{tableInfo.name}OneById($!pk.shortType $!pk.name) {
        return $!tool.firstLowerCase($tableInfo.name)Mapper. delete$!{tableInfo.name}ById( $!pk.name);
    }
        
     /**
     * 通过id批量删除$!{tableInfo.comment}
     */  
    @Override
    public int batchRemove$!{tableInfo.name}( $!pk.shortType$tool.append('[',']') $tool.append($!pk.name,'s')) {
        return $!tool.firstLowerCase($tableInfo.name)Mapper. batchRemove$!{tableInfo.name}( $tool.append($!pk.name,'s'));
    }

}

mapper.xml.vm

##引入mybatis支持
$!{mybatisSupport.vm}


##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml"))
$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper/$tool.firstLowerCase($!{tableInfo.name})"))


##拿到主键
#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}.mapper.$!{tableInfo.name}Mapper">

    <resultMap type="$!{tableInfo.savePackageName}.domain.$!{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="select$!{tableInfo.name}List" resultMap="$!{tableInfo.name}Map">
        select  #allSqlColumn() from $!{tableInfo.obj.getName()} 
         <where>
#foreach($column in $tableInfo.fullColumn)
#if($column.comment.endsWith("LIKE")) 
            <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">and $!column.obj.name like concat('%', #{$!column.name}, '%') </if>
#else
            <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">and $!column.obj.name = #{$!column.name} </if>
#end            
#end
        </where>
       </select>
    
       <insert id="insert$!{tableInfo.name}" >
        insert into $!{tableInfo.obj.name}  
          <trim prefix="(" suffix=")" suffixOverrides=",">
#foreach($column in $tableInfo.fullColumn)
            <if test="$!{column.name} != null">$!column.obj.name,</if>        
#end  
          </trim>
         values           
          <trim prefix="(" suffix=")" suffixOverrides=",">
#foreach($column in $tableInfo.fullColumn)
            <if test="$!{column.name} != null">#{$!{column.name}},</if>
#end 
          </trim>
       </insert>

       <update id="update$!{tableInfo.name}" >
                update $!{tableInfo.obj.name}
            <trim prefix="SET" suffixOverrides=",">
#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
            </trim>
        where $!pk.obj.name = #{$!pk.name}
       </update>

    <!--通过id查询单个-->
    <select id="select$!{tableInfo.name}OneById" resultMap="$!{tableInfo.name}Map">
        select #allSqlColumn()
        from $!tableInfo.obj.name
        where $!pk.obj.name = #{$!pk.name}
    </select>
    
     <!--统计总行数-->
    <select id="select$!{tableInfo.name}Count" resultType="java.lang.Long">
        select count(1) from $!tableInfo.obj.name
        <where>
#foreach($column in $tableInfo.fullColumn)
#if($column.comment.endsWith("LIKE")) 
            <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">and $!column.obj.name like concat('%', #{$!column.name}, '%') </if>
#else
            <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">and $!column.obj.name = #{$!column.name} </if>
#end            
#end
        </where>
    </select>
    
     <!--条件查询单个-->
    <select id="select$!{tableInfo.name}One"  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>
    </select>
    
    <!--通过id单个删除-->
     <delete id="delete$!{tableInfo.name}ById" >   delete from $!tableInfo.obj.name   where $!pk.obj.name = #{$!pk.name} </delete>     

    <!--通过id批量删除-->
    <delete id="batchRemove$!{tableInfo.name}">
        delete from $!{tableInfo.obj.name} where $!pk.obj.name in 
        <foreach item="$!pk.name" collection="array" open="(" separator="," close=")">
            #{$!pk.name}
        </foreach> 
    </delete>
    
    
    <insert id="batchInsert" >
        insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.fullColumn)$!column.obj.name#if($velocityHasNext), #end#end)
        values
        <foreach collection="entities" item="entity" separator=",">
        (#foreach($column in $tableInfo.fullColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)
        </foreach>
    </insert>


</mapper>


Logo

快速构建 Web 应用程序

更多推荐