代码拉取完成,页面将自动刷新
同步操作将从 China-1977/life 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
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 = "work.onss.community.domain;"
typeMapping = [
(~/(?i)int/) : "Long",
(~/(?i)float|double|decimal|real/): "Double",
(~/(?i)datetime|timestamp/) : "LocalDateTime",
(~/(?i)date/) : "LocalDate",
(~/(?i)time/) : "LocalTime",
(~/(?i)numeric/) : "BigDecimal",
(~/(?i)varchar|char/) : "String",
(~/(?i)/) : "Object"
]
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to organization 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 + ".java").withPrintWriter { out -> Entity(out, className, fields) }
new File(dir, className + "Repository.java").withPrintWriter { out -> JpaRepository(out, className) }
}
def Entity(out, className, fields) {
out.println "package $packageName"
out.println ""
out.println "import lombok.Getter;"
out.println "import lombok.Setter;"
out.println "import lombok.extern.log4j.Log4j2;"
out.println "import org.springframework.data.annotation.CreatedDate;"
out.println "import org.springframework.data.annotation.LastModifiedDate;"
out.println "import org.springframework.data.jpa.domain.support.AuditingEntityListener;"
out.println ""
out.println "import jakarta.persistence.*;"
out.println "import java.io.Serializable;"
out.println "import java.time.LocalTime;"
out.println "import java.time.LocalDate;"
out.println "import java.time.LocalDateTime;"
out.println "import java.math.BigDecimal;"
out.println ""
out.println "@Log4j2"
out.println "@Getter"
out.println "@Setter"
out.println "@Entity"
out.println "@EntityListeners(AuditingEntityListener.class)"
out.println "public class $className implements Serializable {"
out.println ""
fields.each() {
it.annos.each() {
out.println " ${it}"
}
out.println " private ${it.type} ${it.name};"
}
out.println "}"
}
def JpaRepository(out, className) {
out.println "package $packageName"
out.println ""
out.println "import org.springframework.data.jpa.repository.JpaRepository;"
out.println "import org.springframework.data.jpa.repository.JpaSpecificationExecutor;"
out.println "import org.springframework.data.querydsl.QuerydslPredicateExecutor;"
out.println "import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;"
out.println "import org.springframework.data.querydsl.binding.QuerydslBindings;"
out.println ""
out.println "import java.time.LocalDate;"
out.println "import java.util.Iterator;"
out.println "import java.util.Optional;"
out.println ""
out.println "public interface ${className}Repository extends JpaRepository<${className}, Long>, JpaSpecificationExecutor<${className}>, QuerydslPredicateExecutor<${className}>, QuerydslBinderCustomizer<Q${className}> {"
out.println ""
out.println " default void customize(QuerydslBindings bindings, Q${className} q${className}) {"
out.println " bindings.bind(q${className}.id).withDefaultBinding();"
out.println " bindings.bind(q${className}.insertDate).all(((path, value) -> {"
out.println " Iterator<? extends LocalDate> iterator = value.iterator();"
out.println " if (value.size() == 1) {"
out.println " return Optional.ofNullable(path.eq(iterator.next()));"
out.println " }"
out.println " return Optional.ofNullable(path.between(iterator.next(), iterator.next()));"
out.println " }));"
out.println " bindings.bind(q${className}.updateDate).all(((path, value) -> {"
out.println " Iterator<? extends LocalDate> iterator = value.iterator();"
out.println " if (value.size() == 1) {"
out.println " return Optional.ofNullable(path.eq(iterator.next()));"
out.println " }"
out.println " return Optional.ofNullable(path.between(iterator.next(), iterator.next()));"
out.println " }));"
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
def name = javaName(col.getName(), false);
def annos = [];
switch (name) {
case "id":
annos.add("@Id")
annos.add("@GeneratedValue(strategy = GenerationType.UUID)"); break
case "insertDate": annos.add("@CreatedDate"); break
case "updateDate": annos.add("@LastModifiedDate"); break
}
fields += [[name: name, type: typeStr, annos: 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]
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。