package com.nf147.policy_publishing_platform.util.auto;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author: WBG
* @description: 自动创建service、impl、controller
* @date: 2019/02/18 08:15
*/
public class AutoCreate {
//驱动
static String DBDRIVER = "org.mariadb.jdbc.Driver";
//服务器地址
static String DBURL = "jdbc:mariadb://localhost:3306/policydb";
//登录用户名
static String DBUID = "root";
//密码
static String DBPWD = "123456";
//项目包名
static String Package = "com.nf147.policy_publishing_platform";
private static String tablename;
private String[] colnames; // 列名数组
private String[] colTypes; // 列名类型数组
private int[] colSizes; // 列名大小数组
public static void main(String[] args) throws Exception {
AutoCreate auto = new AutoCreate();
//获取所有数据表
List<String> list = auto.TBlist();
for (String table : list) {
createStart(tables(table));
}
}
/**
* 获取指定数据库中包含的表 TBlist
*
* @return 返回所有表名(将表名放到一个集合中)
* @throws Exception
* @time 2017年7月14日下午5:54:52
* @packageName com.util
*/
public List<String> TBlist() throws Exception {
// 访问数据库 采用 JDBC方式
Class.forName(DBDRIVER);
Connection con = DriverManager.getConnection(DBURL, DBUID, DBPWD);
DatabaseMetaData md = con.getMetaData();
List<String> list = null;
ResultSet rs = md.getTables(null, null, null, null);
if (rs != null) {
list = new ArrayList<String>();
}
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME");
list.add(tableName);
}
rs = null;
md = null;
con = null;
return list;
}
/**
* 把输入字符串的首字母改成大写
*
* @param str
* @return
*/
private static String initcap(String str) {
char[] ch = str.toCharArray();
if (ch[0] >= 'a' && ch[0] <= 'z') {
ch[0] = (char) (ch[0] - 32);
}
return new String(ch);
}
/**
* 把输入字符串的首字母改成小写
*
* @param str
* @return
*/
private static String initlow(String str) {
char[] ch = str.toCharArray();
if (ch[0] >= 'A' && ch[0] <= 'Z') {
ch[0] = (char) (ch[0] + 32);
}
return new String(ch);
}
//首字母转换和下划线转换
private static String tables(String table) {
String[] tables = table.split("_");
table = "";
for (String s : tables) {
table += initcap(s);
}
return table;
}
/**
* 创建Dao
*/
private static String createDao(String tableName) {
String service = "package "+Package+".dao;
" +
"
" +
"import "+Package+".entity."+tableName+";
" +
"import org.springframework.stereotype.Repository;
" +
"
" +
"import java.util.List;
" +
"
" +
"@Repository
" +
"public interface "+tableName+"Mapper {
" +
" /**
" +
" * 根据id删除操作
" +
" *
" +
" * @param id
" +
" * @return
" +
" */
" +
" int deleteByPrimaryKey(Integer id);
" +
"
" +
" /**
" +
" * 添加操作
" +
" *
" +
" * @param "+initlow(tableName)+"
" +
" * @return
" +
" */
" +
" int insert("+tableName+" "+initlow(tableName)+");
" +
"
" +
" /**
" +
" * 根据id查询操作
" +
" *
" +
" * @param id
" +
" * @return
" +
" */
" +
" "+tableName+" selectByPrimaryKey(Integer id);
" +
"
" +
" /**
" +
" * 全部查询操作
" +
" *
" +
" * @return
" +
" */
" +
" List<"+tableName+"> selectAll();
" +
"
" +
" /**
" +
" * 根据id全部修改操作
" +
" *
" +
" * @param "+initlow(tableName)+"
" +
" * @return
" +
" */
" +
" int updateByPrimaryKey("+tableName+" "+initlow(tableName)+");
" +
"}";
return service;
}
/**
* 创建Service
*
* @param tableName 数据库表
*/
private static String createService(String tableName) {
String service = "package "+Package+".service;
" +
"
" +
"import "+Package+".entity." + tableName + ";
" +
"
" +
"import java.util.List;
" +
"
" +
"public interface " + tableName + "Service {
" +
"
" +
" /**
" +
" * 删除操作 根据id
" +
" *
" +
" * @param id
" +
" * @return
" +
" */" +
"
" +
" int deleteByPrimaryKey(Integer id);
" +
"
" +
" /**
" +
" * 添加操作
" +
" *
" +
" * @param " + initlow(tableName) + "
" +
" * @return
" +
" */" +
"
" +
" int insert(" + tableName + " " + initlow(tableName) + ");
" +
"
" +
" /**
" +
" * 根据id查询操作
" +
" *
" +
" * @param id
" +
" * @return
" +
" */" +
"
" +
" " + tableName + " selectByPrimaryKey(Integer id);
" +
"
" +
" /**
" +
" * 全部查询操作
" +
" *
" +
" * @return
" +
" */" +
"
" +
" List<" + tableName + "> selectAll();
" +
"
" +
" /**
" +
" * 修改操作
" +
" *
" +
" * @param " + initlow(tableName) + "
" +
" * @return
" +
" */" +
"
" +
" int updateByPrimaryKey(" + tableName + " " + initlow(tableName) + ");
" +
"}";
return service;
}
/**
* 创建ServiceImpl
*
* @param tableName 数据库表
*/
private static String createServiceImpl(String tableName) {
String serviceImpl = "package "+Package+".impl;
" +
"
" +
"import "+Package+".dao." + tableName + "Mapper;
" +
"import "+Package+".entity." + tableName + ";
" +
"import "+Package+".service." + tableName + "Service;
" +
"import org.springframework.beans.factory.annotation.Autowired;
" +
"import org.springframework.stereotype.Service;
" +
"
" +
"import java.util.List;
" +
"
" +
"@Service
" +
"public class " + tableName + "ServiceImpl implements " + tableName + "Service {
" +
"
" +
" @Autowired
" +
" private " + tableName + "Mapper " + initlow(tableName) + "Mapper;
" +
"
" +
" /**
" +
" * 删除操作 根据id删除
" +
" *
" +
" * @param id
" +
" * @return
" +
" */
" +
" @Override
" +
" public int deleteByPrimaryKey(Integer id) {
" +
" return " + initlow(tableName) + "Mapper.deleteByPrimaryKey(id);
" +
" }
" +
"
" +
" /**
" +
" * 添加操作
" +
" *
" +
" * @param " + initlow(tableName) + "
" +
" * @return
" +
" */
" +
" @Override
" +
" public int insert(" + tableName + " " + initlow(tableName) + ") {
" +
" return " + initlow(tableName) + "Mapper.insert(" + initlow(tableName) + ");
" +
" }
" +
"
" +
" /**
" +
" * 根据id查询操作
" +
" *
" +
" * @param id
" +
" * @return
" +
" */
" +
" @Override
" +
" public " + tableName + " selectByPrimaryKey(Integer id) {
" +
" return " + initlow(tableName) + "Mapper.selectByPrimaryKey(id);
" +
" }
" +
"
" +
" /**
" +
" * 全部查询操作
" +
" *
" +
" * @return
" +
" */
" +
" @Override
" +
" public List<" + tableName + "> selectAll() {
" +
" return " + initlow(tableName) + "Mapper.selectAll();
" +
" }
" +
"
" +
" /**
" +
" * 修改操作
" +
" *
" +
" * @param " + initlow(tableName) + "
" +
" * @return
" +
" */
" +
" @Override
" +
" public int updateByPrimaryKey(" + tableName + " " + initlow(tableName) + ") {
" +
" return " + initlow(tableName) + "Mapper.updateByPrimaryKey(" + initlow(tableName) + ");
" +
" }
" +
"}
";
return serviceImpl;
}
/**
* 创建Controller
*
* @param tableName 数据库表
*/
private static String createController(String tableName) {
String controller = "package "+Package+".controller;
" +
"import "+Package+".entity." + tableName + ";
" +
"import "+Package+".service." + tableName + "Service;
" +
"import "+Package+".util.Result;
" +
"import org.springframework.beans.factory.annotation.Autowired;
" +
"import org.springframework.web.bind.annotation.*;
" +
"import java.util.List;
" +
"
" +
"@RestController
" +
"@RequestMapping("/" + initlow(tableName) + "")
" +
"public class " + tableName + "Controller {
" +
" @Autowired
" +
" private " + tableName + "Service " + initlow(tableName) + "Service;
" +
"
" +
" /**
" +
" * 根据aId删除
" +
" * 要求转入 aId
" +
" *
" +
" * @param " + initlow(tableName) + "
" +
" * @return
" +
" */
" +
" @GetMapping("/deleteByPrimaryKey")
" +
" public Result deleteByPrimaryKey(" + tableName + " " + initlow(tableName) + ") {
" +
" try {
" +
"
" +
" return " + initlow(tableName) + "Service.deleteByPrimaryKey(" + initlow(tableName) + ".getId()) > 0 ? new Result().successMessage("删除成功") : new Result("删除失败");
" +
" } catch (Exception ex) {
" +
" return new Result().error("出错,请重试!");
" +
" }
" +
" }
" +
"
" +
" /**
" +
" * 添加对象" + initlow(tableName) + "
" +
" *
" +
" * @param " + initlow(tableName) + "
" +
" * @return
" +
" */
" +
" @PostMapping("/insert")
" +
" public Result insert(@RequestBody " + tableName + " " + initlow(tableName) + ") {
" +
" try {
" +
" return " + initlow(tableName) + "Service.insert(" + initlow(tableName) + ") > 0 ? new Result().successMessage("添加成功!") : new Result("添加失败!");
" +
" } catch (Exception ex) {
" +
" return new Result().error("出错,请重试!");
" +
" }
" +
"
" +
" }
" +
"
" +
" /**
" +
" * 根据aid查找对象 最多只能返回一个对象
" +
" *
" +
" * @param " + initlow(tableName) + "
" +
" * @return
" +
" */
" +
" @GetMapping("/selectByPrimaryKey")
" +
" public Result selectByPrimaryKey(" + tableName + " " + initlow(tableName) + ") {
" +
" try {
" +
" " + tableName + " " + initlow(tableName) + "1 = " + initlow(tableName) + "Service.selectByPrimaryKey(" + initlow(tableName) + ".getId());
" +
" if (" + initlow(tableName) + "1 == null) {
" +
" return new Result().successMessage("无数据");
" +
" } else {
" +
" return new Result().success(" + initlow(tableName) + "1);
" +
" }
" +
" } catch (Exception ex) {
" +
" return new Result().error("出错,请重试!");
" +
" }
" +
" }
" +
"
" +
" /**
" +
" * 查询所有数据
" +
" *
" +
" * @return
" +
" */
" +
" @GetMapping("/selectAll")
" +
" public Result selectAll() {
" +
" //public Result selectAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
" +
" try {
" +
" //分页
" +
" //PageHelper.startPage(pageNum, pageSize);
" +
" List<" + tableName + "> list = " + initlow(tableName) + "Service.selectAll();
" +
" if (list == null) {
" +
" return new Result().successMessage("无数据");
" +
" } else {
" +
" // return new Result().success(list, " + initlow(tableName) + "Service.count(""));
" +
" return new Result().success(list);
" +
" }
" +
" } catch (Exception ex) {
" +
" return new Result().error("出错,请重试!");
" +
" }
" +
" }
" +
"
" +
" /**
" +
" * 根据id修改全部字段
" +
" *
" +
" * @param " + initlow(tableName) + "
" +
" * @return
" +
" */
" +
" @PostMapping(value = "/updateByPrimaryKey")
" +
" public Result updateByPrimaryKey(@RequestBody " + tableName + " " + initlow(tableName) + ") {
" +
" try {
" +
" return " + initlow(tableName) + "Service.updateByPrimaryKey(" + initlow(tableName) + ") > 0 ? new Result().successMessage("修改成功") : new Result("修改失败");
" +
" } catch (Exception ex) {
" +
" return new Result().error("出错,请重试!");
" +
" }
" +
"
" +
"
" +
" }
" +
"}
";
return controller;
}
/**
* 开始创建
*
* @param tableName 数据库表
*/
static void createStart(String tableName) {
//获取当前项目的路径
String url = System.getProperty("user.dir");
url += "\src\main\java\com\nf147\policy_publishing_platform\";
//创建Dao
createFile(new File(url + "dao\" + tableName + "Mapper.java"), createDao(tableName));
//创建Service
createFile(new File(url + "service\" + tableName + "Service.java"), createService(tableName));
//创建ServiceImpl
createFile(new File(url + "impl\" + tableName + "ServiceImpl.java"), createServiceImpl(tableName));
//创建Controller
createFile(new File(url + "controller\" + tableName + "Controller.java"), createController(tableName));
}
/**
* @param file 创建的文件
* @param context 文件里面的内容
*/
static boolean createFile(File file, String context) {
//获取文件
File parent = file.getParentFile();
//如果是目录
if (parent != null) {
//创建目录
parent.mkdirs();
}
try {
//创建文件
file.createNewFile();
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(file);
fileWriter.write(context);
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
return false;
}
} catch (IOException e) {
System.out.println("创建文件失败:" + e.getMessage());
}
return true;
}
}