1.打开Database,点击+号-->Data Source-->Oracle如下
2.在General选项下的URL中填入自己数据库的连接信息,基本信息会自动补全。格式如:[ jdbc:oracle:thin:@192.168.1.11:1521:aaa ],然后补全数据库的账户密码。
3.点击Drivers中的Oracle,在Additional files中点击+号添加oracle数据库驱动。然后点击Project Data Sources中刚才自己配置的数据信息。
4.点击Test Connection测试连接,出现Successful表示连接成功。点击Apply。
5.此刻在Database中别可看到自己配置的数据库,右键选择脚本扩展Scripted Extensions-->转到脚本目录Go to Scripts Directory
6.打开模式schema下的Generate POJOs.groovy文件,将如下内容复制替换
import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
import java.text.SimpleDateFormat
/* 关键点请看 TODO */
/* 关键点请看 TODO */
/* 关键点请看 TODO */
typeMapping = [
(~/(?i)int/) : "long",
(~/(?i)float|double|decimal|real/): "double",
(~/(?i)datetime|timestamp/) : "java.sql.Timestamp",
(~/(?i)date/) : "Date",
(~/(?i)time/) : "java.sql.Time",
(~/(?i)/) : "String"
]
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)
packageName = getPackageName(dir)
/* TODO 需要设置编码,否则,导出的脚本会有乱码*/
new File(dir, className + ".java").withPrintWriter("utf-8") { out -> generate(out, className, fields,table) }
}
// 获取包所在文件夹路径
def getPackageName(dir) {
return dir.toString().replaceAll("\\\\", ".").replaceAll("/", ".").replaceAll("^.*src(\\.main\\.java\\.)?", "") + ";"
}
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 java.io.Serializable;"
out.println ""
/* TODO 添加注释*/
out.println "/**\n" +
" * @Description \n" +
" * @Author Functional Manager\n" +
" * @Date: "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +"\n" +
" */"
out.println ""
/* TODO
1.添加@Data 注解,使用lombok,所以不生成 get/set 方法
2.需要生成文档,所以使用swagger注解
*/
out.println "@Data"
out.println "@TableName(\""+table.getName()+"\")"
/* 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]
}
7.点击Scratches选择Project切换回项目目录
8.选择需要生成的表,右键选择脚本扩展Scripted Extensions-->Generate POJOs.groovy
9.选择实体类需要生成的目录位置即可。