Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
szpt
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
以墨为白
szpt
Commits
22afffd7
Commit
22afffd7
authored
Dec 27, 2024
by
以墨为白
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
3 MybatisGenerator
parent
cdb08408
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
236 additions
and
1 deletion
+236
-1
pom.xml
pom.xml
+21
-0
src/main/java/MybatisGenerator.java
src/main/java/MybatisGenerator.java
+37
-0
src/main/java/com/zksy/szpt/config/MybatisGenerator.java
src/main/java/com/zksy/szpt/config/MybatisGenerator.java
+46
-0
src/main/java/generatorConfig.xml
src/main/java/generatorConfig.xml
+62
-0
src/main/resources/application-dev.yml
src/main/resources/application-dev.yml
+1
-1
src/main/resources/generatorConfig.xml
src/main/resources/generatorConfig.xml
+62
-0
src/main/resources/mysql.properties
src/main/resources/mysql.properties
+7
-0
No files found.
pom.xml
View file @
22afffd7
...
...
@@ -121,6 +121,12 @@
<artifactId>
orika-core
</artifactId>
<version>
1.5.4
</version>
</dependency>
<dependency>
<groupId>
org.mybatis.generator
</groupId>
<artifactId>
mybatis-generator-core
</artifactId>
<version>
1.3.7
</version>
</dependency>
</dependencies>
<build>
...
...
@@ -143,6 +149,21 @@
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
</plugin>
<!-- mybatis逆向工程 -->
<plugin>
<groupId>
org.mybatis.generator
</groupId>
<artifactId>
mybatis-generator-maven-plugin
</artifactId>
<version>
1.3.7
</version>
<dependencies>
<dependency>
<groupId>
mysql
</groupId>
<artifactId>
mysql-connector-java
</artifactId>
<version>
5.1.21
</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<resources>
<resource>
...
...
src/main/java/MybatisGenerator.java
0 → 100644
View file @
22afffd7
import
org.mybatis.generator.api.MyBatisGenerator
;
import
org.mybatis.generator.config.Configuration
;
import
org.mybatis.generator.config.xml.ConfigurationParser
;
import
org.mybatis.generator.internal.DefaultShellCallback
;
import
java.io.InputStream
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* 代码方式生成逆向代码.MyBatis逆向工程.
* 不通过插件方式运行,因为自定义的注释类用插件方式会报错Cannot instantiate object of type com.zksy.szpt.config.MybatisGenerator
* 直接运行此类即可
*/
public
class
MybatisGenerator
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
//MBG执行谊程中的警告信息
List
<
String
>
warnings
=
new
ArrayList
<>();
//当生成的代码重复时,质盖原代码
boolean
overwrite
=
true
;
//读取MBG配置文件
InputStream
is
=
MybatisGenerator
.
class
.
getResourceAsStream
(
"./generatorConfig.xml"
);
ConfigurationParser
cParse
=
new
ConfigurationParser
(
warnings
);
Configuration
configuration
=
cParse
.
parseConfiguration
(
is
);
DefaultShellCallback
callback
=
new
DefaultShellCallback
(
overwrite
);
//创建MBG
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
configuration
,
callback
,
warnings
);
//执行生成代码
myBatisGenerator
.
generate
(
null
);
//输出警告信息
for
(
String
warning
:
warnings
)
{
System
.
out
.
println
(
warning
);
}
}
}
src/main/java/com/zksy/szpt/config/MybatisGenerator.java
0 → 100644
View file @
22afffd7
package
com
.
zksy
.
szpt
.
config
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.Field
;
import
org.mybatis.generator.internal.DefaultCommentGenerator
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.Properties
;
/**
* 官方的默认不支持根据表注释生成实体类注释
* 通过自定义的方式根据表注释生成实体类注释
*/
public
class
MybatisGenerator
extends
DefaultCommentGenerator
{
private
Properties
properties
;
private
Properties
systemPro
;
private
boolean
suppressDate
;
private
boolean
suppressAllComments
;
private
String
currentDateStr
;
public
MybatisGenerator
()
{
super
();
properties
=
new
Properties
();
systemPro
=
System
.
getProperties
();
suppressDate
=
false
;
suppressAllComments
=
false
;
currentDateStr
=
(
new
SimpleDateFormat
(
"yyyy-MM-dd"
)).
format
(
new
Date
());
}
@Override
public
void
addFieldComment
(
Field
field
,
IntrospectedTable
introspectedTable
,
IntrospectedColumn
introspectedColumn
)
{
if
(
suppressAllComments
)
{
return
;
}
StringBuilder
sb
=
new
StringBuilder
();
field
.
addJavaDocLine
(
"/**"
);
sb
.
append
(
" * "
);
sb
.
append
(
introspectedColumn
.
getRemarks
());
field
.
addJavaDocLine
(
sb
.
toString
().
replace
(
" "
,
" "
));
field
.
addJavaDocLine
(
" */"
);
}
}
src/main/java/generatorConfig.xml
0 → 100644
View file @
22afffd7
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- mysql数据源配置文件路径 -->
<properties
resource=
"mysql.properties"
/>
<context
id=
"DB2Tables"
targetRuntime=
"MyBatis3"
>
<!--使用自定义主食类,将表注释写入实体类注释中-->
<commentGenerator
type=
"com.zksy.szpt.config.MybatisGenerator"
>
<!--去掉mybatisgenerator自己的注释-->
<property
name=
"suppressDate"
value=
"true"
/>
<property
name=
"suppressAllComments"
value=
"true"
/>
</commentGenerator>
<!--配置数据库连接-->
<jdbcConnection
driverClass=
"${jdbc.driver}"
connectionURL=
"${db.url}"
userId=
"${db.username}"
password=
"${db.password}"
>
<property
name=
"nullCatalogMeansCurrent"
value=
"true"
/>
</jdbcConnection>
<javaTypeResolver>
<property
name=
"forceBigDecimals"
value=
"false"
/>
</javaTypeResolver>
<!--指定生成javabean的位置-->
<javaModelGenerator
targetPackage=
"com.zksy.szpt.domain.po"
targetProject=
".\src\main\java"
>
<property
name=
"enableSubPackages"
value=
"false"
/>
<property
name=
"trimStrings"
value=
"true"
/>
</javaModelGenerator>
<!--指定sql的映射文件-->
<sqlMapGenerator
targetPackage=
"mapper"
targetProject=
".\src\main\resources"
>
<property
name=
"enableSubPackages"
value=
"true"
/>
</sqlMapGenerator>
<!--指定dao接口生成的位置,mapper接口-->
<javaClientGenerator
type=
"XMLMAPPER"
targetPackage=
"com.zksy.szpt.mapper"
targetProject=
".\src\main\java"
>
<property
name=
"enableSubPackages"
value=
"true"
/>
</javaClientGenerator>
<!--table的生成策略-->
<!-- <table tableName="ALLTYPES" domainObjectName="Customer" >
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table> -->
<!--可多写 要生成的表名-->
<table
tableName=
"%"
schema=
"szpt"
>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
src/main/resources/application-dev.yml
View file @
22afffd7
spring
:
datasource
:
url
:
jdbc:mysql://192.168.168.110:3306/s
wagger
?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8
url
:
jdbc:mysql://192.168.168.110:3306/s
zpt
?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8
username
:
root
password
:
123456
driver-class-name
:
com.mysql.cj.jdbc.Driver
...
...
src/main/resources/generatorConfig.xml
0 → 100644
View file @
22afffd7
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- mysql数据源配置文件路径 -->
<properties
resource=
"mysql.properties"
/>
<context
id=
"DB2Tables"
targetRuntime=
"MyBatis3"
>
<!--使用自定义主食类,将表注释写入实体类注释中-->
<commentGenerator
type=
"com.zksy.szpt.config.MybatisGenerator"
>
<!--去掉mybatisgenerator自己的注释-->
<property
name=
"suppressDate"
value=
"true"
/>
<property
name=
"suppressAllComments"
value=
"true"
/>
</commentGenerator>
<!--配置数据库连接-->
<jdbcConnection
driverClass=
"${jdbc.driver}"
connectionURL=
"${db.url}"
userId=
"${db.username}"
password=
"${db.password}"
>
</jdbcConnection>
<javaTypeResolver
>
<property
name=
"forceBigDecimals"
value=
"false"
/>
</javaTypeResolver>
<!--指定生成javabean的位置-->
<javaModelGenerator
targetPackage=
"com.zksy.szpt.domain.po"
targetProject=
".\src\main\java"
>
<property
name=
"enableSubPackages"
value=
"false"
/>
<property
name=
"trimStrings"
value=
"true"
/>
</javaModelGenerator>
<!--指定sql的映射文件-->
<sqlMapGenerator
targetPackage=
"mapper"
targetProject=
".\src\main\resources"
>
<property
name=
"enableSubPackages"
value=
"true"
/>
</sqlMapGenerator>
<!--指定dao接口生成的位置,mapper接口-->
<javaClientGenerator
type=
"XMLMAPPER"
targetPackage=
"com.zksy.szpt.mapper"
targetProject=
".\src\main\java"
>
<property
name=
"enableSubPackages"
value=
"true"
/>
</javaClientGenerator>
<!--table的生成策略-->
<!-- <table tableName="ALLTYPES" domainObjectName="Customer" >
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table> -->
<!--可多写 要生成的表名-->
<table
tableName=
"%"
schema=
"szpt"
>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
src/main/resources/mysql.properties
0 → 100644
View file @
22afffd7
#连接数据库的驱动
jdbc.driver
=
com.mysql.jdbc.Driver
#数据库连接字符串
db.url
=
jdbc:mysql://192.168.168.110:3306/szpt?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8
db.username
=
root
db.password
=
123456
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment