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 @@
...
@@ -12,5 +12,4 @@
hs_err_pid*
hs_err_pid*
.idea/
.idea/
mybatis-generator-plugin.iml
mybatis-generator-plugin.iml
target/
target/
src/test/java/com/itfsw/mybatis/generator/plugins/dao/
\ No newline at end of file
\ No newline at end of file
src/test/java/com/itfsw/mybatis/generator/plugins/BatchInsertPluginTest.java
View file @
8a15c348
...
@@ -17,18 +17,12 @@
...
@@ -17,18 +17,12 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.tools.*
;
import
com.itfsw.mybatis.generator.plugins.tools.*
;
import
org.apache.ibatis.io.Resources
;
import
org.apache.ibatis.session.SqlSession
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.AfterClass
;
import
org.junit.Assert
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Before
Class
;
import
org.junit.Test
;
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.InvalidConfigurationException
;
import
org.mybatis.generator.exception.XMLParserException
;
import
org.mybatis.generator.exception.XMLParserException
;
import
org.mybatis.generator.internal.DefaultShellCallback
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.lang.reflect.Array
;
import
java.lang.reflect.Array
;
...
@@ -46,16 +40,15 @@ import java.util.List;
...
@@ -46,16 +40,15 @@ import java.util.List;
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
*/
*/
public
class
BatchInsertPluginTest
{
public
class
BatchInsertPluginTest
{
private
DBHelper
helper
;
/**
/**
* 初始化
* 初始化
* @throws IOException
* @throws IOException
* @throws SQLException
* @throws SQLException
*/
*/
@Before
@Before
Class
public
void
init
()
throws
IOException
,
SQL
Exception
{
public
static
void
init
()
throws
Exception
{
helper
=
DBHelper
.
getHelper
(
"scripts/BatchInsertPlugin/init.sql"
);
DBHelper
.
createDB
(
"scripts/BatchInsertPlugin/init.sql"
);
}
}
/**
/**
...
@@ -67,17 +60,12 @@ public class BatchInsertPluginTest {
...
@@ -67,17 +60,12 @@ public class BatchInsertPluginTest {
* @throws InterruptedException
* @throws InterruptedException
*/
*/
@Test
@Test
public
void
testWarnings1
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
SQLException
,
InterruptedException
{
public
void
testWarnings1
()
throws
Exception
{
List
<
String
>
warnings
=
new
ArrayList
<>();
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/BatchInsertPlugin/mybatis-generator-without-model-column-plugin.xml"
);
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
tool
.
generate
();
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/BatchInsertPlugin/mybatis-generator-without-model-column-plugin.xml"
));
DefaultShellCallback
shellCallback
=
new
DefaultShellCallback
(
true
);
Assert
.
assertTrue
(
tool
.
getWarnings
().
size
()
==
2
);
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
shellCallback
,
warnings
);
Assert
.
assertEquals
(
tool
.
getWarnings
().
get
(
0
),
"itfsw:插件com.itfsw.mybatis.generator.plugins.BatchInsertPlugin插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!"
);
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插件使用!"
);
}
}
/**
/**
...
@@ -89,17 +77,12 @@ public class BatchInsertPluginTest {
...
@@ -89,17 +77,12 @@ public class BatchInsertPluginTest {
* @throws InterruptedException
* @throws InterruptedException
*/
*/
@Test
@Test
public
void
testWarnings2
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
SQLException
,
InterruptedException
{
public
void
testWarnings2
()
throws
Exception
{
List
<
String
>
warnings
=
new
ArrayList
<>();
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/BatchInsertPlugin/mybatis-generator-with-error-driver.xml"
);
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
tool
.
generate
();
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
);
Assert
.
assertTrue
(
warnings
.
size
()
==
3
);
Assert
.
assertTrue
(
tool
.
getWarnings
()
.
size
()
==
3
);
Assert
.
assertEquals
(
warnings
.
get
(
1
),
"itfsw:插件com.itfsw.mybatis.generator.plugins.BatchInsertPlugin插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键!"
);
Assert
.
assertEquals
(
tool
.
getWarnings
()
.
get
(
1
),
"itfsw:插件com.itfsw.mybatis.generator.plugins.BatchInsertPlugin插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键!"
);
}
}
/**
/**
...
@@ -113,42 +96,39 @@ public class BatchInsertPluginTest {
...
@@ -113,42 +96,39 @@ public class BatchInsertPluginTest {
* @throws NoSuchMethodException
* @throws NoSuchMethodException
*/
*/
@Test
@Test
public
void
testMethods
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
SQLException
,
InterruptedException
,
ClassNotFoundException
,
NoSuchMethodException
{
public
void
testMethods
()
throws
Exception
{
List
<
String
>
warnings
=
new
ArrayList
<>();
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/BatchInsertPlugin/mybatis-generator.xml"
);
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
tool
.
generate
(
new
AbstractShellCallback
()
{
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/BatchInsertPlugin/mybatis-generator.xml"
));
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
AbstractShellCallback
(
true
)
{
@Override
@Override
public
void
reloadProject
(
ClassLoader
loader
)
{
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
{
try
{
try
{
// 1. 普通Mapper参数中List泛型为普通Model
// 1. 普通Mapper参数中List泛型为普通Model
Class
clsTbMapper
=
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao
.TbMapper"
);
Class
clsTbMapper
=
loader
.
loadClass
(
packagz
+
"
.TbMapper"
);
int
count
=
0
;
int
count
=
0
;
for
(
Method
method
:
clsTbMapper
.
getDeclaredMethods
())
{
for
(
Method
method
:
clsTbMapper
.
getDeclaredMethods
())
{
if
(
method
.
getName
().
equals
(
"batchInsert"
))
{
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
++;
count
++;
}
}
if
(
method
.
getName
().
equals
(
"batchInsertSelective"
))
{
if
(
method
.
getName
().
equals
(
"batchInsertSelective"
))
{
Assert
.
assertEquals
(
Util
.
getListActualType
(
method
.
getGenericParameterTypes
()[
0
]),
"com.itfsw.mybatis.generator.plugins.dao.model
.Tb"
);
Assert
.
assertEquals
(
Util
.
getListActualType
(
method
.
getGenericParameterTypes
()[
0
]),
packagz
+
"
.Tb"
);
Assert
.
assertEquals
(
method
.
getGenericParameterTypes
()[
1
].
getTypeName
(),
"com.itfsw.mybatis.generator.plugins.dao.model
.Tb$Column[]"
);
Assert
.
assertEquals
(
method
.
getGenericParameterTypes
()[
1
].
getTypeName
(),
packagz
+
"
.Tb$Column[]"
);
count
++;
count
++;
}
}
}
}
Assert
.
assertEquals
(
count
,
2
);
Assert
.
assertEquals
(
count
,
2
);
// 2. 带有WithBlobs
// 2. 带有WithBlobs
Class
clsTbBlobsMapper
=
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao
.TbBlobsMapper"
);
Class
clsTbBlobsMapper
=
loader
.
loadClass
(
packagz
+
"
.TbBlobsMapper"
);
count
=
0
;
count
=
0
;
for
(
Method
method
:
clsTbBlobsMapper
.
getDeclaredMethods
())
{
for
(
Method
method
:
clsTbBlobsMapper
.
getDeclaredMethods
())
{
if
(
method
.
getName
().
equals
(
"batchInsert"
))
{
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
++;
count
++;
}
}
if
(
method
.
getName
().
equals
(
"batchInsertSelective"
))
{
if
(
method
.
getName
().
equals
(
"batchInsertSelective"
))
{
Assert
.
assertEquals
(
Util
.
getListActualType
(
method
.
getGenericParameterTypes
()[
0
]),
"com.itfsw.mybatis.generator.plugins.dao.model
.TbBlobsWithBLOBs"
);
Assert
.
assertEquals
(
Util
.
getListActualType
(
method
.
getGenericParameterTypes
()[
0
]),
packagz
+
"
.TbBlobsWithBLOBs"
);
Assert
.
assertEquals
(
method
.
getGenericParameterTypes
()[
1
].
getTypeName
(),
"com.itfsw.mybatis.generator.plugins.dao.model
.TbBlobsWithBLOBs$Column[]"
);
Assert
.
assertEquals
(
method
.
getGenericParameterTypes
()[
1
].
getTypeName
(),
packagz
+
"
.TbBlobsWithBLOBs$Column[]"
);
count
++;
count
++;
}
}
}
}
...
@@ -158,8 +138,7 @@ public class BatchInsertPluginTest {
...
@@ -158,8 +138,7 @@ public class BatchInsertPluginTest {
Assert
.
assertTrue
(
false
);
Assert
.
assertTrue
(
false
);
}
}
}
}
},
warnings
);
});
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
}
}
/**
/**
...
@@ -167,27 +146,21 @@ public class BatchInsertPluginTest {
...
@@ -167,27 +146,21 @@ public class BatchInsertPluginTest {
*/
*/
@Test
@Test
public
void
testBatchInsert
()
throws
Exception
{
public
void
testBatchInsert
()
throws
Exception
{
DBHelper
.
cleanDao
();
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/BatchInsertPlugin/mybatis-generator.xml"
);
List
<
String
>
warnings
=
new
ArrayList
<>();
tool
.
generate
(
new
AbstractShellCallback
()
{
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/BatchInsertPlugin/mybatis-generator.xml"
));
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
AbstractShellCallback
(
true
)
{
@Override
@Override
public
void
reloadProject
(
ClassLoader
loader
)
{
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
{
SqlSession
sqlSession
=
null
;
try
{
try
{
// 1. 测试sql
// 1. 测试sql
sqlSession
=
helper
.
getSqlSession
();
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.TbMapper"
)));
List
<
Object
>
params
=
new
ArrayList
<>();
List
<
Object
>
params
=
new
ArrayList
<>();
params
.
add
(
params
.
add
(
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.Tb"
)
new
ObjectUtil
(
loader
,
packagz
+
"
.Tb"
)
.
set
(
"field1"
,
"test"
)
.
set
(
"field1"
,
"test"
)
.
getObject
()
.
getObject
()
);
);
params
.
add
(
params
.
add
(
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.Tb"
)
new
ObjectUtil
(
loader
,
packagz
+
"
.Tb"
)
.
set
(
"field1"
,
"test"
)
.
set
(
"field1"
,
"test"
)
.
set
(
"field2"
,
1
)
.
set
(
"field2"
,
1
)
.
getObject
()
.
getObject
()
...
@@ -200,12 +173,9 @@ public class BatchInsertPluginTest {
...
@@ -200,12 +173,9 @@ public class BatchInsertPluginTest {
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
Assert
.
assertTrue
(
false
);
Assert
.
assertTrue
(
false
);
}
finally
{
sqlSession
.
close
();
}
}
}
}
},
warnings
);
});
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
}
}
/**
/**
...
@@ -213,32 +183,26 @@ public class BatchInsertPluginTest {
...
@@ -213,32 +183,26 @@ public class BatchInsertPluginTest {
*/
*/
@Test
@Test
public
void
testBatchInsertSelective
()
throws
Exception
{
public
void
testBatchInsertSelective
()
throws
Exception
{
DBHelper
.
cleanDao
();
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/BatchInsertPlugin/mybatis-generator.xml"
);
List
<
String
>
warnings
=
new
ArrayList
<>();
tool
.
generate
(
new
AbstractShellCallback
()
{
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/BatchInsertPlugin/mybatis-generator.xml"
));
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
AbstractShellCallback
(
true
)
{
@Override
@Override
public
void
reloadProject
(
ClassLoader
loader
)
{
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
{
SqlSession
sqlSession
=
null
;
try
{
try
{
// 1. 测试sql
// 1. 测试sql
sqlSession
=
helper
.
getSqlSession
();
ObjectUtil
tbBlobsMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbBlobsMapper"
)));
ObjectUtil
tbBlobsMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.TbBlobsMapper"
)));
List
<
Object
>
params
=
new
ArrayList
<>();
List
<
Object
>
params
=
new
ArrayList
<>();
params
.
add
(
params
.
add
(
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.TbBlobsWithBLOBs"
)
new
ObjectUtil
(
loader
,
packagz
+
"
.TbBlobsWithBLOBs"
)
.
set
(
"field1"
,
"test"
)
.
set
(
"field1"
,
"test"
)
.
getObject
()
.
getObject
()
);
);
params
.
add
(
params
.
add
(
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.TbBlobsWithBLOBs"
)
new
ObjectUtil
(
loader
,
packagz
+
"
.TbBlobsWithBLOBs"
)
.
set
(
"field1"
,
"test"
)
.
set
(
"field1"
,
"test"
)
.
set
(
"field2"
,
"test123"
)
.
set
(
"field2"
,
"test123"
)
.
getObject
()
.
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对了可变参数会检查类型!
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object
columns
=
Array
.
newInstance
(
columnField2
.
getCls
(),
1
);
Object
columns
=
Array
.
newInstance
(
columnField2
.
getCls
(),
1
);
Array
.
set
(
columns
,
0
,
columnField2
.
getObject
());
Array
.
set
(
columns
,
0
,
columnField2
.
getObject
());
...
@@ -255,12 +219,6 @@ public class BatchInsertPluginTest {
...
@@ -255,12 +219,6 @@ public class BatchInsertPluginTest {
sqlSession
.
close
();
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 @@
...
@@ -17,15 +17,11 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.tools.DBHelper
;
import
com.itfsw.mybatis.generator.plugins.tools.DBHelper
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.Assert
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
org.junit.Test
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.sql.Connection
;
import
java.sql.*
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
/**
/**
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
...
@@ -43,10 +39,17 @@ public class DBHelperTest {
...
@@ -43,10 +39,17 @@ public class DBHelperTest {
* @throws SQLException
* @throws SQLException
*/
*/
@Test
@Test
public
void
testGetSqlSession
()
throws
IOException
,
SQLException
{
public
void
testGetSqlSession
()
throws
Exception
{
DBHelper
helper
=
DBHelper
.
getHelper
(
"scripts/test_init.sql"
);
DBHelper
.
createDB
(
"scripts/test_init.sql"
);
SqlSession
sqlSession
=
helper
.
getSqlSession
();
Connection
connection
=
sqlSession
.
getConnection
();
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
();
Statement
statement
=
connection
.
createStatement
();
// 执行查询
// 执行查询
...
@@ -58,7 +61,5 @@ public class DBHelperTest {
...
@@ -58,7 +61,5 @@ public class DBHelperTest {
statement
.
close
();
statement
.
close
();
connection
.
close
();
connection
.
close
();
sqlSession
.
close
();
DBHelper
.
reset
();
}
}
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/ExampleTargetPluginTest.java
View file @
8a15c348
...
@@ -17,22 +17,15 @@
...
@@ -17,22 +17,15 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.tools.DBHelper
;
import
com.itfsw.mybatis.generator.plugins.tools.DBHelper
;
import
org.apache.ibatis.io.Resources
;
import
com.itfsw.mybatis.generator.plugins.tools.MyBatisGeneratorTool
;
import
org.junit.After
;
import
org.junit.Assert
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Before
Class
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.mybatis.generator.api.GeneratedJavaFile
;
import
org.mybatis.generator.api.GeneratedJavaFile
;
import
org.mybatis.generator.api.MyBatisGenerator
;
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.io.IOException
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
/**
/**
...
@@ -44,44 +37,35 @@ import java.util.List;
...
@@ -44,44 +37,35 @@ import java.util.List;
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
*/
*/
public
class
ExampleTargetPluginTest
{
public
class
ExampleTargetPluginTest
{
private
DBHelper
helper
;
/**
/**
* 初始化
* 初始化
* @throws IOException
* @throws IOException
* @throws SQLException
* @throws SQLException
*/
*/
@Before
@Before
Class
public
void
init
()
throws
IOException
,
SQL
Exception
{
public
static
void
init
()
throws
Exception
{
helper
=
DBHelper
.
getHelper
(
"scripts/ExampleTargetPlugin/init.sql"
);
DBHelper
.
createDB
(
"scripts/ExampleTargetPlugin/init.sql"
);
}
}
@Test
@Test
public
void
testNormalPath
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
SQLException
,
InterruptedException
{
public
void
testNormalPath
()
throws
Exception
{
List
<
String
>
warnings
=
new
ArrayList
<
String
>();
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/ExampleTargetPlugin/mybatis-generator-without-plugin.xml"
);
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
MyBatisGenerator
myBatisGenerator
=
tool
.
generate
();
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/ExampleTargetPlugin/mybatis-generator-without-plugin.xml"
));
DefaultShellCallback
shellCallback
=
new
DefaultShellCallback
(
true
);
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
shellCallback
,
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
false
);
List
<
GeneratedJavaFile
>
list
=
myBatisGenerator
.
getGeneratedJavaFiles
();
List
<
GeneratedJavaFile
>
list
=
myBatisGenerator
.
getGeneratedJavaFiles
();
for
(
GeneratedJavaFile
file
:
list
){
for
(
GeneratedJavaFile
file
:
list
){
if
(
file
.
getFileName
().
equals
(
"TbExample.java"
)){
if
(
file
.
getFileName
().
equals
(
"TbExample.java"
)){
Assert
.
assertEquals
(
file
.
getTargetPackage
(),
"com.itfsw.mybatis.generator.plugins.dao.model"
);
Assert
.
assertEquals
(
file
.
getTargetPackage
(),
tool
.
getTargetPackage
()
);
}
}
}
}
}
}
@Test
@Test
public
void
testConfigPath
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
SQLException
,
InterruptedException
{
public
void
testConfigPath
()
throws
Exception
{
List
<
String
>
warnings
=
new
ArrayList
<
String
>();
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/ExampleTargetPlugin/mybatis-generator.xml"
);
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
MyBatisGenerator
myBatisGenerator
=
tool
.
generate
();
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/ExampleTargetPlugin/mybatis-generator.xml"
));
DefaultShellCallback
shellCallback
=
new
DefaultShellCallback
(
true
);
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
shellCallback
,
warnings
);
myBatisGenerator
.
generate
(
null
,
null
,
null
,
false
);
List
<
GeneratedJavaFile
>
list
=
myBatisGenerator
.
getGeneratedJavaFiles
();
List
<
GeneratedJavaFile
>
list
=
myBatisGenerator
.
getGeneratedJavaFiles
();
for
(
GeneratedJavaFile
file
:
list
){
for
(
GeneratedJavaFile
file
:
list
){
if
(
file
.
getFileName
().
equals
(
"TbExample.java"
)){
if
(
file
.
getFileName
().
equals
(
"TbExample.java"
)){
...
@@ -90,8 +74,4 @@ public class ExampleTargetPluginTest {
...
@@ -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 @@
...
@@ -16,26 +16,15 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.tools.AbstractShellCallback
;
import
com.itfsw.mybatis.generator.plugins.tools.*
;
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
org.apache.ibatis.session.SqlSession
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.AfterClass
;
import
org.junit.Assert
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Before
Class
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.mybatis.generator.api.GeneratedJavaFile
;
import
org.mybatis.generator.api.GeneratedJavaFile
;
import
org.mybatis.generator.api.MyBatisGenerator
;
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.lang.reflect.Array
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
/**
/**
...
@@ -47,16 +36,10 @@ import java.util.List;
...
@@ -47,16 +36,10 @@ import java.util.List;
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
*/
*/
public
class
SelectSelectivePluginTest
{
public
class
SelectSelectivePluginTest
{
private
DBHelper
helper
;
/**
@BeforeClass
* 初始化
public
static
void
init
()
throws
Exception
{
* @throws IOException
DBHelper
.
createDB
(
"scripts/SelectSelectivePlugin/init.sql"
);
* @throws SQLException
*/
@Before
public
void
init
()
throws
IOException
,
SQLException
{
helper
=
DBHelper
.
getHelper
(
"scripts/SelectSelectivePlugin/init.sql"
);
}
}
/**
/**
...
@@ -65,26 +48,20 @@ public class SelectSelectivePluginTest {
...
@@ -65,26 +48,20 @@ public class SelectSelectivePluginTest {
*/
*/
@Test
@Test
public
void
testSelectByExampleSelective
()
throws
Exception
{
public
void
testSelectByExampleSelective
()
throws
Exception
{
DBHelper
.
cleanDao
();
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/SelectSelectivePlugin/mybatis-generator.xml"
);
List
<
String
>
warnings
=
new
ArrayList
<>();
tool
.
generate
(
new
AbstractShellCallback
()
{
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/SelectSelectivePlugin/mybatis-generator.xml"
));
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
AbstractShellCallback
(
true
)
{
@Override
@Override
public
void
reloadProject
(
ClassLoader
loader
)
{
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
{
SqlSession
sqlSession
=
null
;
try
{
try
{
// 1. 测试sql
// 1. 测试sql
sqlSession
=
helper
.
getSqlSession
();
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.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"
));
ObjectUtil
criteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andIdLessThan"
,
100
l
);
criteria
.
invoke
(
"andIdLessThan"
,
100
l
);
tbExample
.
set
(
"orderByClause"
,
"field2 asc"
);
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对了可变参数会检查类型!
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object
columns1
=
Array
.
newInstance
(
columnField1
.
getCls
(),
1
);
Object
columns1
=
Array
.
newInstance
(
columnField1
.
getCls
(),
1
);
Array
.
set
(
columns1
,
0
,
columnField1
.
getObject
());
Array
.
set
(
columns1
,
0
,
columnField1
.
getObject
());
...
@@ -92,7 +69,7 @@ public class SelectSelectivePluginTest {
...
@@ -92,7 +69,7 @@ public class SelectSelectivePluginTest {
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectByExampleSelective"
,
tbExample
.
getObject
(),
columns1
);
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"selectByExampleSelective"
,
tbExample
.
getObject
(),
columns1
);
Assert
.
assertEquals
(
sql
,
"select field1 from tb WHERE ( id < '100' ) order by field2 asc"
);
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
);
Object
columns2
=
Array
.
newInstance
(
columnField1
.
getCls
(),
2
);
Array
.
set
(
columns2
,
0
,
columnField1
.
getObject
());
Array
.
set
(
columns2
,
0
,
columnField1
.
getObject
());
Array
.
set
(
columns2
,
1
,
columnField2
.
getObject
());
Array
.
set
(
columns2
,
1
,
columnField2
.
getObject
());
...
@@ -124,12 +101,9 @@ public class SelectSelectivePluginTest {
...
@@ -124,12 +101,9 @@ public class SelectSelectivePluginTest {
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
Assert
.
assertTrue
(
false
);
Assert
.
assertTrue
(
false
);
}
finally
{
sqlSession
.
close
();
}
}
}
}
},
warnings
);
});
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
}
}
/**
/**
...
@@ -138,21 +112,15 @@ public class SelectSelectivePluginTest {
...
@@ -138,21 +112,15 @@ public class SelectSelectivePluginTest {
*/
*/
@Test
@Test
public
void
testSelectByPrimaryKeySelective
()
throws
Exception
{
public
void
testSelectByPrimaryKeySelective
()
throws
Exception
{
DBHelper
.
cleanDao
();
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/SelectSelectivePlugin/mybatis-generator.xml"
);
List
<
String
>
warnings
=
new
ArrayList
<>();
tool
.
generate
(
new
AbstractShellCallback
()
{
ConfigurationParser
cp
=
new
ConfigurationParser
(
warnings
);
Configuration
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/SelectSelectivePlugin/mybatis-generator.xml"
));
MyBatisGenerator
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
AbstractShellCallback
(
true
)
{
@Override
@Override
public
void
reloadProject
(
ClassLoader
loader
)
{
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
{
SqlSession
sqlSession
=
null
;
try
{
try
{
// 1. 测试sql
// 1. 测试sql
sqlSession
=
helper
.
getSqlSession
();
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.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对了可变参数会检查类型!
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object
columns1
=
Array
.
newInstance
(
columnField1
.
getCls
(),
1
);
Object
columns1
=
Array
.
newInstance
(
columnField1
.
getCls
(),
1
);
Array
.
set
(
columns1
,
0
,
columnField1
.
getObject
());
Array
.
set
(
columns1
,
0
,
columnField1
.
getObject
());
...
@@ -161,12 +129,12 @@ public class SelectSelectivePluginTest {
...
@@ -161,12 +129,12 @@ public class SelectSelectivePluginTest {
Assert
.
assertEquals
(
sql
,
"select field1 from tb where id = 1"
);
Assert
.
assertEquals
(
sql
,
"select field1 from tb where id = 1"
);
// 2. 测试xxxKey
// 2. 测试xxxKey
ObjectUtil
tbKeysMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao
.TbKeysMapper"
)));
ObjectUtil
tbKeysMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
"
.TbKeysMapper"
)));
ObjectUtil
tbKeysKey
=
new
ObjectUtil
(
loader
,
"com.itfsw.mybatis.generator.plugins.dao.model
.TbKeysKey"
);
ObjectUtil
tbKeysKey
=
new
ObjectUtil
(
loader
,
packagz
+
"
.TbKeysKey"
);
tbKeysKey
.
set
(
"key1"
,
1
l
);
tbKeysKey
.
set
(
"key1"
,
1
l
);
tbKeysKey
.
set
(
"key2"
,
"2"
);
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对了可变参数会检查类型!
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object
columns2
=
Array
.
newInstance
(
columnField2
.
getCls
(),
1
);
Object
columns2
=
Array
.
newInstance
(
columnField2
.
getCls
(),
1
);
Array
.
set
(
columns2
,
0
,
columnField2
.
getObject
());
Array
.
set
(
columns2
,
0
,
columnField2
.
getObject
());
...
@@ -185,8 +153,7 @@ public class SelectSelectivePluginTest {
...
@@ -185,8 +153,7 @@ public class SelectSelectivePluginTest {
sqlSession
.
close
();
sqlSession
.
close
();
}
}
}
}
},
warnings
);
});
myBatisGenerator
.
generate
(
null
,
null
,
null
,
true
);
}
}
/**
/**
...
@@ -196,13 +163,8 @@ public class SelectSelectivePluginTest {
...
@@ -196,13 +163,8 @@ public class SelectSelectivePluginTest {
@Test
@Test
public
void
testSelectOneByExampleSelective
()
throws
Exception
{
public
void
testSelectOneByExampleSelective
()
throws
Exception
{
// 没有配置SelectOneByExamplePlugin插件时不生成对应方法
// 没有配置SelectOneByExamplePlugin插件时不生成对应方法
DBHelper
.
cleanDao
();
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/SelectSelectivePlugin/mybatis-generator.xml"
);
List
<
String
>
warnings
=
new
ArrayList
<>();
MyBatisGenerator
myBatisGenerator
=
tool
.
generate
();
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
);
List
<
GeneratedJavaFile
>
list
=
myBatisGenerator
.
getGeneratedJavaFiles
();
List
<
GeneratedJavaFile
>
list
=
myBatisGenerator
.
getGeneratedJavaFiles
();
for
(
GeneratedJavaFile
file
:
list
)
{
for
(
GeneratedJavaFile
file
:
list
)
{
if
(
file
.
getFileName
().
equals
(
"TbMapper.java"
))
{
if
(
file
.
getFileName
().
equals
(
"TbMapper.java"
))
{
...
@@ -211,24 +173,20 @@ public class SelectSelectivePluginTest {
...
@@ -211,24 +173,20 @@ public class SelectSelectivePluginTest {
}
}
// 配置了SelectOneByExamplePlugin
// 配置了SelectOneByExamplePlugin
DBHelper
.
cleanDao
();
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/SelectSelectivePlugin/mybatis-generator-with-SelectOneByExamplePlugin.xml"
);
config
=
cp
.
parseConfiguration
(
Resources
.
getResourceAsStream
(
"scripts/SelectSelectivePlugin/mybatis-generator-with-SelectOneByExamplePlugin.xml"
));
tool
.
generate
(
new
AbstractShellCallback
()
{
myBatisGenerator
=
new
MyBatisGenerator
(
config
,
new
AbstractShellCallback
(
true
)
{
@Override
@Override
public
void
reloadProject
(
ClassLoader
loader
)
{
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
{
SqlSession
sqlSession
=
null
;
try
{
try
{
// 1. 测试sql
// 1. 测试sql
sqlSession
=
helper
.
getSqlSession
();
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
"com.itfsw.mybatis.generator.plugins.dao.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"
));
ObjectUtil
criteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andIdEqualTo"
,
3
l
);
criteria
.
invoke
(
"andIdEqualTo"
,
3
l
);
tbExample
.
set
(
"orderByClause"
,
"field2 asc"
);
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对了可变参数会检查类型!
// java 动态参数不能有两个会冲突,最后一个封装成Array!!!必须使用反射创建指定类型数组,不然调用invoke对了可变参数会检查类型!
Object
columns1
=
Array
.
newInstance
(
columnField1
.
getCls
(),
1
);
Object
columns1
=
Array
.
newInstance
(
columnField1
.
getCls
(),
1
);
Array
.
set
(
columns1
,
0
,
columnField1
.
getObject
());
Array
.
set
(
columns1
,
0
,
columnField1
.
getObject
());
...
@@ -245,16 +203,8 @@ public class SelectSelectivePluginTest {
...
@@ -245,16 +203,8 @@ public class SelectSelectivePluginTest {
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
Assert
.
assertTrue
(
false
);
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 @@
...
@@ -16,19 +16,12 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
tools
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
tools
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.Assert
;
import
org.junit.Assert
;
import
org.mybatis.generator.api.ShellCallback
;
import
org.mybatis.generator.api.ShellCallback
;
import
org.mybatis.generator.exception.ShellException
;
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.File
;
import
java.io.FileReader
;
import
java.io.FileWriter
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.StringTokenizer
;
import
java.util.StringTokenizer
;
import
static
org
.
mybatis
.
generator
.
internal
.
util
.
messages
.
Messages
.
getString
;
import
static
org
.
mybatis
.
generator
.
internal
.
util
.
messages
.
Messages
.
getString
;
...
@@ -42,18 +35,15 @@ 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
{
public
abstract
class
AbstractShellCallback
implements
ShellCallback
{
/**
private
MyBatisGeneratorTool
tool
;
// MyBatisGenerator 工具
* The overwrite.
*/
private
boolean
overwrite
;
/**
/**
* Instantiates a new default shell callback.
* Setter method for property <tt>tool</tt>.
* @param overwrite the overwrite
* @param tool value to be assigned to property tool
* @author hewei
*/
*/
public
AbstractShellCallback
(
boolean
overwrite
)
{
public
void
setTool
(
MyBatisGeneratorTool
tool
)
{
super
();
this
.
tool
=
tool
;
this
.
overwrite
=
overwrite
;
}
}
/**
/**
...
@@ -62,104 +52,32 @@ public abstract class AbstractShellCallback implements ShellCallback {
...
@@ -62,104 +52,32 @@ public abstract class AbstractShellCallback implements ShellCallback {
*/
*/
@Override
@Override
public
void
refreshProject
(
String
project
)
{
public
void
refreshProject
(
String
project
)
{
File
daoDir
=
new
File
(
project
+
"/com/itfsw/mybatis/generator/plugins/dao"
);
SqlSession
sqlSession
=
null
;
List
<
File
>
files
=
getJavaFiles
(
daoDir
);
try
{
if
(!
files
.
isEmpty
())
{
// 编译项目
compileJavaFiles
(
files
);
sqlSession
=
tool
.
compile
();
copyMappings
(
daoDir
,
project
);
reloadProject
(
sqlSession
,
tool
.
getTargetClassLoader
(),
tool
.
getTargetPackage
());
}
}
catch
(
Exception
e
)
{
reloadProject
(
this
.
getClass
().
getClassLoader
());
e
.
printStackTrace
();
}
Assert
.
assertTrue
(
false
);
}
finally
{
/**
sqlSession
.
close
();
* 拷贝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
);
}
}
}
}
}
}
}
/**
/**
* 动态编译java文件
* 重载项目
* @param files
*
* @param sqlSession
* @param loader
* @param packagz
*/
*/
private
void
compileJavaFiles
(
List
<
File
>
files
)
{
public
abstract
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
);
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
;
}
/* (non-Javadoc)
/* (non-Javadoc)
* @see org.mybatis.generator.api.ShellCallback#getDirectory(java.lang.String, java.lang.String)
* @see org.mybatis.generator.api.ShellCallback#getDirectory(java.lang.String, java.lang.String)
*/
*/
public
File
getDirectory
(
String
targetProject
,
String
targetPackage
)
public
File
getDirectory
(
String
targetProject
,
String
targetPackage
)
throws
ShellException
{
throws
ShellException
{
// targetProject is interpreted as a directory that must exist
// targetProject is interpreted as a directory that must exist
//
//
// targetPackage is interpreted as a sub directory, but in package
// targetPackage is interpreted as a sub directory, but in package
...
@@ -169,12 +87,11 @@ public abstract class AbstractShellCallback implements ShellCallback {
...
@@ -169,12 +87,11 @@ public abstract class AbstractShellCallback implements ShellCallback {
File
project
=
new
File
(
targetProject
);
File
project
=
new
File
(
targetProject
);
if
(!
project
.
isDirectory
())
{
if
(!
project
.
isDirectory
())
{
throw
new
ShellException
(
getString
(
"Warning.9"
,
//$NON-NLS-1$
throw
new
ShellException
(
getString
(
"Warning.9"
,
targetProject
));
targetProject
));
}
}
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
StringTokenizer
st
=
new
StringTokenizer
(
targetPackage
,
"."
);
//$NON-NLS-1$
StringTokenizer
st
=
new
StringTokenizer
(
targetPackage
,
"."
);
while
(
st
.
hasMoreTokens
())
{
while
(
st
.
hasMoreTokens
())
{
sb
.
append
(
st
.
nextToken
());
sb
.
append
(
st
.
nextToken
());
sb
.
append
(
File
.
separatorChar
);
sb
.
append
(
File
.
separatorChar
);
...
@@ -184,8 +101,7 @@ public abstract class AbstractShellCallback implements ShellCallback {
...
@@ -184,8 +101,7 @@ public abstract class AbstractShellCallback implements ShellCallback {
if
(!
directory
.
isDirectory
())
{
if
(!
directory
.
isDirectory
())
{
boolean
rc
=
directory
.
mkdirs
();
boolean
rc
=
directory
.
mkdirs
();
if
(!
rc
)
{
if
(!
rc
)
{
throw
new
ShellException
(
getString
(
"Warning.10"
,
//$NON-NLS-1$
throw
new
ShellException
(
getString
(
"Warning.10"
,
directory
.
getAbsolutePath
()));
directory
.
getAbsolutePath
()));
}
}
}
}
...
@@ -203,7 +119,7 @@ public abstract class AbstractShellCallback implements ShellCallback {
...
@@ -203,7 +119,7 @@ public abstract class AbstractShellCallback implements ShellCallback {
* @see org.mybatis.generator.api.ShellCallback#isOverwriteEnabled()
* @see org.mybatis.generator.api.ShellCallback#isOverwriteEnabled()
*/
*/
public
boolean
isOverwriteEnabled
()
{
public
boolean
isOverwriteEnabled
()
{
return
overwrit
e
;
return
tru
e
;
}
}
/* (non-Javadoc)
/* (non-Javadoc)
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java
View file @
8a15c348
...
@@ -17,15 +17,15 @@
...
@@ -17,15 +17,15 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
tools
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
tools
;
import
org.apache.ibatis.io.Resources
;
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.io.BufferedReader
;
import
java.net.URL
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.sql.Connection
;
import
java.sql.Connection
;
import
java.sql.
SQLException
;
import
java.sql.
DriverManager
;
import
java.sql.Statement
;
import
java.sql.Statement
;
import
java.util.Properties
;
/**
/**
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
...
@@ -36,64 +36,47 @@ import java.sql.Statement;
...
@@ -36,64 +36,47 @@ import java.sql.Statement;
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
*/
*/
public
class
DBHelper
{
public
class
DBHelper
{
private
static
final
String
MYBATIS_CONFIG
=
"mybatis-config.xml"
;
// 配置文件
private
static
final
String
DB_CONFIG
=
"db.properties"
;
p
rivate
static
DBHelper
helper
;
// helper
p
ublic
static
Properties
properties
;
// 数据库信息
/**
static
{
* 构造函数
try
{
*/
// 获取数据库配置信息
private
DBHelper
()
{
properties
=
new
Properties
();
}
InputStream
inputStream
=
Resources
.
getResourceAsStream
(
DB_CONFIG
);
properties
.
load
(
inputStream
);
/**
inputStream
.
close
();
* 获取数据库操作工具
}
catch
(
IOException
e
)
{
*
e
.
printStackTrace
();
* @param initSql
* @return
*/
public
static
DBHelper
getHelper
(
String
initSql
)
throws
IOException
,
SQLException
{
if
(
helper
==
null
){
helper
=
new
DBHelper
();
helper
.
initDB
(
initSql
);
}
}
cleanDao
();
return
helper
;
}
}
/**
/**
*
获取SqlSession
*
创建数据库
*
*
* @
return
* @
param resource
* @throws
IO
Exception
* @throws Exception
*/
*/
public
SqlSession
getSqlSession
()
throws
IOException
{
public
static
void
createDB
(
String
resource
)
throws
Exception
{
InputStream
inputStream
=
Resources
.
getResourceAsStream
(
MYBATIS_CONFIG
);
String
driver
=
properties
.
getProperty
(
"driver"
);
SqlSessionFactory
sqlSessionFactory
=
new
SqlSessionFactoryBuilder
().
build
(
inputStream
);
String
url
=
properties
.
getProperty
(
"url"
);
inputStream
.
close
();
String
username
=
properties
.
getProperty
(
"username"
);
return
sqlSessionFactory
.
openSession
(
true
);
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
();
Statement
statement
=
connection
.
createStatement
();
// 获取建表和初始化sql
// 获取建表和初始化sql
InputStream
inputStream
=
Resources
.
getResourceAsStream
(
initSql
);
InputStream
inputStream
=
Resources
.
getResourceAsStream
(
resource
);
InputStreamReader
inputStreamReader
=
new
InputStreamReader
(
inputStream
,
"UTF-8"
);
InputStreamReader
inputStreamReader
=
new
InputStreamReader
(
inputStream
,
"UTF-8"
);
BufferedReader
bufferedReader
=
new
BufferedReader
(
inputStreamReader
);
BufferedReader
bufferedReader
=
new
BufferedReader
(
inputStreamReader
);
// 读取sql语句执行
// 读取sql语句执行
StringBuffer
sb
=
new
StringBuffer
();
StringBuffer
sb
=
new
StringBuffer
();
String
line
;
String
line
;
while
((
line
=
bufferedReader
.
readLine
())
!=
null
){
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
sb
.
append
(
line
).
append
(
"\n"
);
sb
.
append
(
line
).
append
(
"\n"
);
if
(
line
.
matches
(
".*;$"
)){
if
(
line
.
matches
(
".*;$"
))
{
statement
.
execute
(
sb
.
toString
());
statement
.
execute
(
sb
.
toString
());
sb
.
setLength
(
0
);
sb
.
setLength
(
0
);
}
}
...
@@ -103,47 +86,6 @@ public class DBHelper {
...
@@ -103,47 +86,6 @@ public class DBHelper {
inputStream
.
close
();
inputStream
.
close
();
statement
.
close
();
statement
.
close
();
connection
.
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 @@
...
@@ -16,6 +16,7 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
tools
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
tools
;
import
java.io.*
;
import
java.lang.reflect.ParameterizedType
;
import
java.lang.reflect.ParameterizedType
;
import
java.lang.reflect.Type
;
import
java.lang.reflect.Type
;
...
@@ -31,17 +32,72 @@ public class Util {
...
@@ -31,17 +32,72 @@ public class Util {
/**
/**
* 获取List 泛型参数
* 获取List 泛型参数
*
* @param type
* @param type
* @return
* @return
*/
*/
public
static
String
getListActualType
(
Type
type
){
public
static
String
getListActualType
(
Type
type
)
{
if
(
type
instanceof
ParameterizedType
)
{
if
(
type
instanceof
ParameterizedType
)
{
Type
[]
actualTypeArguments
=
((
ParameterizedType
)
type
).
getActualTypeArguments
();
Type
[]
actualTypeArguments
=
((
ParameterizedType
)
type
).
getActualTypeArguments
();
if
(
actualTypeArguments
.
length
==
1
){
if
(
actualTypeArguments
.
length
==
1
)
{
return
actualTypeArguments
[
0
].
getTypeName
();
return
actualTypeArguments
[
0
].
getTypeName
();
}
}
}
}
return
null
;
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 @@
...
@@ -14,7 +14,7 @@
# limitations under the License.
# limitations under the License.
#
#
db.driver
=
com.mysql.jdbc.Driver
driver
=
com.mysql.jdbc.Driver
db.url
=
jdbc:mysql://localhost:3306/mybatis-generator-plugin?characterEncoding=UTF-8&allowMultiQueries=true
url
=
jdbc:mysql://localhost:3306/mybatis-generator-plugin?characterEncoding=UTF-8&allowMultiQueries=true
db.username
=
root
username
=
root
db.password
=
root
password
=
root
\ No newline at end of file
\ No newline at end of file
src/test/resources/mybatis-config.xml
View file @
8a15c348
...
@@ -38,6 +38,6 @@
...
@@ -38,6 +38,6 @@
<mappers>
<mappers>
<!-- Mapper扫描包,必须同目录同名称下-->
<!-- 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>
</mappers>
</configuration>
</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 @@
...
@@ -33,19 +33,19 @@
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao.model"
targetProject=
"src/test/java
"
>
<javaModelGenerator
targetPackage=
"
"
targetProject=
"
"
>
<!-- 是否对model添加 构造函数 -->
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
/>
<sqlMapGenerator
targetPackage=
"
"
targetProject=
"
"
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的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"
>
<table
tableName=
"tb"
>
...
...
src/test/resources/scripts/BatchInsertPlugin/mybatis-generator-without-model-column-plugin.xml
View file @
8a15c348
...
@@ -27,23 +27,23 @@
...
@@ -27,23 +27,23 @@
<plugin
type=
"com.itfsw.mybatis.generator.plugins.BatchInsertPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.BatchInsertPlugin"
/>
<!--jdbc的数据库连接 -->
<!--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类
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao.model"
targetProject=
"src/test/java
"
>
<javaModelGenerator
targetPackage=
"
"
targetProject=
"
"
>
<!-- 是否对model添加 构造函数 -->
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
/>
<sqlMapGenerator
targetPackage=
"
"
targetProject=
"
"
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的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"
>
<table
tableName=
"tb"
>
...
...
src/test/resources/scripts/BatchInsertPlugin/mybatis-generator.xml
View file @
8a15c348
...
@@ -29,23 +29,23 @@
...
@@ -29,23 +29,23 @@
<plugin
type=
"com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"
/>
<!--jdbc的数据库连接 -->
<!--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类
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao.model"
targetProject=
"src/test/java
"
>
<javaModelGenerator
targetPackage=
"
"
targetProject=
"
"
>
<!-- 是否对model添加 构造函数 -->
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
/>
<sqlMapGenerator
targetPackage=
"
"
targetProject=
"
"
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的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"
>
<table
tableName=
"tb"
>
...
...
src/test/resources/scripts/ExampleTargetPlugin/mybatis-generator-without-plugin.xml
View file @
8a15c348
...
@@ -25,23 +25,23 @@
...
@@ -25,23 +25,23 @@
<!-- 插件 -->
<!-- 插件 -->
<!--jdbc的数据库连接 -->
<!--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类
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao.model"
targetProject=
"src/test/java
"
>
<javaModelGenerator
targetPackage=
"
"
targetProject=
"
"
>
<!-- 是否对model添加 构造函数 -->
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
/>
<sqlMapGenerator
targetPackage=
"
"
targetProject=
"
"
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的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"
>
<table
tableName=
"tb"
>
...
...
src/test/resources/scripts/ExampleTargetPlugin/mybatis-generator.xml
View file @
8a15c348
...
@@ -30,23 +30,23 @@
...
@@ -30,23 +30,23 @@
</plugin>
</plugin>
<!--jdbc的数据库连接 -->
<!--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类
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao.model"
targetProject=
"src/test/java
"
>
<javaModelGenerator
targetPackage=
"
"
targetProject=
"
"
>
<!-- 是否对model添加 构造函数 -->
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
/>
<sqlMapGenerator
targetPackage=
"
"
targetProject=
"
"
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的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"
>
<table
tableName=
"tb"
>
...
...
src/test/resources/scripts/SelectSelectivePlugin/mybatis-generator-with-SelectOneByExamplePlugin.xml
View file @
8a15c348
...
@@ -31,23 +31,23 @@
...
@@ -31,23 +31,23 @@
<plugin
type=
"com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"
/>
<!--jdbc的数据库连接 -->
<!--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类
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao.model"
targetProject=
"src/test/java
"
>
<javaModelGenerator
targetPackage=
"
"
targetProject=
"
"
>
<!-- 是否对model添加 构造函数 -->
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
/>
<sqlMapGenerator
targetPackage=
"
"
targetProject=
"
"
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的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"
>
<table
tableName=
"tb"
>
...
...
src/test/resources/scripts/SelectSelectivePlugin/mybatis-generator.xml
View file @
8a15c348
...
@@ -29,23 +29,23 @@
...
@@ -29,23 +29,23 @@
<plugin
type=
"com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"
/>
<!--jdbc的数据库连接 -->
<!--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类
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao.model"
targetProject=
"src/test/java
"
>
<javaModelGenerator
targetPackage=
"
"
targetProject=
"
"
>
<!-- 是否对model添加 构造函数 -->
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
"
com.itfsw.mybatis.generator.plugins.dao"
targetProject=
"src/test/java
"
/>
<sqlMapGenerator
targetPackage=
"
"
targetProject=
"
"
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的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"
>
<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