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;\n" +
"\n" +
"import "+Package+".entity."+tableName+";\n" +
"import org.springframework.stereotype.Repository;\n" +
"\n" +
"import java.util.List;\n" +
"\n" +
"@Repository\n" +
"public interface "+tableName+"Mapper {\n" +
" /**\n" +
" * 根据id删除操作\n" +
" *\n" +
" * @param id\n" +
" * @return\n" +
" */\n" +
" int deleteByPrimaryKey(Integer id);\n" +
"\n" +
" /**\n" +
" * 添加操作\n" +
" *\n" +
" * @param "+initlow(tableName)+"\n" +
" * @return\n" +
" */\n" +
" int insert("+tableName+" "+initlow(tableName)+");\n" +
"\n" +
" /**\n" +
" * 根据id查询操作\n" +
" *\n" +
" * @param id\n" +
" * @return\n" +
" */\n" +
" "+tableName+" selectByPrimaryKey(Integer id);\n" +
"\n" +
" /**\n" +
" * 全部查询操作\n" +
" *\n" +
" * @return\n" +
" */\n" +
" List<"+tableName+"> selectAll();\n" +
"\n" +
" /**\n" +
" * 根据id全部修改操作\n" +
" *\n" +
" * @param "+initlow(tableName)+"\n" +
" * @return\n" +
" */\n" +
" int updateByPrimaryKey("+tableName+" "+initlow(tableName)+");\n" +
"}";
return service;
} /**
* 创建Service
*
* @param tableName 数据库表
*/
private static String createService(String tableName) {
String service = "package "+Package+".service;\n" +
"\n" +
"import "+Package+".entity." + tableName + ";\n" +
"\n" +
"import java.util.List;\n" +
"\n" +
"public interface " + tableName + "Service {\n" +
"\n" +
" /**\n" +
" * 删除操作 根据id\n" +
" *\n" +
" * @param id\n" +
" * @return\n" +
" */" +
"\n" +
" int deleteByPrimaryKey(Integer id);\n" +
"\n" +
" /**\n" +
" * 添加操作\n" +
" *\n" +
" * @param " + initlow(tableName) + "\n" +
" * @return\n" +
" */" +
"\n" +
" int insert(" + tableName + " " + initlow(tableName) + ");\n" +
"\n" +
" /**\n" +
" * 根据id查询操作\n" +
" *\n" +
" * @param id\n" +
" * @return\n" +
" */" +
"\n" +
" " + tableName + " selectByPrimaryKey(Integer id);\n" +
"\n" +
" /**\n" +
" * 全部查询操作\n" +
" *\n" +
" * @return\n" +
" */" +
"\n" +
" List<" + tableName + "> selectAll();\n" +
"\n" +
" /**\n" +
" * 修改操作\n" +
" *\n" +
" * @param " + initlow(tableName) + "\n" +
" * @return\n" +
" */" +
"\n" +
" int updateByPrimaryKey(" + tableName + " " + initlow(tableName) + ");\n" +
"}";
return service;
} /**
* 创建ServiceImpl
*
* @param tableName 数据库表
*/
private static String createServiceImpl(String tableName) {
String serviceImpl = "package "+Package+".impl;\n" +
"\n" +
"import "+Package+".dao." + tableName + "Mapper;\n" +
"import "+Package+".entity." + tableName + ";\n" +
"import "+Package+".service." + tableName + "Service;\n" +
"import org.springframework.beans.factory.annotation.Autowired;\n" +
"import org.springframework.stereotype.Service;\n" +
"\n" +
"import java.util.List;\n" +
"\n" +
"@Service\n" +
"public class " + tableName + "ServiceImpl implements " + tableName + "Service {\n" +
"\n" +
" @Autowired\n" +
" private " + tableName + "Mapper " + initlow(tableName) + "Mapper;\n" +
"\n" +
" /**\n" +
" * 删除操作 根据id删除\n" +
" *\n" +
" * @param id\n" +
" * @return\n" +
" */\n" +
" @Override\n" +
" public int deleteByPrimaryKey(Integer id) {\n" +
" return " + initlow(tableName) + "Mapper.deleteByPrimaryKey(id);\n" +
" }\n" +
"\n" +
" /**\n" +
" * 添加操作\n" +
" *\n" +
" * @param " + initlow(tableName) + "\n" +
" * @return\n" +
" */\n" +
" @Override\n" +
" public int insert(" + tableName + " " + initlow(tableName) + ") {\n" +
" return " + initlow(tableName) + "Mapper.insert(" + initlow(tableName) + ");\n" +
" }\n" +
"\n" +
" /**\n" +
" * 根据id查询操作\n" +
" *\n" +
" * @param id\n" +
" * @return\n" +
" */\n" +
" @Override\n" +
" public " + tableName + " selectByPrimaryKey(Integer id) {\n" +
" return " + initlow(tableName) + "Mapper.selectByPrimaryKey(id);\n" +
" }\n" +
"\n" +
" /**\n" +
" * 全部查询操作\n" +
" *\n" +
" * @return\n" +
" */\n" +
" @Override\n" +
" public List<" + tableName + "> selectAll() {\n" +
" return " + initlow(tableName) + "Mapper.selectAll();\n" +
" }\n" +
"\n" +
" /**\n" +
" * 修改操作\n" +
" *\n" +
" * @param " + initlow(tableName) + "\n" +
" * @return\n" +
" */\n" +
" @Override\n" +
" public int updateByPrimaryKey(" + tableName + " " + initlow(tableName) + ") {\n" +
" return " + initlow(tableName) + "Mapper.updateByPrimaryKey(" + initlow(tableName) + ");\n" +
" }\n" +
"}\n";
return serviceImpl;
} /**
* 创建Controller
*
* @param tableName 数据库表
*/
private static String createController(String tableName) {
String controller = "package "+Package+".controller;\n" +
"import "+Package+".entity." + tableName + ";\n" +
"import "+Package+".service." + tableName + "Service;\n" +
"import "+Package+".util.Result;\n" +
"import org.springframework.beans.factory.annotation.Autowired;\n" +
"import org.springframework.web.bind.annotation.*;\n" +
"import java.util.List;\n" +
"\n" +
"@RestController\n" +
"@RequestMapping(\"/" + initlow(tableName) + "\")\n" +
"public class " + tableName + "Controller {\n" +
" @Autowired\n" +
" private " + tableName + "Service " + initlow(tableName) + "Service;\n" +
"\n" +
" /**\n" +
" * 根据aId删除\n" +
" * 要求转入 aId\n" +
" *\n" +
" * @param " + initlow(tableName) + "\n" +
" * @return\n" +
" */\n" +
" @GetMapping(\"/deleteByPrimaryKey\")\n" +
" public Result deleteByPrimaryKey(" + tableName + " " + initlow(tableName) + ") {\n" +
" try {\n" +
"\n" +
" return " + initlow(tableName) + "Service.deleteByPrimaryKey(" + initlow(tableName) + ".getId()) > 0 ? new Result().successMessage(\"删除成功\") : new Result(\"删除失败\");\n" +
" } catch (Exception ex) {\n" +
" return new Result().error(\"出错,请重试!\");\n" +
" }\n" +
" }\n" +
"\n" +
" /**\n" +
" * 添加对象" + initlow(tableName) + "\n" +
" *\n" +
" * @param " + initlow(tableName) + "\n" +
" * @return\n" +
" */\n" +
" @PostMapping(\"/insert\")\n" +
" public Result insert(@RequestBody " + tableName + " " + initlow(tableName) + ") {\n" +
" try {\n" +
" return " + initlow(tableName) + "Service.insert(" + initlow(tableName) + ") > 0 ? new Result().successMessage(\"添加成功!\") : new Result(\"添加失败!\");\n" +
" } catch (Exception ex) {\n" +
" return new Result().error(\"出错,请重试!\");\n" +
" }\n" +
"\n" +
" }\n" +
"\n" +
" /**\n" +
" * 根据aid查找对象 最多只能返回一个对象\n" +
" *\n" +
" * @param " + initlow(tableName) + "\n" +
" * @return\n" +
" */\n" +
" @GetMapping(\"/selectByPrimaryKey\")\n" +
" public Result selectByPrimaryKey(" + tableName + " " + initlow(tableName) + ") {\n" +
" try {\n" +
" " + tableName + " " + initlow(tableName) + "1 = " + initlow(tableName) + "Service.selectByPrimaryKey(" + initlow(tableName) + ".getId());\n" +
" if (" + initlow(tableName) + "1 == null) {\n" +
" return new Result().successMessage(\"无数据\");\n" +
" } else {\n" +
" return new Result().success(" + initlow(tableName) + "1);\n" +
" }\n" +
" } catch (Exception ex) {\n" +
" return new Result().error(\"出错,请重试!\");\n" +
" }\n" +
" }\n" +
"\n" +
" /**\n" +
" * 查询所有数据\n" +
" *\n" +
" * @return\n" +
" */\n" +
" @GetMapping(\"/selectAll\")\n" +
" public Result selectAll() {\n" +
" //public Result selectAll(@RequestParam(defaultValue = \"1\") int pageNum, @RequestParam(defaultValue = \"10\") int pageSize) {\n" +
" try {\n" +
" //分页\n" +
" //PageHelper.startPage(pageNum, pageSize);\n" +
" List<" + tableName + "> list = " + initlow(tableName) + "Service.selectAll();\n" +
" if (list == null) {\n" +
" return new Result().successMessage(\"无数据\");\n" +
" } else {\n" +
" // return new Result().success(list, " + initlow(tableName) + "Service.count(\"\"));\n" +
" return new Result().success(list);\n" +
" }\n" +
" } catch (Exception ex) {\n" +
" return new Result().error(\"出错,请重试!\");\n" +
" }\n" +
" }\n" +
"\n" +
" /**\n" +
" * 根据id修改全部字段\n" +
" *\n" +
" * @param " + initlow(tableName) + "\n" +
" * @return\n" +
" */\n" +
" @PostMapping(value = \"/updateByPrimaryKey\")\n" +
" public Result updateByPrimaryKey(@RequestBody " + tableName + " " + initlow(tableName) + ") {\n" +
" try {\n" +
" return " + initlow(tableName) + "Service.updateByPrimaryKey(" + initlow(tableName) + ") > 0 ? new Result().successMessage(\"修改成功\") : new Result(\"修改失败\");\n" +
" } catch (Exception ex) {\n" +
" return new Result().error(\"出错,请重试!\");\n" +
" }\n" +
"\n" +
"\n" +
" }\n" +
"}\n";
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;
}
}

一键生成 dao service serverImpl controller 层的更多相关文章

  1. DAO(Repository),Service,Controller层之间的相互关系

    DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...

  2. 深入理解--SSM框架中Dao层,Mapper层,controller层,service层,model层,entity层都有什么作用

    SSM是sping+springMVC+mybatis集成的框架. MVC即model view controller. model层=entity层.存放我们的实体类,与数据库中的属性值基本保持一致 ...

  3. DAO层,Service层,Controller层、View层 的分工合作

    DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...

  4. [转]DAO层,Service层,Controller层、View层

    来自:http://jonsion.javaeye.com/blog/592335 DAO层 DAO 层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DA ...

  5. controller层负责创建类传递类给service;service层负责逻辑编写调用dao层 将编写后的类传递到dao层,保证事务的正确性;dao层负责数据的持久化

    controller层负责创建类传递类给service:service层负责逻辑编写调用dao层 将编写后的类传递到dao层,保证事务的正确性:dao层负责数据的持久化

  6. controller层,service层,dao层(main函数,子函数,子的子函数)

    controller层相当于main函数————————————————————————————————————————————————————@RequestMapping("/query ...

  7. DAO层,Service层,Controller层、View层

    DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...

  8. 分层 DAO层,Service层,Controller层、View层

    前部分摘录自:http://blog.csdn.net/zdwzzu2006/article/details/6053006 DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务 ...

  9. DAO层,Service层,Controller层、View层介绍

    来自:http://jonsion.javaeye.com/blog/592335 DAO层 DAO 层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DA ...

随机推荐

  1. Linux 打包QT程序到未安装QT的其他Linux主机下运行

    昨天终于改好了一个开源但是用起来有问题的串口调试助手,想把它打包一下以后在其他电脑上也可以用. 找了网上的一个教程打包后,在本机上可以正常使用,但是移植到另一台上就出现缺少xcb的提示. 上网搜资料倒 ...

  2. Linux系列(1):入门之基本命令详解

    Linux常用命令 声明: 作者使用的Linux是CentOS7版本. 本节主要讲解内容: 1.ls.date.bc.cal等指令的详细用法 2.介绍常用快捷键 3.了解磁盘分区以及挂载等概念 在Li ...

  3. Netty源码之解码中两种数据积累器(Cumulator)的区别

    上一篇随笔中已经介绍了解码核心工作流程,里面有个数据积累器的存在(Cumulator),其实解码中有两种Cumulator,那他们的区别是什么呢? 还是先打开ByteToMessageDecoder的 ...

  4. 进阶Python:装饰器 全面详解

    进阶Python:装饰器 前言 前段时间我发了一篇讲解Python调试工具PySnooper的文章,在那篇文章开始一部分我简单的介绍了一下装饰器,文章发出之后有几位同学说"终于了解装饰器的用 ...

  5. 深入理解计算机系统 第十章 系统级I/O 第二遍

    了解 Unix I/O 的好处 了解 Unix I/O 将帮助我们理解其他的系统概念 I/O 是系统操作不可或缺的一部分,因此,我们经常遇到 I/O 和其他系统概念之间的循环依赖.例如,I/O 在进程 ...

  6. 异常-throws的方式处理异常

    定义功能方法时,需要把出现的问题暴露出来让调用者去处理.那么就通过throws在方法上标识. package cn.itcast_05; import java.text.ParseException ...

  7. 豆瓣网post 爬取带验证码

    # -*- coding: utf- -*- import scrapy import requests from ..bao.jiema import get_number fromdata = { ...

  8. cpu 100%怎样定位

    先用top定位最耗cpu的java进程 例如: 12430工具:top或者 htop(高级)方法:top -c 显示进程运行详细列表键入 P (大写P),按照cpu进行排序 然后用top -p 124 ...

  9. Linux学习笔记:7个ssh命令用法

    通过远程控制管理多台服务器. 远程工具:telnet.ssh.vnc ssh采用密文的传输方式,简单安全.Secure Shell 缩写 SSH. 1.基本用法 ssh 192.168.1.1 默认使 ...

  10. java面试5

    1.如何将String类型转化Number类型?列举说明String str = "123"; Integer  num1 = new  Integer(str); int num ...