Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mybatis-generator-plugin
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
Administrator
mybatis-generator-plugin
Commits
13a110ac
Commit
13a110ac
authored
Dec 04, 2018
by
fangzhipeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加分表插件
parent
9d17c497
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
295 additions
and
1 deletion
+295
-1
pom.xml
pom.xml
+1
-1
src/main/java/com/itfsw/mybatis/generator/plugins/TableSplitPlugin.java
...com/itfsw/mybatis/generator/plugins/TableSplitPlugin.java
+175
-0
src/test/java/com/itfsw/mybatis/generator/plugins/TableSplitPluginTest.java
...itfsw/mybatis/generator/plugins/TableSplitPluginTest.java
+27
-0
src/test/resources/scripts/TableSplitPlugin/init.sql
src/test/resources/scripts/TableSplitPlugin/init.sql
+34
-0
src/test/resources/scripts/TableSplitPlugin/mybatis-generator-TableSplitPlugin.xml
...s/TableSplitPlugin/mybatis-generator-TableSplitPlugin.xml
+58
-0
No files found.
pom.xml
View file @
13a110ac
...
...
@@ -3,7 +3,7 @@
<groupId>
com.itfsw
</groupId>
<artifactId>
mybatis-generator-plugin
</artifactId>
<version>
1.2.13-SNAPSHOT
</version>
<version>
1.2.13
.fzp
-SNAPSHOT
</version>
<packaging>
jar
</packaging>
<name>
${project.groupId}:${project.artifactId}
</name>
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/TableSplitPlugin.java
0 → 100644
View file @
13a110ac
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.BeanUtils
;
import
com.itfsw.mybatis.generator.plugins.utils.FormatTools
;
import
com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.*
;
import
org.mybatis.generator.api.dom.xml.Element
;
import
org.mybatis.generator.api.dom.xml.TextElement
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
/**
* @author fangzhipeng
* @date 2018/11/23
*/
public
class
TableSplitPlugin
extends
BasePlugin
{
// record 段修改
@Override
public
boolean
modelBaseRecordClassGenerated
(
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
if
(!
split
(
introspectedTable
))
{
return
true
;
}
Field
index
=
JavaElementGeneratorTools
.
generateField
(
"index"
,
JavaVisibility
.
PRIVATE
,
FullyQualifiedJavaType
.
getStringInstance
(),
null
);
commentGenerator
.
addFieldComment
(
index
,
introspectedTable
);
topLevelClass
.
addField
(
index
);
Method
setter
=
JavaElementGeneratorTools
.
generateSetterMethod
(
index
);
commentGenerator
.
addGeneralMethodComment
(
setter
,
introspectedTable
);
topLevelClass
.
addMethod
(
setter
);
Method
getter
=
JavaElementGeneratorTools
.
generateGetterMethod
(
index
);
commentGenerator
.
addGeneralMethodComment
(
getter
,
introspectedTable
);
topLevelClass
.
addMethod
(
getter
);
return
true
;
}
// example 段修改
@Override
public
boolean
modelExampleClassGenerated
(
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
if
(!
split
(
introspectedTable
))
{
return
true
;
}
Field
index
=
JavaElementGeneratorTools
.
generateField
(
"index"
,
JavaVisibility
.
PRIVATE
,
FullyQualifiedJavaType
.
getStringInstance
(),
null
);
commentGenerator
.
addFieldComment
(
index
,
introspectedTable
);
topLevelClass
.
addField
(
index
);
Method
setter
=
JavaElementGeneratorTools
.
generateSetterMethod
(
index
);
commentGenerator
.
addGeneralMethodComment
(
setter
,
introspectedTable
);
topLevelClass
.
addMethod
(
setter
);
Method
getter
=
JavaElementGeneratorTools
.
generateGetterMethod
(
index
);
commentGenerator
.
addGeneralMethodComment
(
getter
,
introspectedTable
);
topLevelClass
.
addMethod
(
getter
);
// 添加orderBy
Method
orderByMethod
=
JavaElementGeneratorTools
.
generateMethod
(
"index"
,
JavaVisibility
.
PUBLIC
,
topLevelClass
.
getType
(),
new
Parameter
(
FullyQualifiedJavaType
.
getStringInstance
(),
"index"
)
);
commentGenerator
.
addGeneralMethodComment
(
orderByMethod
,
introspectedTable
);
orderByMethod
=
JavaElementGeneratorTools
.
generateMethodBody
(
orderByMethod
,
"this.setIndex(index);"
,
"return this;"
);
FormatTools
.
addMethodWithBestPosition
(
topLevelClass
,
orderByMethod
);
return
true
;
}
// mapper 修改段
@Override
public
boolean
clientSelectByPrimaryKeyMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
if
(!
split
(
introspectedTable
))
{
return
true
;
}
interfaze
.
addImportedType
(
new
FullyQualifiedJavaType
(
"org.apache.ibatis.annotations.Param"
));
method
.
getParameters
().
get
(
0
).
getAnnotations
().
add
(
"@Param(\"id\")"
);
Parameter
index
=
new
Parameter
(
FullyQualifiedJavaType
.
getStringInstance
(),
"index"
,
"@Param(\"index\")"
);
method
.
addParameter
(
index
);
return
true
;
}
// xml 修改段
@Override
public
boolean
sqlMapCountByExampleElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
return
joinIndex
(
element
,
introspectedTable
);
}
@Override
public
boolean
sqlMapInsertElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
return
joinIndex
(
element
,
introspectedTable
);
}
@Override
public
boolean
sqlMapInsertSelectiveElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
return
joinIndex
(
element
,
introspectedTable
);
}
@Override
public
boolean
sqlMapSelectByExampleWithoutBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
return
joinIndex
(
element
,
introspectedTable
);
}
@Override
public
boolean
sqlMapSelectByPrimaryKeyElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
return
joinIndex
(
element
,
introspectedTable
);
}
@Override
public
boolean
sqlMapUpdateByPrimaryKeySelectiveElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
return
joinIndex
(
element
,
introspectedTable
);
}
@Override
public
boolean
sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
return
joinIndex
(
element
,
introspectedTable
);
}
@Override
public
boolean
sqlMapUpdateByExampleWithBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
return
joinIndex
(
element
,
introspectedTable
);
}
/**
* 向xml table名中加入index 后缀
* @param element
* @param introspectedTable
* @return
*/
private
boolean
joinIndex
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
if
(!
split
(
introspectedTable
))
{
return
true
;
}
String
tableName
=
introspectedTable
.
getTableConfiguration
().
getTableName
();
for
(
Element
elementElement
:
element
.
getElements
())
{
if
(!(
elementElement
instanceof
TextElement
))
{
continue
;
}
TextElement
textElement
=
(
TextElement
)
elementElement
;
if
(
textElement
.
getContent
().
indexOf
(
tableName
)
==
-
1
)
{
continue
;
}
try
{
BeanUtils
.
setProperty
(
textElement
,
"content"
,
textElement
.
getContent
().
replace
(
tableName
,
tableName
+
"_${index}"
));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
return
true
;
}
/**
* 判断是否要进行分表
* @param introspectedTable 表信息
* @return
*/
private
boolean
split
(
IntrospectedTable
introspectedTable
)
{
String
split
=
introspectedTable
.
getTableConfigurationProperty
(
"split"
);
if
(
"true"
.
equals
(
split
))
{
return
true
;
}
return
false
;
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/TableSplitPluginTest.java
0 → 100644
View file @
13a110ac
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.tools.DBHelper
;
import
com.itfsw.mybatis.generator.plugins.tools.MyBatisGeneratorTool
;
import
org.junit.BeforeClass
;
import
org.junit.Test
;
import
org.mybatis.generator.api.MyBatisGenerator
;
/**
* @author fangzhipeng
* @date 2018/11/23
*/
public
class
TableSplitPluginTest
{
@BeforeClass
public
static
void
init
()
throws
Exception
{
DBHelper
.
createDB
(
"scripts/TableSplitPlugin/init.sql"
);
}
@Test
public
void
testTableSplit
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/TableSplitPlugin/mybatis-generator-TableSplitPlugin.xml"
);
MyBatisGenerator
myBatisGenerator
=
tool
.
generate
();
myBatisGenerator
.
getGeneratedXmlFiles
();
}
}
src/test/resources/scripts/TableSplitPlugin/init.sql
0 → 100644
View file @
13a110ac
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50617
Source Host : localhost:3306
Source Database : mybatis-generator-plugin
Target Server Type : MYSQL
Target Server Version : 50617
File Encoding : 65001
Date: 2017-06-26 17:30:13
*/
SET
FOREIGN_KEY_CHECKS
=
0
;
-- ----------------------------
-- Table structure for tb1
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb1`
;
CREATE
TABLE
`tb1`
(
`id`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
,
PRIMARY
KEY
(
`id`
)
);
-- ----------------------------
-- Table structure for tb2
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb2`
;
CREATE
TABLE
`tb2`
(
`id`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
,
PRIMARY
KEY
(
`id`
)
);
\ No newline at end of file
src/test/resources/scripts/TableSplitPlugin/mybatis-generator-TableSplitPlugin.xml
0 → 100644
View file @
13a110ac
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2017.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties
resource=
"db.properties"
/>
<!--导入属性配置 -->
<context
id=
"default"
targetRuntime=
"MyBatis3"
>
<!-- 插件 -->
<plugin
type=
"com.itfsw.mybatis.generator.plugins.TableSplitPlugin"
>
</plugin>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${driver}"
connectionURL=
"${url}"
userId=
"${username}"
password=
"${password}"
/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
""
targetProject=
""
>
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
""
targetProject=
""
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator
targetPackage=
""
targetProject=
""
type=
"XMLMAPPER"
/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table
tableName=
"tb1"
>
<property
name=
"split"
value=
"true"
/>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</table>
<table
tableName=
"tb2"
>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</table>
</context>
</generatorConfiguration>
\ 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