46 lines
1.6 KiB
Groovy
46 lines
1.6 KiB
Groovy
|
import com.intellij.database.model.DasTable
|
||
|
import com.intellij.database.util.Case
|
||
|
|
||
|
/*
|
||
|
* Available context bindings:
|
||
|
* SELECTION Iterable<DasObject>
|
||
|
* PROJECT project
|
||
|
* FILES files helper
|
||
|
*/
|
||
|
|
||
|
packageName = "com.lz;"
|
||
|
|
||
|
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
|
||
|
SELECTION.filter { it instanceof DasTable }.each { generate(it, dir) }
|
||
|
}
|
||
|
|
||
|
def generate(table, dir) {
|
||
|
def className = javaName(table.getName(), true)
|
||
|
new File(dir, className + "ServiceImpl.java").withPrintWriter { out -> generate(out, className, table) }
|
||
|
}
|
||
|
|
||
|
def generate(out, className, table) {
|
||
|
out.println "package $packageName"
|
||
|
out.println ""
|
||
|
out.println "import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;"
|
||
|
out.println "import org.springframework.stereotype.Service;"
|
||
|
out.println ""
|
||
|
out.println "/**"
|
||
|
out.println " * ${table.getComment() == null ? table.getName() : table.getName() + " (" + table.getComment() + ") "} 服务实现类"
|
||
|
out.println " * author: luozhun"
|
||
|
out.println " * desc 由groovy脚本自动生成"
|
||
|
out.println " */"
|
||
|
out.println "@Service"
|
||
|
out.println "public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper,${className}> implements ${className}Service{"
|
||
|
out.println ""
|
||
|
out.println "}"
|
||
|
}
|
||
|
|
||
|
def javaName(str, capitalize) {
|
||
|
def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
|
||
|
.collect { Case.LOWER.apply(it).capitalize() }
|
||
|
.join("")
|
||
|
.replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
|
||
|
capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1]
|
||
|
}
|