phpstorm 生产php pojo类
一. 修改Generate POJO.groovy文件
改为
import com.intellij.database.model.DasTable import com.intellij.database.util.Case import com.intellij.database.util.DasUtil /* * Available context bindings: * SELECTION Iterable<DasObject> * PROJECT project * FILES files helper */ packageName = "app\\common\\entity;" typeMapping = [ (~/(?i)int/) : "\$", (~/(?i)float|double|decimal|real/): "\$", (~/(?i)datetime|timestamp/) : "\$", (~/(?i)date/) : "\$", (~/(?i)time/) : "\$", (~/(?i)/) : "\$" ] 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) def fields = calcFields(table) new File(dir, className + ".php").withPrintWriter { out -> generate(out, className, fields) } } def generate(out, className, fields) { out.println "<?php" out.println "" out.println "" out.println "namespace $packageName" out.println "" out.println "" out.println "class $className {" out.println "" fields.each() { if (it.annos != "") out.println " ${it.annos}" out.println " private ${it.type}${it.name};" } out.println "" out.println " public function __construct("+arrayToString(fields)+")" out.println " {" fields.each() { out.println " \$this->${it.name} = "+"\$"+"${it.name};" } out.println " }" fields.each() { out.println "" out.println " public function get${it.name.capitalize()}() {" out.println " return \$this->${it.name};" out.println " }" out.println "" out.println " public function set${it.name.capitalize()}(${it.type}${it.name}) {" out.println " \$this->${it.name} = "+"\$"+"${it.name};" out.println " }" out.println "" } out.println "}" } 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 += [[ name : javaName(col.getName(), false), type : typeStr, annos: ""]] } } 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] } def arrayToString(fields){ String str = ""; fields.each() { str+=("\$"+it.name+",") } str = str.substring(0,str.length()-1); return str; }
二.生产php pojo类
打开phpstorm database菜单,选取要生成POJO类的表点击鼠标右键, 选择scrpted excensission->Gennerate POJOs.groovy