Groovy java idea_idea + groovy + mybatis 自动生成 Dao、mappings 和 实体类

本文介绍了如何在IDEA中结合Groovy和Mybatis,自动生成Dao、mappings和实体类。通过定义类型映射和包名,选择数据库表,可以批量生成对应的Java代码,包括Mapper接口、XML映射文件以及实体类。
摘要由CSDN通过智能技术生成

packagesrcimportcom.intellij.database.model.DasTableimportcom.intellij.database.util.Caseimportcom.intellij.database.util.DasUtil/** Available context bindings:

* SELECTION Iterable

* PROJECT project

* FILES files helper*/

//entity(dto)、mapper(dao) 与数据库表的对应关系在这里手动指明,idea Database 窗口里只能选下列配置了的 mapper//tableName(key) : [mapper(dao),entity(dto)]

typeMapping =[

(~/(?i)int/) : "INTEGER",

(~/(?i)float|double|decimal|real/): "DOUBLE",

(~/(?i)datetime|timestamp/) : "TIMESTAMP",

(~/(?i)date/) : "TIMESTAMP",

(~/(?i)time/) : "TIMESTAMP",

(~/(?i)/) : "VARCHAR"]

basePackage= "com.chitic.bank.mapping" //包名需手动填写

FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->SELECTION.filter { itinstanceofDasTable }.each { generate(it, dir) }

}

def generate(table, dir) {

def baseName= mapperName(table.getName(), true)

def fields=calcFields(table)new File(dir, baseName + "Mapper.xml").withPrintWriter { out ->generate(table, out, baseName, fields) }

}

def generate(table, out, baseName, fields) {

def baseResultMap= 'BaseResultMap'def base_Column_List= 'Base_Column_List'def date= new Date().format("yyyy/MM/dd")

def tableName=table.getName()

def dao= basePackage + ".dao.${baseName}Mapper"def to= basePackage + ".to.${baseName}TO"out.println mappingsStart(dao)

out.println resultMap(baseResultMap, to, fields)

out.println sql(fields, base_Column_List)

out.println selectById(tableName, fields, baseResultMap, base_Column_List)

out.println deleteById(tableName, fields)

out.println delete(tableName, fields, to)

out.println insert(tableName, fields, to)

out.println update(tableName, fields, to)

out.println selectList(tableName, fields, to, base_Column_List, baseResultMap)

out.println mappingsEnd()

}staticdef resultMap(baseResultMap, to, fields) {

def inner= ''fields.each() {

inner+= '\t\t\n'}return '''\t

''' + inner + '''\t

'''}

def calcFields(table) {

DasUtil.getColumns(table).reduce([]) { fields, col->def spec=Case.LOWER.apply(col.getDataType().getSpecification())

def typeStr= typeMapping.find { p, t ->p.matcher(spec).find() }.value

fields+=[[

comment : col.getComment(),

name : mapperName(col.getName(),false),

sqlFieldName: col.getName(),

type : typeStr,

annos :""]]

}

}

def mapperName(str, capitalize) {

def s=com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)

.collect { Case.LOWER.apply(it).capitalize() }

.join("")

.replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")

name= capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1]

}//------------------------------------------------------------------------ mappings

staticdef mappingsStart(mapper) {return '''<?xml version="1.0" encoding="UTF-8"?>

'''}//------------------------------------------------------------------------ mappings

staticdef mappingsEnd() {return ''''''}//------------------------------------------------------------------------ selectById

staticdef selectById(tableName, fields, baseResultMap, base_Column_List) {return ''' selectfrom''' + tableName + '''where id=#{id}'''}//------------------------------------------------------------------------ insert

staticdef insert(tableName, fields, parameterType) {return ''' insert into''' + tableName + '''

''' + testNotNullStr(fields) + '''

''' + testNotNullStrSet(fields) + '''

'''}//------------------------------------------------------------------------ update

staticdef update(tableName, fields, parameterType) {return ''' update''' + tableName + '''

''' + testNotNullStrWhere(fields) + '''

where id=#{id}'''}//------------------------------------------------------------------------ deleteById

staticdef deleteById(tableName, fields) {return ''' delete

from''' + tableName + '''where id=#{id}'''}//------------------------------------------------------------------------ delete

staticdef delete(tableName, fields, parameterType) {return ''' delete from''' + tableName + '''where1 = 1

''' + testNotNullStrWhere(fields) + '''

'''}//------------------------------------------------------------------------ selectList

staticdef selectList(tableName, fields, parameterType, base_Column_List, baseResultMap) {return ''' selectfrom''' + tableName + '''where1 = 1

''' + testNotNullStrWhere(fields) + '''order by id desc'''}//------------------------------------------------------------------------ sql

staticdef sql(fields, base_Column_List) {

def str= '''\t

@inner@ '''def inner= ''fields.each() {

inner+= ('\t\t' + it.sqlFieldName + ',\n')

}return str.replace("@inner@", inner.substring(0, inner.length() - 2))

}staticdef testNotNullStrWhere(fields) {

def inner= ''fields.each {

inner+= ''' and''' + it.sqlFieldName + ''' = #{''' + it.name + '''}\n'''}returninner

}staticdef testNotNullStrSet(fields) {

def inner= ''fields.each {

inner+= ''' #{''' + it.name + '''},\n'''}returninner

}staticdef testNotNullStr(fields) {

def inner1= ''fields.each {

inner1+= ''' \t''' + it.sqlFieldName + ''',\n'''}returninner1

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值