1、连接数据库
连接上的效果图
有时候table下没有数据表,刷新一下就会出来
2、生成并修改模板
选中要生成实体类的表,右击
这时候会生成一个Generate POJOs.groovy文件,文件位置在
双击打开后编写自己的模板,下面可供参考
importcom.intellij.database.model.DasTableimportcom.intellij.database.util.Caseimportcom.intellij.database.util.DasUtilimportjava.text.SimpleDateFormat/* 关键点请看 TODO *//* 关键点请看 TODO *//* 关键点请看 TODO *//* TODO 项目包名 */
packageName ="com.shengchi.community.admin.modules.fee.model;"
typeMapping =[(~/(?i)int/):"long",(~/(?i)float|double|decimal|real/):"double",(~/(?i)datetime|timestamp/):"java.sql.Date",(~/(?i)date/):"java.sql.Date",(~/(?i)time/):"java.sql.Time",(~/(?i)/):"String"]FILES.chooseDirectoryAndSave("Choose directory","Choose where to store generated files"){ dir ->SELECTION.filter { it instanceofDasTable}.each {generate(it, dir)}}
def generate(table, dir){
def className =javaName(table.getName(),true)
def fields =calcFields(table)/* TODO 需要设置编码,否则,导出的脚本会有乱码*/newFile(dir, className +".java").withPrintWriter("utf-8"){ out ->generate(out, className, fields,table)}}
def generate(out, className, fields,table){
out.println "package $packageName"/* TODO 手动添加项目需要的包*/
out.println "import lombok.Data;"
out.println "import com.baomidou.mybatisplus.annotation.TableName;"
out.println "import lombok.EqualsAndHashCode;"
out.println "import lombok.experimental.Accessors;"
out.println "import io.swagger.annotations.ApiModel;"
out.println "import java.util.Date;"
out.println "import javax.persistence.Column;"
out.println "import javax.persistence.Entity;"
out.println "import javax.persistence.Table;"
out.println ""/* TODO 添加注释*/
out.println "/**\n"+" * @Description \n"+" * @Author MXZ\n"+" * @Date: "+newSimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newDate())+"\n"+" */"
out.println ""/* TODO
1.添加@Data 注解,使用lombok,所以不生成 get/set 方法
2.需要生成文档,所以使用swagger注解
*/
out.println "@Data"
out.println "@TableName(\""+table.getName()+"\")"
out.println "@EqualsAndHashCode(callSuper = false)"
out.println "@Accessors(chain = true)"/* TODO 添加表名和表说明*/
out.println "@ApiModel(value=\" "+ table.getName()+" \", description=\" "+table.getComment()+" \")"
out.println "public class $className {"
out.println ""
fields.each(){/* TODO 添加字段属性和注解*/
out.println " @ApiModelProperty(value = \""+it.commoent+"\")"
out.println " private ${it.type} ${it.name};"
out.println " \n"}
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 +=[[/* TODO name:字段名 type:字段类型 commoent:字段说明*/
name :javaName(col.getName(),false),
type : typeStr,
commoent: col.getComment()]]}}
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]}
3、生成实体类
然后选择生成实体类要存放的位置
4、生成结果
本文转载自: https://blog.csdn.net/qq_45133806/article/details/138123355
版权归原作者 一梦荼靡 所有, 如有侵权,请联系我们删除。
版权归原作者 一梦荼靡 所有, 如有侵权,请联系我们删除。