最近写个java web 程序,用的spring boot、spring data jpa

由于数据库表较多、逆向工程生成的实体类也比较多,重复性工作就很多也很枯燥,今天把重复部分提取出来,用代码自动生成 repository层(dao层)、controller层、service 和 service.impl 中的所有基本文件, 有兴趣的可以加上基本的包装。

意义:让程序猿更专注于写业务逻辑、而不是重复性的工作
效果:

废话不多说,上代码

import os
import time"""
用途:根据实体类,创建最基本的持久层、服务层、服务层实现类 (repository、service、serviceImpl)
作者:刘颜铭
默认编码 utf8
"""packgeName = 'msgc'entityDir = 'G:/files/项目_代码/msgCollect/src/main/java/com/' + packgeName + '/entity/'aimDir = 'F:/autoG/'repositoryText = """package com.--PACKGENAME--.repository;import org.springframework.data.jpa.repository.JpaRepository;import com.--PACKGENAME--.entity.--ENTITYNAME--;
/**
* Description: DAO.
*   Deal directly with the database,
*   if you customize the query, take the index first
*
*       Please follow the prefix convention below
*
*   getOne ------------ getXXX
*   getMultiple ------- listXXX
*   count ------------- countXXX
*   getOne ------------ getXXX
*   insert ------------ saveXXX
*   delete ------------ deleteXXX
*   modify ------------ updateXXX
*
*/
public interface I--ENTITYNAME--Repository extends JpaRepository<--ENTITYNAME--, Integer> {}
"""serviceText = """package com.--PACKGENAME--.service;import com.--PACKGENAME--.entity.--ENTITYNAME--;public interface I--ENTITYNAME--Service {}
"""serviceImplText = """package com.--PACKGENAME--.service.impl;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import com.--PACKGENAME--.entity.--ENTITYNAME--;
import com.--PACKGENAME--.repository.I--ENTITYNAME--Repository;
import com.--PACKGENAME--.service.I--ENTITYNAME--Service;/**
* Type: --ENTITYNAME--ServiceImpl
* Description: serviceImp
* @author LYM*/
@Service
public class --ENTITYNAME--ServiceImpl implements I--ENTITYNAME--Service{private I--ENTITYNAME--Repository --SMALLENTITYNAME--Repository;@Autowiredpublic void set--ENTITYNAME--Repositry(I--ENTITYNAME--Repository --SMALLENTITYNAME--Repositry) {this.--SMALLENTITYNAME--Repository = --SMALLENTITYNAME--Repositry;}}
"""controllerText="""package com.--PACKGENAME--.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;import com.--PACKGENAME--.entity.--ENTITYNAME--;
import com.--PACKGENAME--.service.I--ENTITYNAME--Service;/**
*Type: --ENTITYNAME--Controller
* Description:
* @author LMM*/
@Controller
public class --ENTITYNAME--Controller {@Autowiredprivate I--ENTITYNAME--Service --SMALLENTITYNAME--Service;}
"""def makeSureDir(dir):if not os.path.exists(dir):os.makedirs(dir)makeSureDir(aimDir)def generateRepository(entityName, aimPath):makeSureDir(aimPath)repositoryName = 'I' + entityName + 'Repository'filePath = aimPath + repositoryName + '.java'fileHandle = open (filePath, 'w', encoding='utf-8')text = repositoryText.replace('--PACKGENAME--', packgeName)text = text.replace('--ENTITYNAME--', entityName)fileHandle.write(text)fileHandle.close()#repositorydef generateService(entityName, aimPath):makeSureDir(aimPath)serviceName = 'I' + entityName + 'Service'serviceFilePath = aimPath + serviceName + '.java'fileHandle = open (serviceFilePath, 'w', encoding='utf-8')text = serviceText.replace('--PACKGENAME--', packgeName)text = text.replace('--ENTITYNAME--', entityName)fileHandle.write(text)fileHandle.close()def generateServiceImpl(entityName, aimPath):makeSureDir(aimPath)serviceImplName = entityName + 'ServiceImpl'serviceImplFilePath = aimPath + serviceImplName + '.java'fileHandle = open (serviceImplFilePath, 'w', encoding='utf-8')text = serviceImplText.replace('--PACKGENAME--', packgeName)text = text.replace('--ENTITYNAME--', entityName)text = text.replace('--SMALLENTITYNAME--', entityName.lower())fileHandle.write(text)fileHandle.close()    def generateController(entityName, aimPath):makeSureDir(aimPath)controllerName = entityName + 'Controller'controllerFilePath = aimPath + controllerName + '.java'fileHandle = open (controllerFilePath, 'w', encoding='utf-8')text = controllerText.replace('--PACKGENAME--', packgeName)text = text.replace('--ENTITYNAME--', entityName)text = text.replace('--SMALLENTITYNAME--', entityName.lower())fileHandle.write(text)fileHandle.close()count = 0
list = os.listdir(entityDir) #列出文件夹下所有的目录与文件
time_start=time.time()
for i in range(0,len(list)):path = os.path.join(entityDir,list[i])#找到所有实体类if os.path.isfile(path) and "java" == os.path.basename(path).split(".")[-1]:entityName = os.path.basename(path).split(".")[0]print('emtity--' + entityName + '-- is generating...........')generateRepository(entityName, aimDir + 'repository/')print('finish repository')generateService(entityName, aimDir + 'service/')print('finish service')generateServiceImpl(entityName, aimDir + 'service/impl/')    print('finish serviceImpl')generateController(entityName, aimDir + 'controller/')print('finish controller')count = count + 1
time_end=time.time()
print()
print("-----------------------")
print()
print("完成! 共创建 " + str(count * 4) + ' 个java类。耗时共计 %.3f 秒' % (time_end-time_start))

根据实体类生成持久层、控制层、服务层、实现层相关推荐

  1. c mysql实体类生成工具_【干货分享】C# 实体类生成工具

    前言: 项目实战中不论是业务编码还是通用编码,总会归纳出一些通用的工具类.放入项目中一劳永逸,让兄弟姐妹们避免编写重复代码.所以利用了工作之余的时间,将这些散落在多个项目中精致优雅的工具类,归纳起来形 ...

  2. [开源] FreeSql.AdminLTE.Tools 根据实体类生成后台管理代码

    前言 FreeSql 发布至今已经有9个月,功能渐渐完善,自身的生态也逐步形成,早在几个月前写过一篇文章<ORM 开发环境之利器:MVC 中间件 FreeSql.AdminLTE>,您可以 ...

  3. java实体类生成mysql表_springboot+mybatis通过实体类自动生成数据库表的方法

    前言 本章介绍使用mybatis结合mysql数据库自动根据实体类生成相关的数据库表. 首先引入相关的pom包我这里使用的是springboot2.1.8.RELEASE的版本 org.mybatis ...

  4. 根据java实体类生成创建表sql步骤

    根据java实体类生成创建表sql步骤 根据java实体类生成创建表sql语句时,方法是利用java反射+AOP注解,主要步骤如下: 1.注解类 一般在生成表的时候,需要表名.主键名.字段名,对应到注 ...

  5. Springboot根据实体类生成数据库表

    springboot数据库(一)-springboot-JPA JPA:springboot -jpa:数据库的一系列的定义数据持久化的标准的体系 学习的目的是: 利用springboot实现对数据库 ...

  6. 根据实体类生成数据库表

    参考文档地址:通过实体类生成数据库表_weixin_44571808的博客-CSDN博客_java实体类生成数据库表 注意springboot版本号需要比较低(没有详细测试2.6不行,2.1.11可以 ...

  7. CodeDom六--实体类生成示例

    CodeDom这个东西个人觉得知识点不多,前几个续节写的已差不多了.在这节将演示一个CodeDom示例: 数据库实体类的生成.这里先声明在如今的CodeSmith或者是T4模板中实现这些都很简单,并且 ...

  8. EntityFramework实体框架—反向工程(实体类生成)

    文章目录 前言 1. Entity Framework Core反向工程定义 2.实现工作原理 3.准备工作 4. 编写命令 前言 Entity Framework Core 是适用于 .NET 的新 ...

  9. Spring Boot配置Mysql后无法根据java实体类生成table

    https://www.imooc.com/qadetail/193270?t=292816 检查了两天,没找到问题.原因为实体类必须在主程序application的同名包或者子包下面,启动时才会扫描 ...

最新文章

  1. centos php ioncube_Linux/Centos 安装PHP ioncube扩展
  2. 图表+笔记-python语言-第5章:数字/5.11 位操作
  3. Python高级技巧:用一行代码减少一半内存占用!
  4. 《播客》项目总结——web标准页面设计方面
  5. 软件工程 --第七章 -- 实现(未完)
  6. 察看网页源代码全攻略
  7. SQL获取上个月的第一天最后一天datetime
  8. 自己动手写Docker系列 -- 3.3使用命令管道优化参数传递
  9. 思科五个高危漏洞 CDPwn 影响数千万台企业设备
  10. GNS3中下载路由器
  11. 基于Easyui框架的datagrid绑定数据,新增,修改,删除方法(一)
  12. JS 页面繁简字转换
  13. leetcode每日一题 838. 推多米诺 正解 双指针~这题很考察模拟逻辑——是一道不错的模拟题
  14. mysql duplicateentry_Mysql错误:Duplicate entry 'xxx' for key 'PRIMARY'的解决方法_mysql_爱周末...
  15. Nginx + uWSGI + Python + Django构建必应高清壁纸站
  16. Linux下的FTP安装和登录
  17. 用C语言实现杨辉三角
  18. 矩阵的等价,相似,合同
  19. 清除系统垃圾文件bat
  20. mysql数据库实验查询_MySQL数据库表数据的查询操作实验

热门文章

  1. 状压DP学习总结 (详解,适合没状压dp基础的人学习,还在更新中,,,,)
  2. ios开发 静音键设置_iOS获取设备静音键的开关状态 误差优化
  3. Xcode Command Line Tools命令
  4. 弘辽科技:淘宝智能推广常见问题回答
  5. OFFICE 2016零售版与VISIO 2016批量授权版并存的解决方法
  6. 手机访问服务器网站出现广告,【洗白白】浏览器老是弹出运营商的“小广告”?三招完美解决!...
  7. LeetCode -前缀和专题
  8. ant pattern 语法
  9. @Pattern进行正则校验
  10. java中set的遍历_java中遍历set集合,java中set怎么遍历?