(1)模板位置 模板统一放置到相对于当前路径的 模板 文件夹下
(2)自定义数据 自定义的数据以 .propeties 文件(key-value)的形式存放入相对于当前路径的 properties 文件夹下
(3)数据格式
table中数据内容:
制作通用的SpringBoot程序的通用模板
/**
* ${comment!}服务层
* @author ${author!"itcast"}
*/
@Entity
@Table(name="${table.name}")
public class ${ClassName} implements Serializable {
//定义私有属性
<#list table.columns as column>
<#if column.columnKey??>
@Id
</#if>
private ${column.columnType} ${column.columnName2};
</#list>
//处理getter,setter方法
<#list table.columns as column>
public void set${column.columnName2?cap_first}(${column.columnType} value) {
this.${column.columnName2} = value;
}
public ${column.columnType} get${column.columnName2?cap_first}() {
return this.${column.columnName2};
}
</#list> }
package ${pPackage}.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import ${pPackage}.pojo.${ClassName};
/**
* ${comment!}数据访问接口
* @author ${author!"itcast"}
*/
public interface ${ClassName}Dao extends
JpaRepository<${ClassName},String>,JpaSpecificationExecutor<${ClassName}>{
}
<#assign classNameLower = ClassName ? uncap_first>
package ${pPackage}.service;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;
import ${pPackage}.dao.${ClassName}Dao;
import ${pPackage}.pojo.${ClassName};
/**
* ${comment!}服务层
* @author ${author!"itcast"}
*/
@Service
public class ${ClassName}Service {
@Autowired
private ${ClassName}Dao ${classNameLower}Dao;
@Autowired
private IdWorker idWorker;
/**
* 查询全部列表
* @return
*/
public List<${ClassName}> findAll() {
return ${classNameLower}Dao.findAll();
}
/**
* 分页查询
*
* @param page
* @param size
* @return
*/
public Page<${ClassName}> findPage(int page, int size) {
PageRequest pageRequest = PageRequest.of(page-1, size);
return ${classNameLower}Dao.findAll(pageRequest);
}
/**
* 根据ID查询实体
* @param id
* @return
*/
public ${ClassName} findById(String id) {
return ${classNameLower}Dao.findById(id).get();
}
/**
* 增加
* @param ${ClassName}
*/
public void add(${ClassName} ${ClassName}) {
${ClassName}.setId( idWorker.nextId()+"" );
${classNameLower}Dao.save(${ClassName});
}
/**
* 修改
* @param ${ClassName}
*/
public void update(${ClassName} ${ClassName}) {
${classNameLower}Dao.save(${ClassName});
}
/**
* 删除
* @param id
*/
public void deleteById(String id) {
${classNameLower}Dao.deleteById(id);
}
}
<#assign classNameLower = ClassName ? uncap_first>
package ${pPackage}.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import ${pPackage}.pojo.${ClassName};
import ${pPackage}.service.${ClassName}Service;
import entity.PageResult;
import entity.Result;
/**
* [comment]控制器层
* @author Administrator
*
*/
@RestController
@CrossOrigin
@RequestMapping("/${classNameLower}")
public class ${ClassName}Controller {
@Autowired
private ${ClassName}Service ${classNameLower}Service;
/**
* 查询全部数据
* @return
*/
@RequestMapping(method= RequestMethod.GET)
public Result findAll(){
return new Result(ResultCode.SUCCESS,${classNameLower}Service.findAll());
}
/**
* 根据ID查询
* @param id ID
* * @return
*/
@RequestMapping(value="/{id}",method= RequestMethod.GET)
public Result findById(@PathVariable String id){
return new Result(ResultCode.SUCCESS,${classNameLower}Service.findById(id));
}
/**
* 分页查询全部数据
* @param page
* @param size
* @return
*/
@RequestMapping(value="/{page}/{size}",method=RequestMethod.GET)
public Result findPage(@PathVariable int page,@PathVariable int size){
Page<${ClassName}> searchPage = ${classNameLower}Service.findPage(page, size);
PageResult<Role> pr = new
PageResult(searchPage.getTotalElements(),searchPage.getContent());
return new Result(ResultCode.SUCCESS,pr);
}
/**
* 增加
* @param ${classNameLower}
*/
@RequestMapping(method=RequestMethod.POST)
public Result add(@RequestBody ${ClassName} ${classNameLower} ){
${classNameLower}Service.add(${classNameLower});
return new Result(ResultCode.SUCCESS);
}
/**
* 修改
* @param ${classNameLower}
*/
@RequestMapping(value="/{id}",method= RequestMethod.PUT)
public Result update(@RequestBody ${ClassName} ${classNameLower}, @PathVariable String id ){
${classNameLower}.setId(id);
${classNameLower}Service.update(${classNameLower});
return new Result(ResultCode.SUCCESS);
}
/**
* 删除
* @param id
*/
@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
public Result delete(@PathVariable String id ){
${classNameLower}Service.deleteById(id);
return new Result(ResultCode.SUCCESS);
}
}
(1)application.yml
server:
port: 9001
spring:
application:
name: ${project}-${path3} #指定服务名
datasource:
driverClassName: ${driverName}
url: ${url}
username: ${dbuser}
password: ${dbpassword}
jpa:
database: MySQL
show-sql: true
(2)pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>${path_1}.${path2}</groupId>
<artifactId>${project}_parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>${project}_${path3}</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>${path1}.${path2}</groupId>
<artifactId>${project}_common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>