在高质量的敏捷开发中,强类型和限制使用,是代码重构和数据模型变更的有利保证。
in high quality Agile development, strong-type and data-limit
are the guarantee for code refactoring and data model changes.
然而遗憾的是,在大多数项目中,数据库访问层(DAL)违背以上实践,情形如下。
Unfortunately, in most projects, the database access layer (DAL)
goes against the above best practice, as follows.
- SQL语句是文本的,重构时只能基于文本查找。
SQL statements are text-based, code refactor based on text search. - 基于生成的POJO,大量
select *
选择多余的字段。
Based on the generated POJO, most usage isselect *
with useless fields. - 大量DTO转换,且一般是基于反射的动态转换。
Too many DTO conversions, and convert by reflect dynamically.
Any2Dto插件,可以根据数据库和JavaCode生成强类型且精确字段的DTO。
Any2Dto plug-in generate strong-type and exact fields DTOs
by database query or JavaCode.
推荐在service中使用静态内类作为DTO,这样可以限制DTO在外部被乱用,而DTO的转换
可以通过wings提供的 live template 直接生成 MapStruct 代码。
It is recommended to use static inner classes as DTOs in services,
which can limit the usage outside of the service. conversion of DTO
can be generated by wings's live template using MapStruct.
在编辑的Java代码中,选择Jooq预定义的字段,可生成与之对应的强类型DTO。
In the Java Editor, select Jooq's fields to generate the strong type DTO.
基于PSI的命名推导,会采用所选表达式的文本,而非运行时命名。
The PSI-based naming derivation use the selected expression not the runtime evaluation.
SysSchemaJournal t = SysSchemaJournal.SYS_SCHEMA_JOURNAL;
Field<Integer> count = count(t.COMMIT_ID).as("cnt");
Field<BigDecimal> par = DSL.field("1", BigDecimal.class);
val result = dsl.select(
count, // <== private Integer count;
par, // <== private BigDecimal par;
t.CREATE_DT, // <== private LocalDateTime createDt;
max(t.MODIFY_DT), // <== private java.time.LocalDateTime modifyDt;
t.TABLE_NAME.as("tn"), // <== private java.lang.String tn;
t.COMMIT_ID.add(t.COMMIT_ID).as(t.COMMIT_ID) // <== java.lang.Long commitId;
)
在Database工具窗(依赖Database插件)选择表和字段,为选择的所有字段生成DTO。
需要注意的是,有时需要SQL到Java的类型绑定,或同名字段调整。
In the Database tool window (need Database plugin), Select tables or columns
to generate DTOs for all selected fields. Note, sometimes it needs modify
the sql-to-Java type-mapping, or the same name field.
在数据库控制台(依赖Database插件)验证业务SELECT
是否正确,并直接为所有查询字段生成DTO。
需要注意的是,此方法通常需要的类型绑定或字段调整。
In database console (need Database plugin), verify the business SELECT
result,
and generate DTOs directly for all query fields. Note, generally it needs
modify the sql-to-Java type-mapping, or the same name field.
选择特点格式的文本,先以按特征字符分行,再逐行分隔成词,以末尾的两个词,可构成DTO。
Select formatted text, split to line by some regexp, and then split each line into words,
take the end of the two words generate the DTO.
选择select的sql语句,然后在剪切板生成wings风格的jooqDSL代码,仅支持简单的mysql和java,
复杂SQL建议使用jdbcTemplate,理由是jooq不是sql,使用jooq就是要拆解复杂SQL。
select SQL-SELECT and generate wings-style jooqDSL java code to clipboard.
only support simple mysql select and java. complex SQL should use jdbcTemplate.
jooq is NOT sql, The purpose of using jooq is to decouple complex SQL.
把选择的代码变为Markdown的审查格式。 review selected code in markdown.
the configuration is project level not global,
click load Default
to reset the plugin default.
Preferences > Tools > Any2dto
generate DTO from JooqDSL/Database/QueryResult/Text.
Where to Save
save DTO code to clipboard or java fileWhich Template
inner class or standard POJO fileClass Name
default isDto
, orprompt
every time.Source Path
when saving to java file,.
means current projectPackage Name
the package name of DTO using inOuter File
Line Separator
the RegExp to split lines in selectionWord Separator
the RegExp to split words in each line
generate JooqDsl from SQL-select. support variables.
Table Naming
table naming, see the input's tipColumn Naming
column naming, see the input's tipJooq DSL
the DSLContext sentence and reference
comment begins with #
, and ignore leading white spaces
TYPE_NAME(PRECISION,SCALE) = JAVA_CLASS, one rule per line,
- TYPE_NAME are case-insensitive
- PRECISION & SCALE are digit,
1-3
=[1,2,3]
,*
=ANY
- (PRECISION,SCALE) should be omitted if match ANY
- for simple
CHAR(1,*)
=CHAR(1)
;CHAR(*)
=CHAR
; the order is FIFO, the top matches first, stop when matched.
the template engine is Meepo that having the same syntax as the target file. there are some builtin variable,
javaPackageName
package fromPackage Name
configjavaTypeImports
imports Class forjavaFields
className
the DTO name in config or promptjavaFields
DTO's fieldstype
java type. eg. Stringname
java name. eg. author
the variable format and sample, only for Sql2Dsl, not Meepo.
- variable -
{var}
or{var|fun}
- var -
tab
|ref
|col
- fun -
PascalCase
|camelCase
|BIG_SNAKE
|snake_case
- suppose x=
moilion-circle
, and {x}=moilion-circle
- {x|PascalCase}=
MoilionCircle
, {x|camelCase}=moilionCircle
- {x|BIG_SNAKE}=
MOILION_CIRCLE
, {x|snake_case}=moilion_circle
Commonly using generated Pojo to SELECT *
is a bad practice. the better way
is SELECT ONLY, WHAT, YOU, NEED
and generate the exact DTO automatically.
Generate DTO code by selected JavaCode, DbColumn, SqlResult, ValuedText.
Right-click the selection in JavaEditor, SqlConsole or DatabaseView,
and choose Any2Dto
in the popup menus, then, the DTO code will
be Generated to clipboard or package with pre-configured rules.
- JooqCode - select Jooq's Fields in DSL, StrongType
- SqlResult - right click query Result View, MappedType
- Database - select columns in Database ToolWindow, MappedType
- ValuedText - select any Non-Java-Word separated values, GuessType
- Sql2DSL - simple mysql select to jooq DSL in wings style
- Review - copy code as markdown to review
for more detail usage, go to github any2dto
Open Preferences > Plugins, and
-
Using IDE built-in plugin system: Marketplace > Search for "Any2dto" > Install Plugin
-
Manually: Download the latest release and install it manually using ⚙️ > Install plugin from disk...
-
Developer: git clone, clean, compile, buildPlugin / runIde / publishPlugin
- redis-replicator Redis Replicator implement Redis Replication protocol written in java. It can parse, filter, broadcast the RDB and AOF events in a real time manner. It also can synchronize redis data to your local cache or to database.
- moilioncircle sharing any topic, skill to make life easier & happier.
- godbart go-db-art, a SQL-based CLI for RDBMS schema versioning & data migration
- WingsBoot java bootstrap of springboot for startup team to fast refactor and fast delivery. type-safe, compile-time, db-schema & table-record can be versioned.
- meepo a comment-base template engine for any program language.
- mirara a java common library for id, text, time, code, etc.