由于数据库表较多、逆向工程生成的实体类也比较多,重复性工作就很多也很枯燥,今天把重复部分提取出来,用代码自动生成 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;
@Autowired
public 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 {
@Autowired
private 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()
#repository
def 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))