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
8a15c348
Commit
8a15c348
authored
Jul 05, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重构测试框架,JAVA类转载不能实现按需卸载
parent
645a961f
Changes
18
Hide whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
490 additions
and
448 deletions
+490
-448
.gitignore
.gitignore
+1
-2
src/test/java/com/itfsw/mybatis/generator/plugins/BatchInsertPluginTest.java
...tfsw/mybatis/generator/plugins/BatchInsertPluginTest.java
+42
-84
src/test/java/com/itfsw/mybatis/generator/plugins/DBHelperTest.java
...ava/com/itfsw/mybatis/generator/plugins/DBHelperTest.java
+12
-11
src/test/java/com/itfsw/mybatis/generator/plugins/ExampleTargetPluginTest.java
...sw/mybatis/generator/plugins/ExampleTargetPluginTest.java
+12
-32
src/test/java/com/itfsw/mybatis/generator/plugins/SelectSelectivePluginTest.java
.../mybatis/generator/plugins/SelectSelectivePluginTest.java
+31
-81
src/test/java/com/itfsw/mybatis/generator/plugins/tools/AbstractShellCallback.java
...ybatis/generator/plugins/tools/AbstractShellCallback.java
+28
-112
src/test/java/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java
...a/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java
+31
-89
src/test/java/com/itfsw/mybatis/generator/plugins/tools/MyBatisGeneratorTool.java
...mybatis/generator/plugins/tools/MyBatisGeneratorTool.java
+240
-0
src/test/java/com/itfsw/mybatis/generator/plugins/tools/Util.java
.../java/com/itfsw/mybatis/generator/plugins/tools/Util.java
+61
-5
src/test/resources/db.properties
src/test/resources/db.properties
+4
-4
src/test/resources/mybatis-config.xml
src/test/resources/mybatis-config.xml
+1
-1
src/test/resources/scripts/BatchInsertPlugin/mybatis-generator-with-error-driver.xml
...BatchInsertPlugin/mybatis-generator-with-error-driver.xml
+3
-3
src/test/resources/scripts/BatchInsertPlugin/mybatis-generator-without-model-column-plugin.xml
...tPlugin/mybatis-generator-without-model-column-plugin.xml
+4
-4
src/test/resources/scripts/BatchInsertPlugin/mybatis-generator.xml
...resources/scripts/BatchInsertPlugin/mybatis-generator.xml
+4
-4
src/test/resources/scripts/ExampleTargetPlugin/mybatis-generator-without-plugin.xml
.../ExampleTargetPlugin/mybatis-generator-without-plugin.xml
+4
-4
src/test/resources/scripts/ExampleTargetPlugin/mybatis-generator.xml
...sources/scripts/ExampleTargetPlugin/mybatis-generator.xml
+4
-4
src/test/resources/scripts/SelectSelectivePlugin/mybatis-generator-with-SelectOneByExamplePlugin.xml
...lugin/mybatis-generator-with-SelectOneByExamplePlugin.xml
+4
-4
src/test/resources/scripts/SelectSelectivePlugin/mybatis-generator.xml
...urces/scripts/SelectSelectivePlugin/mybatis-generator.xml
+4
-4
No files found.
.gitignore
View file @
8a15c348
...
...
@@ -12,5 +12,4 @@
hs_err_pid*
.idea/
mybatis-generator-plugin.iml
target/
src/test/java/com/itfsw/mybatis/generator/plugins/dao/
\ No newline at end of file
target/
\ No newline at end of file
src/test/java/com/itfsw/mybatis/generator/plugins/BatchInsertPluginTest.java
View file @
8a15c348
...
...
@@ -17,18 +17,12 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.tools.*
;
import
org.apache.ibatis.io.Resources
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.AfterClass
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Before
Class
;
import
org.junit.Test
;
import
org.mybatis.generator.api.MyBatisGenerator
;
import
org.mybatis.generator.config.Configuration
;
import
org.mybatis.generator.config.xml.ConfigurationParser
;
import
org.mybatis.generator.exception.InvalidConfigurationException
;
import
org.mybatis.generator.exception.XMLParserException
;
import
org.mybatis.generator.internal.DefaultShellCallback
;
import
java.io.IOException
;
import
java.lang.reflect.Array
;
...
...
@@ -46,16 +40,15 @@ import java.util.List;
* ---------------------------------------------------------------------------
*/
public
class
BatchInsertPluginTest
{
private
DBHelper
helper
;
/**
* 初始化
* @throws IOException
* @throws SQLException
*/
@Before
public
void
init
()
throws
IOException
,
SQL
Exception
{
helper
=
DBHelper
.
getHelper
(
"scripts/BatchInsertPlugin/init.sql"
);
@Before
Class
public
static
void
init
()
throws
Exception
{
DBHelper
.
createDB
(
"scripts/BatchInsertPlugin/init.sql"
);
}
/**
...
...
@@ -67,17 +60,12 @@ public class BatchInsertPluginTest {
* @throws InterruptedException
*/
@Test
public
void
testWarnings1
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
SQLException
,
InterruptedException
{
List
<
String
>
warnings
=
new
ArrayList
<>();
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/BatchInsertPlugin/mybatis-generator-without-model-column-plugin.xml"
));
public
void
testWarnings1
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/BatchInsertPlugin/mybatis-generator-without-model-column-plugin.xml"
);
tool
.
generate
();
DefaultShellCallback
shellCallback
=
new
DefaultShellCallback
(
true
);
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
shellCallback
,
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
false
);
Assert
.
assertTrue
(
warnings
.
size
()
==
2
);
Assert
.
assertEquals
(
warnings
.
get
(
0
),
"itfsw:插件com.itfsw.mybatis.generator.plugins.BatchInsertPlugin插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!"
);
Assert
.
assertTrue
(
tool
.
getWarnings
().
size
()
==
2
);
Assert
.
assertEquals
(
tool
.
getWarnings
().
get
(
0
),
"itfsw:插件com.itfsw.mybatis.generator.plugins.BatchInsertPlugin插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!"
);
}
/**
...
...
@@ -89,17 +77,12 @@ public class BatchInsertPluginTest {
* @throws InterruptedException
*/
@Test
public
void
testWarnings2
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
SQLException
,
InterruptedException
{
List
<
String
>
warnings
=
new
ArrayList
<>();
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/BatchInsertPlugin/mybatis-generator-with-error-driver.xml"
));
DefaultShellCallback
shellCallback
=
new
DefaultShellCallback
(
true
);
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
shellCallback
,
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
false
);
public
void
testWarnings2
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/BatchInsertPlugin/mybatis-generator-with-error-driver.xml"
);
tool
.
generate
();
Assert
.
assertTrue
(
warnings
.
size
()
==
3
);
Assert
.
assertEquals
(
warnings
.
get
(
1
),
"itfsw:插件com.itfsw.mybatis.generator.plugins.BatchInsertPlugin插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键!"
);
Assert
.
assertTrue
(
tool
.
getWarnings
()
.
size
()
==
3
);
Assert
.
assertEquals
(
tool
.
getWarnings
()
.
get
(
1
),
"itfsw:插件com.itfsw.mybatis.generator.plugins.BatchInsertPlugin插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键!"
);
}
/**
...
...
@@ -113,42 +96,39 @@ public class BatchInsertPluginTest {
* @throws NoSuchMethodException
*/
@Test
public
void
testMethods
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
SQLException
,
InterruptedException
,
ClassNotFoundException
,
NoSuchMethodException
{
List
<
String
>
warnings
=
new
ArrayList
<>();
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/BatchInsertPlugin/mybatis-generator.xml"
));
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
AbstractShellCallback
(
true
)
{
public
void
testMethods
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/BatchInsertPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
ClassLoader
loader
)
{
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
{
try
{
// 1. 普通Mapper参数中List泛型为普通Model
Class
clsTbMapper
=
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao
.TbMapper"
);
Class
clsTbMapper
=
loader
.
loadClass
(
packagz
+
"
.TbMapper"
);
int
count
=
0
;
for
(
Method
method
:
clsTbMapper
.
getDeclaredMethods
())
{
if
(
method
.
getName
().
equals
(
"batchInsert"
))
{
Assert
.
assertEquals
(
Util
.
getListActualType
(
method
.
getGenericParameterTypes
()[
0
]),
"com.itfsw.mybatis.generator.plugins.dao.model
.Tb"
);
Assert
.
assertEquals
(
Util
.
getListActualType
(
method
.
getGenericParameterTypes
()[
0
]),
packagz
+
"
.Tb"
);
count
++;
}
if
(
method
.
getName
().
equals
(
"batchInsertSelective"
))
{
Assert
.
assertEquals
(
Util
.
getListActualType
(
method
.
getGenericParameterTypes
()[
0
]),
"com.itfsw.mybatis.generator.plugins.dao.model
.Tb"
);
Assert
.
assertEquals
(
method
.
getGenericParameterTypes
()[
1
].
getTypeName
(),
"com.itfsw.mybatis.generator.plugins.dao.model
.Tb$Column[]"
);
Assert
.
assertEquals
(
Util
.
getListActualType
(
method
.
getGenericParameterTypes
()[
0
]),
packagz
+
"
.Tb"
);
Assert
.
assertEquals
(
method
.
getGenericParameterTypes
()[
1
].
getTypeName
(),
packagz
+
"
.Tb$Column[]"
);
count
++;
}
}
Assert
.
assertEquals
(
count
,
2
);
// 2. 带有WithBlobs
Class
clsTbBlobsMapper
=
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao
.TbBlobsMapper"
);
Class
clsTbBlobsMapper
=
loader
.
loadClass
(
packagz
+
"
.TbBlobsMapper"
);
count
=
0
;
for
(
Method
method
:
clsTbBlobsMapper
.
getDeclaredMethods
())
{
if
(
method
.
getName
().
equals
(
"batchInsert"
))
{
Assert
.
assertEquals
(
Util
.
getListActualType
(
method
.
getGenericParameterTypes
()[
0
]),
"com.itfsw.mybatis.generator.plugins.dao.model
.TbBlobsWithBLOBs"
);
Assert
.
assertEquals
(
Util
.
getListActualType
(
method
.
getGenericParameterTypes
()[
0
]),
packagz
+
"
.TbBlobsWithBLOBs"
);
count
++;
}
if
(
method
.
getName
().
equals
(
"batchInsertSelective"
))
{
Assert
.
assertEquals
(
Util
.
getListActualType
(
method
.
getGenericParameterTypes
()[
0
]),
"com.itfsw.mybatis.generator.plugins.dao.model
.TbBlobsWithBLOBs"
);
Assert
.
assertEquals
(
method
.
getGenericParameterTypes
()[
1
].
getTypeName
(),
"com.itfsw.mybatis.generator.plugins.dao.model
.TbBlobsWithBLOBs$Column[]"
);
Assert
.
assertEquals
(
Util
.
getListActualType
(
method
.
getGenericParameterTypes
()[
0
]),
packagz
+
"
.TbBlobsWithBLOBs"
);
Assert
.
assertEquals
(
method
.
getGenericParameterTypes
()[
1
].
getTypeName
(),
packagz
+
"
.TbBlobsWithBLOBs$Column[]"
);
count
++;
}
}
...
...
@@ -158,8 +138,7 @@ public class BatchInsertPluginTest {
Assert
.
assertTrue
(
false
);
}
}
},
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
});
}
/**
...
...
@@ -167,27 +146,21 @@ public class BatchInsertPluginTest {
*/
@Test
public
void
testBatchInsert
()
throws
Exception
{
DBHelper
.
cleanDao
();
List
<
String
>
warnings
=
new
ArrayList
<>();
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/BatchInsertPlugin/mybatis-generator.xml"
));
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
AbstractShellCallback
(
true
)
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/BatchInsertPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
ClassLoader
loader
)
{
SqlSession
sqlSession
=
null
;
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
{
try
{
// 1. 测试sql
sqlSession
=
helper
.
getSqlSession
();
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.TbMapper"
)));
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
List
<
Object
>
params
=
new
ArrayList
<>();
params
.
add
(
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.Tb"
)
new
ObjectUtil
(
loader
,
packagz
+
"
.Tb"
)
.
set
(
"field1"
,
"test"
)
.
getObject
()
);
params
.
add
(
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.Tb"
)
new
ObjectUtil
(
loader
,
packagz
+
"
.Tb"
)
.
set
(
"field1"
,
"test"
)
.
set
(
"field2"
,
1
)
.
getObject
()
...
...
@@ -200,12 +173,9 @@ public class BatchInsertPluginTest {
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
Assert
.
assertTrue
(
false
);
}
finally
{
sqlSession
.
close
();
}
}
},
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
});
}
/**
...
...
@@ -213,32 +183,26 @@ public class BatchInsertPluginTest {
*/
@Test
public
void
testBatchInsertSelective
()
throws
Exception
{
DBHelper
.
cleanDao
();
List
<
String
>
warnings
=
new
ArrayList
<>();
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/BatchInsertPlugin/mybatis-generator.xml"
));
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
AbstractShellCallback
(
true
)
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/BatchInsertPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
ClassLoader
loader
)
{
SqlSession
sqlSession
=
null
;
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
{
try
{
// 1. 测试sql
sqlSession
=
helper
.
getSqlSession
();
ObjectUtil
tbBlobsMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.TbBlobsMapper"
)));
ObjectUtil
tbBlobsMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbBlobsMapper"
)));
List
<
Object
>
params
=
new
ArrayList
<>();
params
.
add
(
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.TbBlobsWithBLOBs"
)
new
ObjectUtil
(
loader
,
packagz
+
"
.TbBlobsWithBLOBs"
)
.
set
(
"field1"
,
"test"
)
.
getObject
()
);
params
.
add
(
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.TbBlobsWithBLOBs"
)
new
ObjectUtil
(
loader
,
packagz
+
"
.TbBlobsWithBLOBs"
)
.
set
(
"field1"
,
"test"
)
.
set
(
"field2"
,
"test123"
)
.
getObject
()
);
ObjectUtil
columnField2
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.TbBlobsWithBLOBs$Column#field2"
);
ObjectUtil
columnField2
=
new
ObjectUtil
(
loader
,
packagz
+
"
.TbBlobsWithBLOBs$Column#field2"
);
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object
columns
=
Array
.
newInstance
(
columnField2
.
getCls
(),
1
);
Array
.
set
(
columns
,
0
,
columnField2
.
getObject
());
...
...
@@ -255,12 +219,6 @@ public class BatchInsertPluginTest {
sqlSession
.
close
();
}
}
},
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
}
@AfterClass
public
static
void
clean
()
{
DBHelper
.
reset
();
});
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/DBHelperTest.java
View file @
8a15c348
...
...
@@ -17,15 +17,11 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.tools.DBHelper
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
java.io.IOException
;
import
java.sql.Connection
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.sql.*
;
/**
* ---------------------------------------------------------------------------
...
...
@@ -43,10 +39,17 @@ public class DBHelperTest {
* @throws SQLException
*/
@Test
public
void
testGetSqlSession
()
throws
IOException
,
SQLException
{
DBHelper
helper
=
DBHelper
.
getHelper
(
"scripts/test_init.sql"
);
SqlSession
sqlSession
=
helper
.
getSqlSession
();
Connection
connection
=
sqlSession
.
getConnection
();
public
void
testGetSqlSession
()
throws
Exception
{
DBHelper
.
createDB
(
"scripts/test_init.sql"
);
String
driver
=
DBHelper
.
properties
.
getProperty
(
"driver"
);
String
url
=
DBHelper
.
properties
.
getProperty
(
"url"
);
String
username
=
DBHelper
.
properties
.
getProperty
(
"username"
);
String
password
=
DBHelper
.
properties
.
getProperty
(
"password"
);
// 获取connection
Class
.
forName
(
driver
);
Connection
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
);
Statement
statement
=
connection
.
createStatement
();
// 执行查询
...
...
@@ -58,7 +61,5 @@ public class DBHelperTest {
statement
.
close
();
connection
.
close
();
sqlSession
.
close
();
DBHelper
.
reset
();
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/ExampleTargetPluginTest.java
View file @
8a15c348
...
...
@@ -17,22 +17,15 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.tools.DBHelper
;
import
org.apache.ibatis.io.Resources
;
import
org.junit.After
;
import
com.itfsw.mybatis.generator.plugins.tools.MyBatisGeneratorTool
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Before
Class
;
import
org.junit.Test
;
import
org.mybatis.generator.api.GeneratedJavaFile
;
import
org.mybatis.generator.api.MyBatisGenerator
;
import
org.mybatis.generator.config.Configuration
;
import
org.mybatis.generator.config.xml.ConfigurationParser
;
import
org.mybatis.generator.exception.InvalidConfigurationException
;
import
org.mybatis.generator.exception.XMLParserException
;
import
org.mybatis.generator.internal.DefaultShellCallback
;
import
java.io.IOException
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
...
...
@@ -44,44 +37,35 @@ import java.util.List;
* ---------------------------------------------------------------------------
*/
public
class
ExampleTargetPluginTest
{
private
DBHelper
helper
;
/**
* 初始化
* @throws IOException
* @throws SQLException
*/
@Before
public
void
init
()
throws
IOException
,
SQL
Exception
{
helper
=
DBHelper
.
getHelper
(
"scripts/ExampleTargetPlugin/init.sql"
);
@Before
Class
public
static
void
init
()
throws
Exception
{
DBHelper
.
createDB
(
"scripts/ExampleTargetPlugin/init.sql"
);
}
@Test
public
void
testNormalPath
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
SQLException
,
InterruptedException
{
List
<
String
>
warnings
=
new
ArrayList
<
String
>();
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/ExampleTargetPlugin/mybatis-generator-without-plugin.xml"
));
public
void
testNormalPath
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/ExampleTargetPlugin/mybatis-generator-without-plugin.xml"
);
MyBatisGenerator
myBatisGenerator
=
tool
.
generate
();
DefaultShellCallback
shellCallback
=
new
DefaultShellCallback
(
true
);
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
shellCallback
,
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
false
);
List
<
GeneratedJavaFile
>
list
=
myBatisGenerator
.
getGeneratedJavaFiles
();
for
(
GeneratedJavaFile
file
:
list
){
if
(
file
.
getFileName
().
equals
(
"TbExample.java"
)){
Assert
.
assertEquals
(
file
.
getTargetPackage
(),
"com.itfsw.mybatis.generator.plugins.dao.model"
);
Assert
.
assertEquals
(
file
.
getTargetPackage
(),
tool
.
getTargetPackage
()
);
}
}
}
@Test
public
void
testConfigPath
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
SQLException
,
InterruptedException
{
List
<
String
>
warnings
=
new
ArrayList
<
String
>();
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/ExampleTargetPlugin/mybatis-generator.xml"
));
public
void
testConfigPath
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/ExampleTargetPlugin/mybatis-generator.xml"
);
MyBatisGenerator
myBatisGenerator
=
tool
.
generate
();
DefaultShellCallback
shellCallback
=
new
DefaultShellCallback
(
true
);
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
shellCallback
,
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
false
);
List
<
GeneratedJavaFile
>
list
=
myBatisGenerator
.
getGeneratedJavaFiles
();
for
(
GeneratedJavaFile
file
:
list
){
if
(
file
.
getFileName
().
equals
(
"TbExample.java"
)){
...
...
@@ -90,8 +74,4 @@ public class ExampleTargetPluginTest {
}
}
@After
public
void
clean
(){
DBHelper
.
reset
();
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/SelectSelectivePluginTest.java
View file @
8a15c348
...
...
@@ -16,26 +16,15 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.tools.AbstractShellCallback
;
import
com.itfsw.mybatis.generator.plugins.tools.DBHelper
;
import
com.itfsw.mybatis.generator.plugins.tools.ObjectUtil
;
import
com.itfsw.mybatis.generator.plugins.tools.SqlHelper
;
import
org.apache.ibatis.io.Resources
;
import
com.itfsw.mybatis.generator.plugins.tools.*
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.AfterClass
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Before
Class
;
import
org.junit.Test
;
import
org.mybatis.generator.api.GeneratedJavaFile
;
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.IOException
;
import
java.lang.reflect.Array
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
...
...
@@ -47,16 +36,10 @@ import java.util.List;
* ---------------------------------------------------------------------------
*/
public
class
SelectSelectivePluginTest
{
private
DBHelper
helper
;
/**
* 初始化
* @throws IOException
* @throws SQLException
*/
@Before
public
void
init
()
throws
IOException
,
SQLException
{
helper
=
DBHelper
.
getHelper
(
"scripts/SelectSelectivePlugin/init.sql"
);
@BeforeClass
public
static
void
init
()
throws
Exception
{
DBHelper
.
createDB
(
"scripts/SelectSelectivePlugin/init.sql"
);
}
/**
...
...
@@ -65,26 +48,20 @@ public class SelectSelectivePluginTest {
*/
@Test
public
void
testSelectByExampleSelective
()
throws
Exception
{
DBHelper
.
cleanDao
();
List
<
String
>
warnings
=
new
ArrayList
<>();
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/SelectSelectivePlugin/mybatis-generator.xml"
));
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
AbstractShellCallback
(
true
)
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/SelectSelectivePlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
ClassLoader
loader
)
{
SqlSession
sqlSession
=
null
;
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
{
try
{
// 1. 测试sql
sqlSession
=
helper
.
getSqlSession
();
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.TbMapper"
)));
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.TbExample"
);
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
packagz
+
"
.TbExample"
);
ObjectUtil
criteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andIdLessThan"
,
100
l
);
tbExample
.
set
(
"orderByClause"
,
"field2 asc"
);
ObjectUtil
columnField1
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.Tb$Column#field1"
);
ObjectUtil
columnField1
=
new
ObjectUtil
(
loader
,
packagz
+
"
.Tb$Column#field1"
);
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object
columns1
=
Array
.
newInstance
(
columnField1
.
getCls
(),
1
);
Array
.
set
(
columns1
,
0
,
columnField1
.
getObject
());
...
...
@@ -92,7 +69,7 @@ public class SelectSelectivePluginTest {
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectByExampleSelective"
,
tbExample
.
getObject
(),
columns1
);
Assert
.
assertEquals
(
sql
,
"select field1 from tb WHERE ( id < '100' ) order by field2 asc"
);
ObjectUtil
columnField2
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.Tb$Column#field2"
);
ObjectUtil
columnField2
=
new
ObjectUtil
(
loader
,
packagz
+
"
.Tb$Column#field2"
);
Object
columns2
=
Array
.
newInstance
(
columnField1
.
getCls
(),
2
);
Array
.
set
(
columns2
,
0
,
columnField1
.
getObject
());
Array
.
set
(
columns2
,
1
,
columnField2
.
getObject
());
...
...
@@ -124,12 +101,9 @@ public class SelectSelectivePluginTest {
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
Assert
.
assertTrue
(
false
);
}
finally
{
sqlSession
.
close
();
}
}
},
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
});
}
/**
...
...
@@ -138,21 +112,15 @@ public class SelectSelectivePluginTest {
*/
@Test
public
void
testSelectByPrimaryKeySelective
()
throws
Exception
{
DBHelper
.
cleanDao
();
List
<
String
>
warnings
=
new
ArrayList
<>();
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/SelectSelectivePlugin/mybatis-generator.xml"
));
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
AbstractShellCallback
(
true
)
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/SelectSelectivePlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
ClassLoader
loader
)
{
SqlSession
sqlSession
=
null
;
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
{
try
{
// 1. 测试sql
sqlSession
=
helper
.
getSqlSession
();
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.TbMapper"
)));
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
columnField1
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.TbKeys$Column#field1"
);
ObjectUtil
columnField1
=
new
ObjectUtil
(
loader
,
packagz
+
"
.TbKeys$Column#field1"
);
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object
columns1
=
Array
.
newInstance
(
columnField1
.
getCls
(),
1
);
Array
.
set
(
columns1
,
0
,
columnField1
.
getObject
());
...
...
@@ -161,12 +129,12 @@ public class SelectSelectivePluginTest {
Assert
.
assertEquals
(
sql
,
"select field1 from tb where id = 1"
);
// 2. 测试xxxKey
ObjectUtil
tbKeysMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao
.TbKeysMapper"
)));
ObjectUtil
tbKeysKey
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.TbKeysKey"
);
ObjectUtil
tbKeysMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
"
.TbKeysMapper"
)));
ObjectUtil
tbKeysKey
=
new
ObjectUtil
(
loader
,
packagz
+
"
.TbKeysKey"
);
tbKeysKey
.
set
(
"key1"
,
1
l
);
tbKeysKey
.
set
(
"key2"
,
"2"
);
ObjectUtil
columnField2
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.TbKeys$Column#field2"
);
ObjectUtil
columnField2
=
new
ObjectUtil
(
loader
,
packagz
+
"
.TbKeys$Column#field2"
);
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object
columns2
=
Array
.
newInstance
(
columnField2
.
getCls
(),
1
);
Array
.
set
(
columns2
,
0
,
columnField2
.
getObject
());
...
...
@@ -185,8 +153,7 @@ public class SelectSelectivePluginTest {
sqlSession
.
close
();
}
}
},
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
});
}
/**
...
...
@@ -196,13 +163,8 @@ public class SelectSelectivePluginTest {
@Test
public
void
testSelectOneByExampleSelective
()
throws
Exception
{
// 没有配置SelectOneByExamplePlugin插件时不生成对应方法
DBHelper
.
cleanDao
();
List
<
String
>
warnings
=
new
ArrayList
<>();
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/SelectSelectivePlugin/mybatis-generator.xml"
));
DefaultShellCallback
shellCallback
=
new
DefaultShellCallback
(
true
);
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
shellCallback
,
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
false
);
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/SelectSelectivePlugin/mybatis-generator.xml"
);
MyBatisGenerator
myBatisGenerator
=
tool
.
generate
();
List
<
GeneratedJavaFile
>
list
=
myBatisGenerator
.
getGeneratedJavaFiles
();
for
(
GeneratedJavaFile
file
:
list
)
{
if
(
file
.
getFileName
().
equals
(
"TbMapper.java"
))
{
...
...
@@ -211,24 +173,20 @@ public class SelectSelectivePluginTest {
}
// 配置了SelectOneByExamplePlugin
DBHelper
.
cleanDao
();
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/SelectSelectivePlugin/mybatis-generator-with-SelectOneByExamplePlugin.xml"
));
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
AbstractShellCallback
(
true
)
{
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/SelectSelectivePlugin/mybatis-generator-with-SelectOneByExamplePlugin.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
ClassLoader
loader
)
{
SqlSession
sqlSession
=
null
;
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
{
try
{
// 1. 测试sql
sqlSession
=
helper
.
getSqlSession
();
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.TbMapper"
)));
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.TbExample"
);
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
packagz
+
"
.TbExample"
);
ObjectUtil
criteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andIdEqualTo"
,
3
l
);
tbExample
.
set
(
"orderByClause"
,
"field2 asc"
);
ObjectUtil
columnField1
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.Tb$Column#field1"
);
ObjectUtil
columnField1
=
new
ObjectUtil
(
loader
,
packagz
+
"
.Tb$Column#field1"
);
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object
columns1
=
Array
.
newInstance
(
columnField1
.
getCls
(),
1
);
Array
.
set
(
columns1
,
0
,
columnField1
.
getObject
());
...
...
@@ -245,16 +203,8 @@ public class SelectSelectivePluginTest {
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
Assert
.
assertTrue
(
false
);
}
finally
{
sqlSession
.
close
();
}
}
},
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
}
@AfterClass
public
static
void
clean
()
{
DBHelper
.
reset
();
});
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/tools/AbstractShellCallback.java
View file @
8a15c348
...
...
@@ -16,19 +16,12 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
tools
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.Assert
;
import
org.mybatis.generator.api.ShellCallback
;
import
org.mybatis.generator.exception.ShellException
;
import
javax.tools.JavaCompiler
;
import
javax.tools.JavaFileObject
;
import
javax.tools.StandardJavaFileManager
;
import
javax.tools.ToolProvider
;
import
java.io.File
;
import
java.io.FileReader
;
import
java.io.FileWriter
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.StringTokenizer
;
import
static
org
.
mybatis
.
generator
.
internal
.
util
.
messages
.
Messages
.
getString
;
...
...
@@ -42,18 +35,15 @@ import static org.mybatis.generator.internal.util.messages.Messages.getString;
* ---------------------------------------------------------------------------
*/
public
abstract
class
AbstractShellCallback
implements
ShellCallback
{
/**
* The overwrite.
*/
private
boolean
overwrite
;
private
MyBatisGeneratorTool
tool
;
// MyBatisGenerator 工具
/**
* Instantiates a new default shell callback.
* @param overwrite the overwrite
* Setter method for property <tt>tool</tt>.
* @param tool value to be assigned to property tool
* @author hewei
*/
public
AbstractShellCallback
(
boolean
overwrite
)
{
super
();
this
.
overwrite
=
overwrite
;
public
void
setTool
(
MyBatisGeneratorTool
tool
)
{
this
.
tool
=
tool
;
}
/**
...
...
@@ -62,104 +52,32 @@ public abstract class AbstractShellCallback implements ShellCallback {
*/
@Override
public
void
refreshProject
(
String
project
)
{
File
daoDir
=
new
File
(
project
+
"/com/itfsw/mybatis/generator/plugins/dao"
);
List
<
File
>
files
=
getJavaFiles
(
daoDir
);
if
(!
files
.
isEmpty
())
{
compileJavaFiles
(
files
);
copyMappings
(
daoDir
,
project
);
}
reloadProject
(
this
.
getClass
().
getClassLoader
());
}
/**
* 拷贝xml
* @param file
* @param project
*/
private
void
copyMappings
(
File
file
,
String
project
)
{
if
(
file
.
exists
())
{
File
[]
files
=
file
.
listFiles
();
for
(
File
childFile
:
files
)
{
if
(
childFile
.
isDirectory
())
{
copyMappings
(
childFile
,
project
);
}
else
if
(
childFile
.
getName
().
endsWith
(
".xml"
))
{
try
{
FileReader
fileReader
=
new
FileReader
(
childFile
);
// 目标路径
String
path
=
childFile
.
getPath
();
path
=
path
.
replaceAll
(
"\\\\"
,
"/"
);
String
target
=
this
.
getClass
().
getClassLoader
().
getResource
(
""
).
getPath
()
+
path
.
replace
(
project
,
""
);
File
targetFile
=
new
File
(
target
);
if
(!
targetFile
.
getParentFile
().
exists
()){
targetFile
.
getParentFile
().
mkdirs
();
}
targetFile
.
createNewFile
();
FileWriter
fileWriter
=
new
FileWriter
(
targetFile
);
int
ch
=
0
;
while
((
ch
=
fileReader
.
read
())
!=
-
1
)
{
fileWriter
.
write
(
ch
);
}
fileReader
.
close
();
fileWriter
.
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
Assert
.
assertTrue
(
false
);
}
}
}
SqlSession
sqlSession
=
null
;
try
{
// 编译项目
sqlSession
=
tool
.
compile
();
reloadProject
(
sqlSession
,
tool
.
getTargetClassLoader
(),
tool
.
getTargetPackage
());
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
Assert
.
assertTrue
(
false
);
}
finally
{
sqlSession
.
close
();
}
}
/**
* 动态编译java文件
* @param files
* 重载项目
*
* @param sqlSession
* @param loader
* @param packagz
*/
private
void
compileJavaFiles
(
List
<
File
>
files
)
{
JavaCompiler
compiler
=
ToolProvider
.
getSystemJavaCompiler
();
//获取java文件管理类
StandardJavaFileManager
manager
=
compiler
.
getStandardFileManager
(
null
,
null
,
null
);
//获取java文件对象迭代器
Iterable
<?
extends
JavaFileObject
>
it
=
manager
.
getJavaFileObjectsFromFiles
(
files
);
//设置编译参数
ArrayList
<
String
>
ops
=
new
ArrayList
<>();
ops
.
add
(
"-Xlint:unchecked"
);
// 设置输出目录
ops
.
add
(
"-d"
);
ops
.
add
(
this
.
getClass
().
getClassLoader
().
getResource
(
""
).
getPath
());
//获取编译任务
JavaCompiler
.
CompilationTask
task
=
compiler
.
getTask
(
null
,
manager
,
null
,
ops
,
null
,
it
);
//执行编译任务
task
.
call
();
}
public
abstract
void
reloadProject
(
ClassLoader
loader
);
/**
* 获取JAVA 文件
* @param file
* @return
*/
private
List
<
File
>
getJavaFiles
(
File
file
)
{
List
<
File
>
list
=
new
ArrayList
<>();
if
(
file
.
exists
())
{
File
[]
files
=
file
.
listFiles
();
for
(
File
childFile
:
files
)
{
if
(
childFile
.
isDirectory
())
{
list
.
addAll
(
getJavaFiles
(
childFile
));
}
else
if
(
childFile
.
getName
().
endsWith
(
".java"
))
{
list
.
add
(
childFile
);
}
}
}
return
list
;
}
public
abstract
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
);
/* (non-Javadoc)
* @see org.mybatis.generator.api.ShellCallback#getDirectory(java.lang.String, java.lang.String)
*/
public
File
getDirectory
(
String
targetProject
,
String
targetPackage
)
throws
ShellException
{
public
File
getDirectory
(
String
targetProject
,
String
targetPackage
)
throws
ShellException
{
// targetProject is interpreted as a directory that must exist
//
// targetPackage is interpreted as a sub directory, but in package
...
...
@@ -169,12 +87,11 @@ public abstract class AbstractShellCallback implements ShellCallback {
File
project
=
new
File
(
targetProject
);
if
(!
project
.
isDirectory
())
{
throw
new
ShellException
(
getString
(
"Warning.9"
,
//$NON-NLS-1$
targetProject
));
throw
new
ShellException
(
getString
(
"Warning.9"
,
targetProject
));
}
StringBuilder
sb
=
new
StringBuilder
();
StringTokenizer
st
=
new
StringTokenizer
(
targetPackage
,
"."
);
//$NON-NLS-1$
StringTokenizer
st
=
new
StringTokenizer
(
targetPackage
,
"."
);
while
(
st
.
hasMoreTokens
())
{
sb
.
append
(
st
.
nextToken
());
sb
.
append
(
File
.
separatorChar
);
...
...
@@ -184,8 +101,7 @@ public abstract class AbstractShellCallback implements ShellCallback {
if
(!
directory
.
isDirectory
())
{
boolean
rc
=
directory
.
mkdirs
();
if
(!
rc
)
{
throw
new
ShellException
(
getString
(
"Warning.10"
,
//$NON-NLS-1$
directory
.
getAbsolutePath
()));
throw
new
ShellException
(
getString
(
"Warning.10"
,
directory
.
getAbsolutePath
()));
}
}
...
...
@@ -203,7 +119,7 @@ public abstract class AbstractShellCallback implements ShellCallback {
* @see org.mybatis.generator.api.ShellCallback#isOverwriteEnabled()
*/
public
boolean
isOverwriteEnabled
()
{
return
overwrit
e
;
return
tru
e
;
}
/* (non-Javadoc)
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java
View file @
8a15c348
...
...
@@ -17,15 +17,15 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
tools
;
import
org.apache.ibatis.io.Resources
;
import
org.apache.ibatis.session.SqlSession
;
import
org.apache.ibatis.session.SqlSessionFactory
;
import
org.apache.ibatis.session.SqlSessionFactoryBuilder
;
import
java.io.*
;
import
java.net.URL
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.sql.Connection
;
import
java.sql.
SQLException
;
import
java.sql.
DriverManager
;
import
java.sql.Statement
;
import
java.util.Properties
;
/**
* ---------------------------------------------------------------------------
...
...
@@ -36,64 +36,47 @@ import java.sql.Statement;
* ---------------------------------------------------------------------------
*/
public
class
DBHelper
{
private
static
final
String
MYBATIS_CONFIG
=
"mybatis-config.xml"
;
// 配置文件
p
rivate
static
DBHelper
helper
;
// helper
private
static
final
String
DB_CONFIG
=
"db.properties"
;
p
ublic
static
Properties
properties
;
// 数据库信息
/**
* 构造函数
*/
private
DBHelper
()
{
}
/**
* 获取数据库操作工具
*
* @param initSql
* @return
*/
public
static
DBHelper
getHelper
(
String
initSql
)
throws
IOException
,
SQLException
{
if
(
helper
==
null
){
helper
=
new
DBHelper
();
helper
.
initDB
(
initSql
);
static
{
try
{
// 获取数据库配置信息
properties
=
new
Properties
();
InputStream
inputStream
=
Resources
.
getResourceAsStream
(
DB_CONFIG
);
properties
.
load
(
inputStream
);
inputStream
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
cleanDao
();
return
helper
;
}
/**
*
获取SqlSession
*
创建数据库
*
* @
return
* @throws
IO
Exception
* @
param resource
* @throws Exception
*/
public
SqlSession
getSqlSession
()
throws
IOException
{
InputStream
inputStream
=
Resources
.
getResourceAsStream
(
MYBATIS_CONFIG
);
SqlSessionFactory
sqlSessionFactory
=
new
SqlSessionFactoryBuilder
().
build
(
inputStream
);
inputStream
.
close
();
return
sqlSessionFactory
.
openSession
(
true
);
}
public
static
void
createDB
(
String
resource
)
throws
Exception
{
String
driver
=
properties
.
getProperty
(
"driver"
);
String
url
=
properties
.
getProperty
(
"url"
);
String
username
=
properties
.
getProperty
(
"username"
);
String
password
=
properties
.
getProperty
(
"password"
);
// 获取connection
Class
.
forName
(
driver
);
Connection
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
);
/**
* 初始化数据库
*
* @param initSql
* @throws IOException
* @throws SQLException
*/
private
void
initDB
(
String
initSql
)
throws
IOException
,
SQLException
{
SqlSession
sqlSession
=
this
.
getSqlSession
();
Connection
connection
=
sqlSession
.
getConnection
();
Statement
statement
=
connection
.
createStatement
();
// 获取建表和初始化sql
InputStream
inputStream
=
Resources
.
getResourceAsStream
(
initSql
);
InputStream
inputStream
=
Resources
.
getResourceAsStream
(
resource
);
InputStreamReader
inputStreamReader
=
new
InputStreamReader
(
inputStream
,
"UTF-8"
);
BufferedReader
bufferedReader
=
new
BufferedReader
(
inputStreamReader
);
// 读取sql语句执行
StringBuffer
sb
=
new
StringBuffer
();
String
line
;
while
((
line
=
bufferedReader
.
readLine
())
!=
null
){
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
sb
.
append
(
line
).
append
(
"\n"
);
if
(
line
.
matches
(
".*;$"
)){
if
(
line
.
matches
(
".*;$"
))
{
statement
.
execute
(
sb
.
toString
());
sb
.
setLength
(
0
);
}
...
...
@@ -103,47 +86,6 @@ public class DBHelper {
inputStream
.
close
();
statement
.
close
();
connection
.
close
();
sqlSession
.
close
();
}
/**
* 重置
*/
public
static
void
reset
(){
helper
=
null
;
cleanDao
();
}
/**
* 清理Dao空间
*/
public
static
void
cleanDao
(){
delDir
(
new
File
(
"src/test/java/com/itfsw/mybatis/generator/plugins/dao"
));
// 清理Dao class目录
URL
daoClass
=
DBHelper
.
class
.
getClassLoader
().
getResource
(
"com/itfsw/mybatis/generator/plugins/dao"
);
if
(
daoClass
!=
null
){
delDir
(
new
File
(
daoClass
.
getPath
()));
}
}
/**
* 清理工作区间
*
* @param file
*/
private
static
void
delDir
(
File
file
)
{
if
(
file
.
exists
()){
if
(
file
.
isFile
()){
file
.
delete
();
}
else
if
(
file
.
isDirectory
()){
File
[]
files
=
file
.
listFiles
();
for
(
File
file1:
files
)
{
delDir
(
file1
);
}
file
.
delete
();
}
}
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/tools/MyBatisGeneratorTool.java
0 → 100644
View file @
8a15c348
/*
* 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.
*/
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
tools
;
import
org.apache.ibatis.datasource.pooled.PooledDataSourceFactory
;
import
org.apache.ibatis.io.Resources
;
import
org.apache.ibatis.mapping.Environment
;
import
org.apache.ibatis.session.SqlSession
;
import
org.apache.ibatis.session.SqlSessionFactory
;
import
org.apache.ibatis.session.SqlSessionFactoryBuilder
;
import
org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory
;
import
org.mybatis.generator.api.MyBatisGenerator
;
import
org.mybatis.generator.config.Configuration
;
import
org.mybatis.generator.config.Context
;
import
org.mybatis.generator.config.xml.ConfigurationParser
;
import
org.mybatis.generator.internal.DefaultShellCallback
;
import
javax.sql.DataSource
;
import
javax.tools.JavaCompiler
;
import
javax.tools.JavaFileObject
;
import
javax.tools.StandardJavaFileManager
;
import
javax.tools.ToolProvider
;
import
java.io.File
;
import
java.io.IOException
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.net.URLClassLoader
;
import
java.sql.SQLException
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/7/4 16:14
* ---------------------------------------------------------------------------
*/
public
class
MyBatisGeneratorTool
{
public
final
static
String
DAO_PACKAGE
=
"com.itfsw.mybatis.generator.plugins.dao"
;
// dao package
private
List
<
String
>
warnings
;
// 提示信息
private
Configuration
config
;
// 配置信息
private
String
targetProject
;
// 目标
private
String
targetPackage
;
// package
/**
* 创建
*
* @param resource
* @return
*/
public
static
MyBatisGeneratorTool
create
(
String
resource
)
throws
Exception
{
MyBatisGeneratorTool
tool
=
new
MyBatisGeneratorTool
();
tool
.
warnings
=
new
ArrayList
<>();
// MyBatisGenerator 创建
ConfigurationParser
cp
=
new
ConfigurationParser
(
tool
.
warnings
);
tool
.
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
resource
));
// 修正配置目标
tool
.
fixConfigToTarget
();
return
tool
;
}
/**
* 执行MyBatisGenerator
*
* @param callback
* @return
* @throws SQLException
* @throws IOException
* @throws InterruptedException
*/
public
MyBatisGenerator
generate
(
AbstractShellCallback
callback
)
throws
Exception
{
callback
.
setTool
(
this
);
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
callback
,
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
return
myBatisGenerator
;
}
/**
* 执行MyBatisGenerator(不生成文件)
*
* @return
* @throws SQLException
* @throws IOException
* @throws InterruptedException
*/
public
MyBatisGenerator
generate
()
throws
Exception
{
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
DefaultShellCallback
(
true
),
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
false
);
return
myBatisGenerator
;
}
/**
* 编译项目并返回 SqlSession
*
* @return
*/
public
SqlSession
compile
()
throws
IOException
,
ClassNotFoundException
{
// 动态编译java文件
String
target
=
targetProject
+
targetPackage
.
replaceAll
(
"\\."
,
"/"
);
List
<
File
>
javaFiles
=
getGeneratedFiles
(
new
File
(
target
),
".java"
);
compileJavaFiles
(
javaFiles
);
return
getSqlSession
();
}
/**
* 获取目标目录的ClassLoader
* @return
*/
public
ClassLoader
getTargetClassLoader
()
throws
MalformedURLException
{
return
URLClassLoader
.
newInstance
(
new
URL
[]{
new
File
(
targetProject
).
toURI
().
toURL
()
});
}
/**
* 获取SqlSession
* @return
* @throws IOException
*/
public
SqlSession
getSqlSession
()
throws
IOException
,
ClassNotFoundException
{
org
.
apache
.
ibatis
.
session
.
Configuration
config
=
new
org
.
apache
.
ibatis
.
session
.
Configuration
();
config
.
setCallSettersOnNulls
(
true
);
// 设计null调用setter方法
// 设置mapper
config
.
addMappers
(
targetPackage
);
// 设置数据源,事务
PooledDataSourceFactory
dataSourceFactory
=
new
PooledDataSourceFactory
();
dataSourceFactory
.
setProperties
(
DBHelper
.
properties
);
DataSource
dataSource
=
dataSourceFactory
.
getDataSource
();
JdbcTransactionFactory
transactionFactory
=
new
JdbcTransactionFactory
();
Environment
environment
=
new
Environment
(
"test"
,
transactionFactory
,
dataSource
);
config
.
setEnvironment
(
environment
);
SqlSessionFactory
sqlSessionFactory
=
new
SqlSessionFactoryBuilder
().
build
(
config
);
return
sqlSessionFactory
.
openSession
(
true
);
}
/**
* 动态编译java文件
* @param files
*/
private
void
compileJavaFiles
(
List
<
File
>
files
)
{
JavaCompiler
compiler
=
ToolProvider
.
getSystemJavaCompiler
();
//获取java文件管理类
StandardJavaFileManager
manager
=
compiler
.
getStandardFileManager
(
null
,
null
,
null
);
//获取java文件对象迭代器
Iterable
<?
extends
JavaFileObject
>
it
=
manager
.
getJavaFileObjectsFromFiles
(
files
);
//设置编译参数
ArrayList
<
String
>
ops
=
new
ArrayList
<>();
ops
.
add
(
"-Xlint:unchecked"
);
//获取编译任务
JavaCompiler
.
CompilationTask
task
=
compiler
.
getTask
(
null
,
manager
,
null
,
ops
,
null
,
it
);
//执行编译任务
task
.
call
();
}
/**
* 获取指定后缀的文件
*
* @param file
* @return
*/
private
List
<
File
>
getGeneratedFiles
(
File
file
,
String
ext
)
{
List
<
File
>
list
=
new
ArrayList
<>();
if
(
file
.
exists
())
{
File
[]
files
=
file
.
listFiles
();
for
(
File
childFile
:
files
)
{
if
(
childFile
.
isDirectory
())
{
list
.
addAll
(
getGeneratedFiles
(
childFile
,
ext
));
}
else
if
(
childFile
.
getName
().
endsWith
(
ext
))
{
list
.
add
(
childFile
);
}
}
}
return
list
;
}
/**
* 修正配置到指定target
*/
private
void
fixConfigToTarget
(){
this
.
targetProject
=
this
.
getClass
().
getClassLoader
().
getResource
(
""
).
getPath
();
this
.
targetPackage
=
DAO_PACKAGE
+
".s"
+
new
SimpleDateFormat
(
"yyyyMMddHHmmssSSS"
).
format
(
new
Date
());
for
(
Context
context
:
config
.
getContexts
()){
context
.
getJavaModelGeneratorConfiguration
().
setTargetProject
(
targetProject
);
context
.
getJavaModelGeneratorConfiguration
().
setTargetPackage
(
targetPackage
);
context
.
getSqlMapGeneratorConfiguration
().
setTargetProject
(
targetProject
);
context
.
getSqlMapGeneratorConfiguration
().
setTargetPackage
(
targetPackage
);
context
.
getJavaClientGeneratorConfiguration
().
setTargetProject
(
targetProject
);
context
.
getJavaClientGeneratorConfiguration
().
setTargetPackage
(
targetPackage
);
}
}
/**
* Getter method for property <tt>warnings</tt>.
* @return property value of warnings
* @author hewei
*/
public
List
<
String
>
getWarnings
()
{
return
warnings
;
}
/**
* Getter method for property <tt>config</tt>.
* @return property value of config
* @author hewei
*/
public
Configuration
getConfig
()
{
return
config
;
}
/**
* Getter method for property <tt>targetPackage</tt>.
* @return property value of targetPackage
* @author hewei
*/
public
String
getTargetPackage
()
{
return
targetPackage
;
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/tools/Util.java
View file @
8a15c348
...
...
@@ -16,6 +16,7 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
tools
;
import
java.io.*
;
import
java.lang.reflect.ParameterizedType
;
import
java.lang.reflect.Type
;
...
...
@@ -31,17 +32,72 @@ public class Util {
/**
* 获取List 泛型参数
*
* @param type
* @return
*/
public
static
String
getListActualType
(
Type
type
){
if
(
type
instanceof
ParameterizedType
)
{
Type
[]
actualTypeArguments
=
((
ParameterizedType
)
type
).
getActualTypeArguments
();
if
(
actualTypeArguments
.
length
==
1
){
public
static
String
getListActualType
(
Type
type
)
{
if
(
type
instanceof
ParameterizedType
)
{
Type
[]
actualTypeArguments
=
((
ParameterizedType
)
type
).
getActualTypeArguments
();
if
(
actualTypeArguments
.
length
==
1
)
{
return
actualTypeArguments
[
0
].
getTypeName
();
}
}
return
null
;
}
/**
* 文件拷贝
* @param src
* @param dist
* @param overwrite
* @return
*/
public
static
int
copyFile
(
File
src
,
File
dist
,
boolean
overwrite
)
throws
IOException
{
if
(
src
.
exists
()
&&
src
.
isFile
())
{
if
(
dist
.
exists
()){
if
(
overwrite
){
dist
.
delete
();
}
else
{
throw
new
IOException
(
"目标文件已经存在:"
+
dist
.
getPath
());
}
}
// 创建目标文件夹
if
(!
dist
.
getParentFile
().
exists
()
||
dist
.
getParentFile
().
isFile
())
{
dist
.
mkdirs
();
}
// 创建目标文件
dist
.
createNewFile
();
// 进行拷贝操作
int
byteCount
=
0
;
InputStream
in
=
null
;
OutputStream
out
=
null
;
try
{
in
=
new
FileInputStream
(
src
);
out
=
new
FileOutputStream
(
dist
);
byte
[]
buffer
=
new
byte
[
4096
];
int
bytesRead1
;
for
(
boolean
bytesRead
=
true
;
(
bytesRead1
=
in
.
read
(
buffer
))
!=
-
1
;
byteCount
+=
bytesRead1
)
{
out
.
write
(
buffer
,
0
,
bytesRead1
);
}
out
.
flush
();
}
catch
(
Exception
e
){
out
.
close
();
dist
.
delete
();
}
finally
{
if
(
in
!=
null
)
{
in
.
close
();
}
if
(
out
!=
null
)
{
out
.
close
();
}
}
return
byteCount
;
}
throw
new
IOException
(
"没有找到对应文件:"
+
src
);
}
}
src/test/resources/db.properties
View file @
8a15c348
...
...
@@ -14,7 +14,7 @@
# limitations under the License.
#
db.driver
=
com.mysql.jdbc.Driver
db.url
=
jdbc:mysql://localhost:3306/mybatis-generator-plugin?characterEncoding=UTF-8&allowMultiQueries=true
db.username
=
root
db.password
=
root
\ No newline at end of file
driver
=
com.mysql.jdbc.Driver
url
=
jdbc:mysql://localhost:3306/mybatis-generator-plugin?characterEncoding=UTF-8&allowMultiQueries=true
username
=
root
password
=
root
\ No newline at end of file
src/test/resources/mybatis-config.xml
View file @
8a15c348
...
...
@@ -38,6 +38,6 @@
<mappers>
<!-- Mapper扫描包,必须同目录同名称下-->
<
package
name=
"com.itfsw.mybatis.generator.plugins.dao
"
/>
<
mapper
resource=
"E:\work_java\mybatis-generator-plugin\target\test-tmp\123456\com\itfsw\mybatis\generator\plugins\dao\TbMapper.xml
"
/>
</mappers>
</configuration>
\ No newline at end of file
src/test/resources/scripts/BatchInsertPlugin/mybatis-generator-with-error-driver.xml
View file @
8a15c348
...
...
@@ -33,19 +33,19 @@
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao.model"
targetProject=
"src/test/java
"
>
<javaModelGenerator
targetPackage=
"
"
targetProject=
"
"
>
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
/>
<sqlMapGenerator
targetPackage=
"
"
targetProject=
"
"
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
type=
"XMLMAPPER"
/>
<javaClientGenerator
targetPackage=
"
"
targetProject=
"
"
type=
"XMLMAPPER"
/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table
tableName=
"tb"
>
...
...
src/test/resources/scripts/BatchInsertPlugin/mybatis-generator-without-model-column-plugin.xml
View file @
8a15c348
...
...
@@ -27,23 +27,23 @@
<plugin
type=
"com.itfsw.mybatis.generator.plugins.BatchInsertPlugin"
/>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${d
b.driver}"
connectionURL=
"${db.url}"
userId=
"${db.username}"
password=
"${db.
password}"
/>
<jdbcConnection
driverClass=
"${d
river}"
connectionURL=
"${url}"
userId=
"${username}"
password=
"${
password}"
/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao.model"
targetProject=
"src/test/java
"
>
<javaModelGenerator
targetPackage=
"
"
targetProject=
"
"
>
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
/>
<sqlMapGenerator
targetPackage=
"
"
targetProject=
"
"
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
type=
"XMLMAPPER"
/>
<javaClientGenerator
targetPackage=
"
"
targetProject=
"
"
type=
"XMLMAPPER"
/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table
tableName=
"tb"
>
...
...
src/test/resources/scripts/BatchInsertPlugin/mybatis-generator.xml
View file @
8a15c348
...
...
@@ -29,23 +29,23 @@
<plugin
type=
"com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"
/>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${d
b.driver}"
connectionURL=
"${db.url}"
userId=
"${db.username}"
password=
"${db.
password}"
/>
<jdbcConnection
driverClass=
"${d
river}"
connectionURL=
"${url}"
userId=
"${username}"
password=
"${
password}"
/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao.model"
targetProject=
"src/test/java
"
>
<javaModelGenerator
targetPackage=
"
"
targetProject=
"
"
>
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
/>
<sqlMapGenerator
targetPackage=
"
"
targetProject=
"
"
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
type=
"XMLMAPPER"
/>
<javaClientGenerator
targetPackage=
"
"
targetProject=
"
"
type=
"XMLMAPPER"
/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table
tableName=
"tb"
>
...
...
src/test/resources/scripts/ExampleTargetPlugin/mybatis-generator-without-plugin.xml
View file @
8a15c348
...
...
@@ -25,23 +25,23 @@
<!-- 插件 -->
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${d
b.driver}"
connectionURL=
"${db.url}"
userId=
"${db.username}"
password=
"${db.
password}"
/>
<jdbcConnection
driverClass=
"${d
river}"
connectionURL=
"${url}"
userId=
"${username}"
password=
"${
password}"
/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao.model"
targetProject=
"src/test/java
"
>
<javaModelGenerator
targetPackage=
"
"
targetProject=
"
"
>
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
/>
<sqlMapGenerator
targetPackage=
"
"
targetProject=
"
"
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
type=
"XMLMAPPER"
/>
<javaClientGenerator
targetPackage=
"
"
targetProject=
"
"
type=
"XMLMAPPER"
/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table
tableName=
"tb"
>
...
...
src/test/resources/scripts/ExampleTargetPlugin/mybatis-generator.xml
View file @
8a15c348
...
...
@@ -30,23 +30,23 @@
</plugin>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${d
b.driver}"
connectionURL=
"${db.url}"
userId=
"${db.username}"
password=
"${db.
password}"
/>
<jdbcConnection
driverClass=
"${d
river}"
connectionURL=
"${url}"
userId=
"${username}"
password=
"${
password}"
/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao.model"
targetProject=
"src/test/java
"
>
<javaModelGenerator
targetPackage=
"
"
targetProject=
"
"
>
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
/>
<sqlMapGenerator
targetPackage=
"
"
targetProject=
"
"
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
type=
"XMLMAPPER"
/>
<javaClientGenerator
targetPackage=
"
"
targetProject=
"
"
type=
"XMLMAPPER"
/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table
tableName=
"tb"
>
...
...
src/test/resources/scripts/SelectSelectivePlugin/mybatis-generator-with-SelectOneByExamplePlugin.xml
View file @
8a15c348
...
...
@@ -31,23 +31,23 @@
<plugin
type=
"com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"
/>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${d
b.driver}"
connectionURL=
"${db.url}"
userId=
"${db.username}"
password=
"${db.
password}"
/>
<jdbcConnection
driverClass=
"${d
river}"
connectionURL=
"${url}"
userId=
"${username}"
password=
"${
password}"
/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao.model"
targetProject=
"src/test/java
"
>
<javaModelGenerator
targetPackage=
"
"
targetProject=
"
"
>
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
/>
<sqlMapGenerator
targetPackage=
"
"
targetProject=
"
"
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
type=
"XMLMAPPER"
/>
<javaClientGenerator
targetPackage=
"
"
targetProject=
"
"
type=
"XMLMAPPER"
/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table
tableName=
"tb"
>
...
...
src/test/resources/scripts/SelectSelectivePlugin/mybatis-generator.xml
View file @
8a15c348
...
...
@@ -29,23 +29,23 @@
<plugin
type=
"com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"
/>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${d
b.driver}"
connectionURL=
"${db.url}"
userId=
"${db.username}"
password=
"${db.
password}"
/>
<jdbcConnection
driverClass=
"${d
river}"
connectionURL=
"${url}"
userId=
"${username}"
password=
"${
password}"
/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao.model"
targetProject=
"src/test/java
"
>
<javaModelGenerator
targetPackage=
"
"
targetProject=
"
"
>
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
/>
<sqlMapGenerator
targetPackage=
"
"
targetProject=
"
"
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
type=
"XMLMAPPER"
/>
<javaClientGenerator
targetPackage=
"
"
targetProject=
"
"
type=
"XMLMAPPER"
/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table
tableName=
"tb"
>
...
...
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