springBoot整合velocity实现代码⽣成器思路代码⽣成器,由表数据,表名,列名转换为类名,属性名,⽣成类,此⽣成器只提供⼀个思路,模板依据原理⾃⼰实现。
1,导⼊pom依赖
<!-- 引⼊SpringBoot⽗类依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
bigdecimal转换为integer<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.22.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
<mysql.version>5.1.38</mysql.version>
<druid.version>1.0.28</druid.version>
<commons.lang.version>2.6</commons.lang.version>
<commons.io.version>2.5</commons.io.version>
&figuration.version>1.10</figuration.version>
<fastjson.version>1.2.31</fastjson.version>
<velocity.version>1.7</velocity.version>
</properties>
<dependencies>
<!-- springboot-web组件 springmvc+spring+mybatis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons.lang.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>${figuration.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<artifactId>velocity</artifactId>
<groupId>org.apache.velocity</groupId>
<version>${velocity.version}</version>
</dependency>
</dependencies>
⼆,整理配置⽂件
# Tomcat
server:
tomcat:
max-threads: 10
min-spare-threads: 10
port: 8090
# mysql ⽤户,密码,库⾃⼰修改配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: sql.jdbc.Driver
url: jdbc:mysql://192.168.25.130:3306/chong?useUnicode=true&characterEncoding=UTF-8 username: root
password: xxxxxx
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
resources:
static-locations: classpath:/static/,classpath:/views/
# Mybatis配置
mybatis:
mapperLocations: classpath:mapper/**/*.xml
三 ,添加⽣成器模板配置⽂件
generator.properties
#\u8DEF\u5F84
mainPath=com.liu
#\u6A21\u5757
package=dules
moduleName=log
#\u6CE8\u91CA
author=liuerchong
#Email
email=xxxxxx@xxx
#\u7C7B\u578B\u8F6C\u6362\uFF0C\u914D\u7F6E\u4FE1\u606F
tinyint=Integer
smallint=Integer
mediumint=Integer
int=Integer
integer=Integer
bigint=Long
float=Float
double=Double
decimal=BigDecimal
bit=Boolean
char=String
varchar=String
tinytext=String
text=String
mediumtext=String
longtext=String
date=Date
datetime=Date
timestamp=Date
四,添加模板⽂件 ,此处类具体实现可根据实际⾃⾏处理,⽐如返回值类型ResultInfo Controller.java.vm
package ${package}.controller;
import java.util.List;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.web.bind.annotation.RequestBody;
import ${package}.entity.${className}Entity;
import ${package}.service.${className}Service;
/**
* ${comments}
*
* @author ${author}
*
* @date ${datetime}
*/
@RestController
@RequestMapping("/${moduleName}/${pathName}")
public class ${className}Controller {
@Autowired
private ${className}Service ${classname}Service;
/**
* 列表
*/
@RequestMapping("/list")
@RequiresPermissions("${moduleName}:${pathName}:list")
public ResultInfo list(@RequestParam Map<String, Object> params){
//查询列表数据
Query query = new Query(params);
List<${className}Entity> ${classname}List = ${classname}Service.queryList(query);
int total = ${classname}Service.queryTotal(query);
PageUtils pageUtil = new PageUtils(${classname}List, total, Limit(), Page());
return ResultInfo.ok().put("page", pageUtil);
}
/**
* 信息
*/
@RequestMapping("/info/{${pk.attrname}}")
@RequiresPermissions("${moduleName}:${pathName}:info")
public ResultInfo info(@PathVariable("${pk.attrname}") ${pk.attrType} ${pk.attrname}){
${className}Entity ${classname} = ${classname}Service.queryObject(${pk.attrname});
return ResultInfo.ok().put("${classname}", ${classname});
}
/**
* 保存
*/
@RequestMapping("/save")
@RequiresPermissions("${moduleName}:${pathName}:save")
public ResultInfo save(@RequestBody ${className}Entity ${classname}){ ${classname}Service.save(${classname});
return ResultInfo.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
@RequiresPermissions("${moduleName}:${pathName}:update")
public ResultInfo update(@RequestBody ${className}Entity ${classname}){ ${classname}Service.update(${classname});
return ResultInfo.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
@RequiresPermissions("${moduleName}:${pathName}:delete")
public ResultInfo delete(@RequestBody ${pk.attrType}[] ${pk.attrname}s){ ${classname}Service.deleteBatch(${pk.attrname}s);
return ResultInfo.ok();
}
}
五,实现模板⼯具⽅法
package com.liu.utils;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论