Commit 71af219d by bianpeng

1.0基础提交

parent 0ddd8754
/target/
/.classpath
/.project
/org.eclipse.core.resources.prefs
/org.eclipse.jdt.core.prefs
/org.eclipse.m2e.core.prefs
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.github</groupId>
<artifactId>mybatis-dsc-generator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mybatis-dsc-generator</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<druid.version>1.1.10</druid.version>
<mysql-connector-java.version>5.1.46</mysql-connector-java.version>
<slf4j-api.version>1.7.25</slf4j-api.version>
<lombok.version>1.16.20</lombok.version>
<freemarker.version>2.3.9</freemarker.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api.version}</version>
</dependency>
<dependency>
<groupId>freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
/**
* @filename:BasisInfo 2018年06月06日
* @project mybatis-generator flying-cattle V1.0
* Copyright(c) 2018 BianP Co. Ltd.
* All right reserved.
*/
package com.github.mybatis.entity;
import java.io.Serializable;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
* @Description: BasisInfo 基础信息
* @Author: flying-cattle
* @CreateDate: 2018-06-06
* @Version: v1.0
*
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BasisInfo implements Serializable{
private static final long serialVersionUID = 123123L;
/**
* @Description: 项目名字
*/
private String project;
/**
* @Description: 作者名字
*/
private String author;
/**
* @Description: 版本号
*/
private String version;
/**
* @Description: 数据库连接
*/
private String dbUrl;
/**
* @Description: 数据访问账户
*/
private String dbName;
/**
* @Description: 数据访问密码
*/
private String dbPassword;
/**
* @Description: 数据库名
*/
private String database;
/**
* @Description: 数据库表
*/
private String table;
/**
* @Description: 类名
*/
private String entityName;
/**
* @Description: 类对象
*/
private String objectName;
/**
* @Description: 类说明
*/
private String entityComment;
/**
* @Description: 日期时间
*/
private String createTime;
/**
* @Description: 随机数/sql字段
*/
private String agile;
/**
* @Description: 类路径
*/
private String entityUrl;
/**
* @Description: dao路径
*/
private String daoUrl;
/**
* @Description: xml-mapper路径
*/
private String mapperUrl;
/**
* @Description: service路径
*/
private String serviceUrl;
/**
* @Description: serviceImpl路径
*/
private String serviceImplUrl;
/**
* @Description: Controller路径
*/
private String controllerUrl;
/**
* @Description: 类信息
*/
private List<PropertyInfo> cis;
public BasisInfo(String project, String author, String version, String dbUrl, String dbName, String dbPassword,
String database, String createTime, String agile, String entityUrl, String daoUrl, String mapperUrl,
String serviceUrl, String serviceImplUrl, String controllerUrl) {
super();
this.project = project;
this.author = author;
this.version = version;
this.dbUrl = dbUrl;
this.dbName = dbName;
this.dbPassword = dbPassword;
this.database = database;
this.createTime = createTime;
this.agile = agile;
this.entityUrl = entityUrl;
this.daoUrl = daoUrl;
this.mapperUrl = mapperUrl;
this.serviceUrl = serviceUrl;
this.serviceImplUrl = serviceImplUrl;
this.controllerUrl = controllerUrl;
}
}
/**
* @filename:GeneratorFileUrl 2018年06月06日
* @project mybatis-generator flying-cattle V1.0
* Copyright(c) 2018 BianP Co. Ltd.
* All right reserved.
*/
package com.github.mybatis.entity;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
* @Description: GeneratorFileUrl 生产文件地址
* @Author: flying-cattle
* @CreateDate: 2018-06-06
* @Version: v1.0
*
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GeneratorFileUrl implements Serializable{
private static final long serialVersionUID = 123125L;
/**
* @Description: 实体类路径
*/
private String entityUrl;
/**
* @Description: dao路径
*/
private String daoUrl;
/**
* @Description: mapper配置文件路径
*/
private String mapperUrl;
/**
* @Description: service路径
*/
private String serviceUrl;
/**
* @Description: service实现类路径
*/
private String serviceImplUrl;
/**
* @Description: controller 控制类的
*/
private String controllerUrl;
}
package com.github.mybatis.entity;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class JsonResult implements Serializable{
private static final long serialVersionUID = 123126L;
/**
* @Description: 状态码(大于0成功,小于0失败)
*/
private Integer code;
/**
* @Description: 消息说明
*/
private String message;
/**
* @Description: 返回对象
*/
private Object data;
}
/**
* @filename:ClassInfo 2018年06月06日
* @project mybatis-generator flying-cattle V1.0
* Copyright(c) 2018 BianP Co. Ltd.
* All right reserved.
*/
package com.github.mybatis.entity;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
* @Description: ClassInfo 表基础信息
* @Author: flying-cattle
* @CreateDate: 2018-06-06
* @Version: v1.0
*
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PropertyInfo implements Serializable{
private static final long serialVersionUID = 123124L;
/**
* @Description: 数据库列名
*/
private String column;
/**
* @Description: 数据库类型
*/
private String jdbcType;
/**
* @Description: 数据库字段注释
*/
private String comment;
/**
* @Description: 属性名
*/
private String property;
/**
* @Description: Java类型
*/
private String javaType;
}
package com.github.mybatis.test;
import java.sql.SQLException;
import java.util.Date;
import com.github.mybatis.entity.BasisInfo;
import com.github.mybatis.util.EntityInfoUtil;
import com.github.mybatis.util.Generator;
import com.github.mybatis.util.MySqlToJavaUtil;
public class TestMain {
//基础信息
public static final String PROJECT="deal-center";
public static final String AUTHOR="BianP";
public static final String VERSION="V1.0";
//数据库连接信息
public static final String URL="jdbc:mysql://127.0.0.1:3306/xin?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&useSSL=true";
public static final String NAME="root";
public static final String PASSWORD="123456";
public static final String DATABASE="xin";
//类信息
public static final String TABLE="user";
public static final String CLASSNAME="User";
public static final String CLASSCOMMENT="用户信息";
public static final String TIME="2018年6月30日";
public static final String AGILE=new Date().getTime()+"";
//路径信息
public static final String ENTITY_URL="com.xin.dealcenter.entity";
public static final String DAO_URL="com.xin.dealcenter.dao";
public static final String XML_URL="com.xin.dealcenter.dao.impl";
public static final String SERVICE_URL="com.xin.dealcenter.service";
public static final String SERVICE_IMPL_URL="com.xin.dealcenter.service.impl";
public static final String CONTROLLER_URL="com.xin.dealcenter.webApi";
public static void main(String[] args) {
BasisInfo bi=new BasisInfo(PROJECT, AUTHOR, VERSION, URL, NAME, PASSWORD, DATABASE, TIME, AGILE, ENTITY_URL, DAO_URL, XML_URL, SERVICE_URL, SERVICE_IMPL_URL, CONTROLLER_URL);
bi.setTable(TABLE);
bi.setEntityName(MySqlToJavaUtil.getClassName(TABLE));
bi.setObjectName(MySqlToJavaUtil.changeToJavaFiled(TABLE));
bi.setEntityComment(CLASSCOMMENT);
try {
bi=EntityInfoUtil.getInfo(bi);
String aa1=Generator.createEntity("E:\\test_workspace\\mybatis-generator\\src\\main\\java\\", bi).toString();
String aa2=Generator.createDao("E:\\test_workspace\\mybatis-generator\\src\\main\\java\\", bi).toString();
String aa3=Generator.createDaoImpl("E:\\test_workspace\\mybatis-generator\\src\\main\\java\\", bi).toString();
String aa4=Generator.createService("E:\\test_workspace\\mybatis-generator\\src\\main\\java\\", bi).toString();
String aa5=Generator.createServiceImpl("E:\\test_workspace\\mybatis-generator\\src\\main\\java\\", bi).toString();
String aa6=Generator.createController("E:\\test_workspace\\mybatis-generator\\src\\main\\java\\", bi).toString();
System.out.println(aa1);
System.out.println(aa2);
System.out.println(aa3);
System.out.println(aa4);
System.out.println(aa5);
System.out.println(aa6);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.github.mybatis.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.github.mybatis.entity.BasisInfo;
import com.github.mybatis.entity.PropertyInfo;
public class EntityInfoUtil {
public static BasisInfo getInfo(BasisInfo bi) throws SQLException {
List<PropertyInfo> columns= new ArrayList<PropertyInfo>();
// 创建连接
Connection con = null;
PreparedStatement pstemt = null;
//sql
String sql="select column_name,data_type,column_comment from information_schema.columns where table_schema='"+bi.getDatabase()+"' and table_name='"+bi.getTable()+"'";
try {
con = DriverManager.getConnection(bi.getDbUrl(), bi.getDbName(), bi.getDbPassword());
pstemt = con.prepareStatement(sql);
ResultSet executeQuery = pstemt.executeQuery();
while (executeQuery.next()) {
String column = executeQuery.getString(1);
String jdbcType = executeQuery.getString(2);
String comment = executeQuery.getString(3);
PropertyInfo ci=new PropertyInfo();
ci.setColumn(column);
if (jdbcType.equals("int")) {
ci.setJdbcType("Integer");
}else {
ci.setJdbcType(jdbcType);
}
ci.setComment(comment);
ci.setProperty(MySqlToJavaUtil.changeToJavaFiled(column));
ci.setJavaType(MySqlToJavaUtil.jdbcTypeToJavaType(jdbcType));
columns.add(ci);
}
bi.setCis(columns);
return bi;
} catch (Exception e) {
throw new RuntimeException("自动生成实体类错误:"+e.getMessage());
} finally {
pstemt.close();
con.close();
}
}
public static String getGeneratorFileUrl(String url,String packageUrl,String entityName, String type){
if (type.equals("entity")) {
return url+pageToUrl(packageUrl)+entityName+".java";
}else if(type.equals("dao")) {
return url+pageToUrl(packageUrl)+entityName+"Dao.java";
}else if(type.equals("daoImpl")) {
return url+pageToUrl(packageUrl)+entityName+"Mapper.xml";
}else if(type.equals("service")) {
return url+pageToUrl(packageUrl)+entityName+"Service.java";
}else if(type.equals("serviceImpl")) {
return url+pageToUrl(packageUrl)+entityName+"ServiceImpl.java";
}else if(type.equals("controller")) {
return url+pageToUrl(packageUrl)+entityName+"Controller.java";
}
return null;
}
public static String pageToUrl(String url) {
return url.replace(".", "/")+"/";
}
}
package com.github.mybatis.util;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import com.github.mybatis.entity.BasisInfo;
import com.github.mybatis.entity.JsonResult;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class FreemarkerUtil {
/**
* 创建通过模板生成的文件
*
* @param dataModel
* 数据模型
* @param templateName
* 输出模版
* @param filePath
* 输出文件路径
*/
public static JsonResult createFile(BasisInfo dataModel, String templateName, String filePath) {
JsonResult result=new JsonResult();
FileWriter out = null;
String fileName=dataModel.getEntityName()+messageStr(templateName);
try {
// 通过FreeMarker的Confuguration读取相应的模板文件
Configuration configuration = new Configuration();
// 设置模板路径
configuration.setClassForTemplateLoading(FreemarkerUtil.class, "/freemarker/ftl");
// 设置默认字体
configuration.setDefaultEncoding("utf-8");
// 获取模板
Template template = configuration.getTemplate(templateName);
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if(!file.exists()) {
file.createNewFile();
}else {
result.setCode(-1);
result.setMessage("已经存在"+fileName);
return result;
}
//设置输出流
out = new FileWriter(file);
//模板输出静态文件
template.process(dataModel, out);
result.setCode(1);
result.setMessage("创建"+fileName+"成功");
return result;
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
result.setCode(-1);
result.setMessage("创建"+fileName+"失败");
return result;
}
public static String messageStr(String name) {
if (name.equals("entity.ftl")) {
name=".java";
} else if(name.equals("dao.ftl")){
name="Dao.java";
} else if(name.equals("mapper.ftl")){
name="Mapper.xml";
} else if(name.equals("service.ftl")){
name="Service.java";
} else if(name.equals("serviceImpl.ftl")){
name="ServiceImpl.java";
} else if(name.equals("controller.ftl")){
name="Controller.java";
}
return name;
}
}
package com.github.mybatis.util;
import java.util.List;
import com.github.mybatis.entity.BasisInfo;
import com.github.mybatis.entity.JsonResult;
import com.github.mybatis.entity.PropertyInfo;
public class Generator {
//路径信息
public static final String ENTITY="entity";
public static final String DAO="dao";
public static final String DAO_IMPL="daoImpl";
public static final String SERVICE="service";
public static final String SERVICE_IMPL="serviceImpl";
public static final String CONTROLLER="controller";
/**
* @explain ①创建实体类
* @param 对象参数:url、BasisInfo
* @return JsonResult
* @author BianP
*/
public static JsonResult createEntity(String url,BasisInfo bi) {
String fileUrl= EntityInfoUtil.getGeneratorFileUrl(url, bi.getEntityUrl(), bi.getEntityName(), ENTITY);
return FreemarkerUtil.createFile(bi, "entity.ftl", fileUrl);
}
/**
* @explain ②创建DAO
* @param 对象参数:url、BasisInfo
* @return JsonResult
* @author BianP
*/
public static JsonResult createDao(String url,BasisInfo bi) {
String fileUrl= EntityInfoUtil.getGeneratorFileUrl(url, bi.getDaoUrl(), bi.getEntityName(), DAO);
return FreemarkerUtil.createFile(bi, "dao.ftl", fileUrl);
}
/**
* @explain ③创建mapper配置文件
* @param 对象参数:url、BasisInfo
* @return JsonResult
* @author BianP
*/
public static JsonResult createDaoImpl(String url,BasisInfo bi) {
String fileUrl= EntityInfoUtil.getGeneratorFileUrl(url, bi.getMapperUrl(), bi.getEntityName(), DAO_IMPL);
List<PropertyInfo> list=bi.getCis();
String agile="";
for (PropertyInfo propertyInfo : list) {
agile=agile+propertyInfo.getColumn()+", ";
}
agile=agile.substring(0, agile.length()-2);
bi.setAgile(agile);
return FreemarkerUtil.createFile(bi, "mapper.ftl", fileUrl);
}
/**
* @explain ④创建SERVICE
* @param 对象参数:url、BasisInfo
* @return JsonResult
* @author BianP
*/
public static JsonResult createService(String url,BasisInfo bi) {
String fileUrl= EntityInfoUtil.getGeneratorFileUrl(url, bi.getServiceUrl(), bi.getEntityName(), SERVICE);
return FreemarkerUtil.createFile(bi, "service.ftl", fileUrl);
}
/**
* @explain ⑤创建SERVICE_IMPL
* @param 对象参数:url、BasisInfo
* @return JsonResult
* @author BianP
*/
public static JsonResult createServiceImpl(String url,BasisInfo bi) {
String fileUrl= EntityInfoUtil.getGeneratorFileUrl(url, bi.getServiceImplUrl(), bi.getEntityName(), SERVICE_IMPL);
return FreemarkerUtil.createFile(bi, "serviceImpl.ftl", fileUrl);
}
/**
* @explain ⑥创建CONTROLLER
* @param 对象参数:url、BasisInfo
* @return JsonResult
* @author BianP
*/
public static JsonResult createController(String url,BasisInfo bi) {
String fileUrl= EntityInfoUtil.getGeneratorFileUrl(url, bi.getControllerUrl(), bi.getEntityName(), CONTROLLER);
return FreemarkerUtil.createFile(bi, "controller.ftl", fileUrl);
}
}
package com.github.mybatis.util;
public class MySqlToJavaUtil {
/**
*
* @Title: firstTocapitalLetters
* @Description: TODO(类名首先字母变大写)
* @param table
* @return 设定文件
* @return String 返回类型
* @author BianPeng
* @date 2018年6月30日
*/
public static String getClassName(String table) {
table=changeToJavaFiled(table);
StringBuilder sbuilder = new StringBuilder();
char[] cs = table.toCharArray();
cs[0] -= 32;
sbuilder.append(String.valueOf(cs));
return sbuilder.toString();
}
/**
*
* @Title: changeToJavaFiled
* @Description: TODO(将数据库中带下划线的字段转换为Java常用的驼峰字段)
* @param field
* @return 设定文件
* @return String 返回类型
* @author BianPeng
* @date 2018年6月30日
*/
public static String changeToJavaFiled(String field) {
String[] fields = field.split("_");
StringBuilder sbuilder = new StringBuilder(fields[0]);
for (int i = 1; i < fields.length; i++) {
char[] cs = fields[i].toCharArray();
cs[0] -= 32;
sbuilder.append(String.valueOf(cs));
}
return sbuilder.toString();
}
/**
*
* @Title: sqlTypeToJavaType
* @Description: TODO(功能:感觉数据类型获得Java的数据类型)
* @param sqlType
* @return javaType
* @return String 返回类型
* @author BianPeng
* @date 2018年6月30日
*/
public static String jdbcTypeToJavaType(String sqlType) {
if (sqlType.equalsIgnoreCase("bit")) {
return "Boolean";
} else if (sqlType.equalsIgnoreCase("tinyint")) {
return "Integer";
} else if (sqlType.equalsIgnoreCase("smallint")) {
return "Integer";
} else if (sqlType.equalsIgnoreCase("int")) {
return "Integer";
} else if (sqlType.equalsIgnoreCase("bigint")) {
return "Long";
} else if (sqlType.equalsIgnoreCase("float")) {
return "Float";
} else if (sqlType.equalsIgnoreCase("decimal") || sqlType.equalsIgnoreCase("numeric") || sqlType.equalsIgnoreCase("real") || sqlType.equalsIgnoreCase("money") || sqlType.equalsIgnoreCase("smallmoney")) {
return "Double";
} else if (sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char") || sqlType.equalsIgnoreCase("nvarchar") || sqlType.equalsIgnoreCase("nchar") || sqlType.equalsIgnoreCase("text")) {
return "String";
} else if (sqlType.equalsIgnoreCase("datetime") || sqlType.equalsIgnoreCase("date") || sqlType.equalsIgnoreCase("timestamp")) {
return "Date";
} else if (sqlType.equalsIgnoreCase("image")) {
return "Blod";
}
return null;
}
}
/**
* @filename:${entityName}Controller ${createTime}
* @project ${project} ${version}
* Copyright(c) 2018 ${author} Co. Ltd.
* All right reserved.
*/
package ${controllerUrl};
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 ${entityUrl}.${entityName};
import ${serviceUrl}.${entityName}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
/**
*
* @Description: ${entityComment}接口层
* @Author: ${author}
* @CreateDate: ${createTime}
* @Version: ${version}
*
*/
@Api(description = "${entityComment}",value="${entityComment}" )
@RestController
@RequestMapping("/${objectName}")
public class ${entityName}Controller {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
public ${entityName}Service ${objectName}ServiceImpl;
/**
* @explain 查询${entityComment}对象 <swagger GET请求>
* @param 对象参数:id
* @return ${objectName}
* @author ${author}
* @time ${createTime}
*/
@GetMapping("/get${entityName}ById/{id}")
@ApiOperation(value = "获取${entityComment}信息", notes = "获取${entityComment}信息[${objectName}],作者:${author}")
@ApiImplicitParam(paramType="path", name = "id", value = "${entityComment}id", required = true, dataType = "Long")
public JsonResult get{entityName}ById(@PathVariable("id")Long id){
JsonResult result=new JsonResult();
try {
{entityName} ${objectName}=${objectName}ServiceImpl.selectByPrimaryKey(id);
if (${objectName}!=null) {
result.setCode(1);
result.setMessage("成功");
result.setData(${objectName});
} else {
logger.error("获取${entityComment}失败ID:"+id);
result.setCode(-1);
result.setMessage("你获取的${entityComment}不存在");
}
} catch (Exception e) {
logger.error("获取${entityComment}执行异常:"+e.getMessage());
result.setCode(-1);
result.setMessage("执行异常,请稍后重试");
}
return result;
}
/**
* @explain 添加${entityComment}对象
* @param 对象参数:${objectName}
* @return int
* @author ${author}
* @time ${createTime}
*/
@PostMapping("/insertSelective")
@ApiOperation(value = "添加${entityComment}", notes = "添加${entityComment}[${objectName}],作者:${author}")
public JsonResult insertSelective({entityName} ${objectName}){
JsonResult result=new JsonResult();
try {
int rg=${objectName}ServiceImpl.insertSelective(${objectName});
if (rg>0) {
result.setCode(1);
result.setMessage("成功");
result.setData(${objectName});
} else {
logger.error("添加${entityComment}执行失败:"+${objectName}.toString());
result.setCode(-1);
result.setMessage("执行失败,请稍后重试");
}
} catch (Exception e) {
logger.error("添加${entityComment}执行异常:"+e.getMessage());
result.setCode(-1);
result.setMessage("执行异常,请稍后重试");
}
return result;
}
/**
* @explain 删除${entityComment}对象
* @param 对象参数:id
* @return int
* @author ${author}
* @time ${createTime}
*/
@PostMapping("/deleteByPrimaryKey")
@ApiOperation(value = "删除${entityComment}", notes = "删除${entityComment},作者:${author}")
@ApiImplicitParam(paramType="query", name = "id", value = "${entityComment}id", required = true, dataType = "Long")
public JsonResult deleteByPrimaryKey(Long id){
JsonResult result=new JsonResult();
try {
int reg=${objectName}ServiceImpl.deleteByPrimaryKey(id);
if (reg>0) {
result.setCode(1);
result.setMessage("成功");
result.setData(id);
} else {
logger.error("删除${entityComment}失败ID:"+id);
result.setCode(-1);
result.setMessage("执行错误,请稍后重试");
}
} catch (Exception e) {
logger.error("删除${entityComment}执行异常:"+e.getMessage());
result.setCode(-1);
result.setMessage("执行异常,请稍后重试");
}
return result;
}
/**
* @explain 修改${entityComment}对象
* @param 对象参数:${objectName}
* @return ${objectName}
* @author ${author}
* @time ${createTime}
*/
@ApiOperation(value = "修改${entityComment}", notes = "修改${entityComment}[${objectName}],作者:${author}")
@PostMapping("/updateByPrimaryKeySelective")
public JsonResult updateByPrimaryKeySelective({entityName} ${objectName}){
JsonResult result=new JsonResult();
try {
int reg = ${objectName}ServiceImpl.updateByPrimaryKeySelective(${objectName});
if (reg>0) {
result.setCode(1);
result.setMessage("成功");
result.setData(${objectName});
} else {
logger.error("修改${entityComment}失败ID:"+${objectName}.toString());
result.setCode(-1);
result.setMessage("执行错误,请稍后重试");
}
} catch (Exception e) {
logger.error("修改${entityComment}执行异常:"+e.getMessage());
result.setCode(-1);
result.setMessage("执行异常,请稍后重试");
}
return result;
}
/**
* @explain 获取匹配${entityComment}
* @param 对象参数:${objectName}
* @return List<{entityName}>
* @author ${author}
* @time ${createTime}
*/
@ApiOperation(value = "条件查询${entityComment}", notes = "条件查询[${objectName}],作者:${author}")
@PostMapping("/query{entityName}List")
public JsonResult query{entityName}List({entityName} ${objectName}){
JsonResult result=new JsonResult();
try {
List<{entityName}> list = ${objectName}ServiceImpl.query{entityName}List(${objectName});
result.setCode(1);
result.setMessage("成功");
result.setData(list);
} catch (Exception e) {
logger.error("获取${entityComment}执行异常:"+e.getMessage());
result.setCode(-1);
result.setMessage("执行异常,请稍后重试");
}
return result;
}
/**
* @explain 分页条件查询${entityComment}
* @param 对象参数:AppPage<{entityName}>
* @return PageInfo<{entityName}>
* @author ${author}
* @time ${createTime}
*/
@GetMapping("/getPage{entityName}")
@ApiOperation(value = "分页查询", notes = "分页查询返回对象[PageInfo<{entityName}>],作者:边鹏")
@ApiImplicitParams({
@ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true, dataType = "int"),
@ApiImplicitParam(paramType="query", name = "pageSize", value = "页行数", required = true, dataType = "int")
})
public JsonResult get{entityName}BySearch(Integer pageNum,Integer pageSize){
JsonResult result=new JsonResult();
AppPage<{entityName}> page =new AppPage<{entityName}>();
page.setPageNum(pageNum);
page.setPageSize(pageSize);
//其他参数
{entityName} ${objectName}=new {entityName}();
page.setParam(${objectName});
//分页数据
try {
PageInfo<{entityName}> pageInfo = ${objectName}ServiceImpl.get{entityName}BySearch(page);
result.setCode(1);
result.setMessage("成功");
result.setData(pageInfo);
} catch (Exception e) {
logger.error("分页查询${entityComment}执行异常:"+e.getMessage());
result.setCode(-1);
result.setMessage("执行异常,请稍后重试");
}
return result;
}
}
\ No newline at end of file
/**
* @filename:${entityName}Dao ${createTime}
* @project ${project} ${version}
* Copyright(c) 2018 ${author} Co. Ltd.
* All right reserved.
*/
package ${daoUrl};
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import ${entityUrl}.${entityName};
/**
*
* @Description: ${entityComment}——DAO
* @Author: ${author}
* @CreateDate: ${createTime}
* @Version: ${version}
*
*/
@Mapper
public interface ${entityName}Dao {
public ${entityName} selectByPrimaryKey(Long id);
public int deleteByPrimaryKey(Long id);
public int insertSelective(${entityName} ${objectName});
public int updateByPrimaryKeySelective(${entityName} ${objectName});
public List<${entityName}> query${entityName}List(${entityName} ${objectName});
}
\ No newline at end of file
/**
* @filename:${entityName} ${createTime}
* @project ${project} ${version}
* Copyright(c) 2018 ${author} Co. Ltd.
* All right reserved.
*/
package ${entityUrl};
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: ${entityComment}
* @Author: ${author}
* @CreateDate: ${createTime}
* @Version: ${version}
*
*/
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class ${entityName} implements Serializable {
private static final long serialVersionUID = ${agile}L;
<#list cis as ci>
<#if ci.javaType=="Date">
<#if ci.jdbcType=="date">
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
<#elseif ci.jdbcType=="time">
@DateTimeFormat(pattern = "HH:mm:ss")
@JsonFormat(pattern="HH:mm:ss",timezone = "GMT+8")
<#else>
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
</#if>
</#if>
@ApiModelProperty(name = "${ci.property}" , value = "${ci.comment}")
private ${ci.javaType} ${ci.property};
</#list>
}
\ No newline at end of file
<?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="${daoUrl}.${entityName}Dao">
<resultMap id="BaseResultMap" type="${entityUrl}.${entityName}">
<#list cis as ci>
<id column="${ci.column}" jdbcType="${ci.jdbcType?upper_case}" property="${ci.property}" />
</#list>
</resultMap>
<sql id="Base_Column_List">
${agile}
</sql>
<!-- 查询 -->
<select id="selectByPrimaryKey" parameterType="java.lang.Long"
resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from ${table}
where id = ${r'#{id,jdbcType=BIGINT}'}
</select>
<!-- 删除 -->
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from ${table}
where id = ${r'#{id,jdbcType=BIGINT}'}
</delete>
<!-- 选择添加 -->
<insert id="insertSelective" parameterType="${entityUrl}.${entityName}">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
SELECT
LAST_INSERT_ID()
</selectKey>
insert into ${table}
<trim prefix="(" suffix=")" suffixOverrides=",">
<#list cis as ci>
<if test="${ci.property} != null">
${ci.column},
</if>
</#list>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<#list cis as ci>
<if test="${ci.property} != null">
${r'#{'}${ci.property},jdbcType=${ci.jdbcType?upper_case}},
</if>
</#list>
</trim>
</insert>
<!-- 选择修改 -->
<update id="updateByPrimaryKeySelective" parameterType="${entityUrl}.${entityName}">
update ${table}
<set>
<#list cis as ci>
<if test="${ci.property} != null">
${ci.column} = ${r'#{'}${ci.property},jdbcType=${ci.jdbcType?upper_case}},
</if>
</#list>
</set>
where id = ${r'#{id,jdbcType=BIGINT}'}
</update>
<!-- 组合条件查询 -->
<select id="query${entityName}List" parameterType="${entityUrl}.${entityName}"
resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from ${table}
<where>
<if test="id != null">
id = ${r'#{'}id,jdbcType=BIGINT}
</if>
<#list cis as ci>
<#if ci.column!="id">
<if test="${ci.property} != null">
AND ${ci.column} = ${r'#{'}${ci.property} ,jdbcType=${ci.jdbcType?upper_case}}
</if>
</#if>
</#list>
</where>
</select>
</mapper>
\ No newline at end of file
/**
* @filename:${entityName}Service ${createTime}
* @project ${project} ${version}
* Copyright(c) 2018 ${author} Co. Ltd.
* All right reserved.
*/
package ${serviceUrl};
import java.util.List;
import com.github.pagehelper.PageInfo;
import com.item.util.AppPage;
import ${entityUrl}.${entityName};
/**
*
* @Description: ${entityComment}——SERVICE
* @Author: ${author}
* @CreateDate: ${createTime}
* @Version: ${version}
*
*/
public interface ${entityName}Service {
/**
* @explain 查询${entityComment}对象
* @param 对象参数:id
* @return ${entityName}
* @author ${author}
*/
public ${entityName} selectByPrimaryKey(Long id);
/**
* @explain 删除${entityComment}对象
* @param 对象参数:id
* @return int
* @author ${author}
*/
public int deleteByPrimaryKey(Long id);
/**
* @explain 添加${entityComment}对象
* @param 对象参数:${entityName}
* @return int
* @author ${author}
*/
public int insertSelective(${entityName} ${objectName});
/**
* @explain 修改${entityComment}对象
* @param 对象参数:${entityName}
* @return int
* @author ${author}
*/
public int updateByPrimaryKeySelective(${entityName} ${objectName});
/**
* @explain 查询${entityComment}集合
* @param 对象参数:${entityName}
* @return List<${entityName}>
* @author ${author}
*/
public List<${entityName}> query${entityName}List(${entityName} ${objectName});
/**
* @explain 分页查询${entityComment}
* @param 对象参数:${entityName}
* @return PageInfo<${entityName}>
* @author ${author}
*/
public PageInfo<${entityName}> getUserBySearch(AppPage<User> page);
}
\ No newline at end of file
/**
* @filename:${entityName}ServiceImpl ${createTime}
* @project ${project} ${version}
* Copyright(c) 2018 ${author} Co. Ltd.
* All right reserved.
*/
package ${serviceImplUrl};
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 ${entityUrl}.${entityName};
import ${daoUrl}.${entityName}Dao;
import ${serviceUrl}.${entityName}Service;
/**
*
* @Description: ${entityComment}——SERVICEIMPL
* @Author: ${author}
* @CreateDate: ${createTime}
* @Version: ${version}
*
*/
@Service
public class ${entityName}ServiceImpl implements ${entityName}Service {
@Autowired
public ${entityName}Dao ${objectName}Dao;
//查询对象
@Override
public ${entityName} selectByPrimaryKey(Long id) {
return ${objectName}Dao.selectByPrimaryKey(id);
}
//删除对象
@Override
public int deleteByPrimaryKey(Long id) {
return ${objectName}Dao.deleteByPrimaryKey(id);
}
//添加对象
@Override
public int insertSelective(${entityName} ${objectName}) {
return ${objectName}Dao.insertSelective(${objectName});
}
//修改对象
@Override
public int updateByPrimaryKeySelective(${entityName} ${objectName}) {
return ${objectName}Dao.updateByPrimaryKeySelective(${objectName});
}
//查询集合
@Override
public List<${entityName}> query${entityName}List(${entityName} ${objectName}) {
return ${objectName}Dao.query${entityName}List(${objectName});
}
//分页查询
@Override
public PageInfo<${entityName}> get${entityName}BySearch(AppPage<${entityName}> page) {
// TODO Auto-generated method stub
PageHelper.startPage(page.getPageNum(),page.getPageSize());
List<${entityName}> list=${objectName}Dao.query${entityName}List(page.getParam());
PageInfo<${entityName}> pageInfo = new PageInfo<${entityName}>(list);
return pageInfo;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment