superInterfaceTypes = compilationUnit.getSuperInterfaceTypes();
+ boolean match = superInterfaceTypes.stream()
+ .anyMatch(f -> f.getFullyQualifiedName().equals("java.io.Serializable"));
+ Assert.assertTrue(match);
+ }
+ }
+
+ }
+ }
+
+ @Test
+ public void testSerialize() throws Exception {
+ MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/SerializablePlugin/mybatis-generator.xml");
+ tool.generate(new AbstractShellCallback() {
+ @Override
+ public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
+ // get & set
+ ObjectUtil objectUtil = new ObjectUtil(loader, packagz + ".Tb");
+ objectUtil.invoke("setId", 1l);
+ Object object = objectUtil.getObject();
+ String path = loader.getResource("").getPath();
+ String target = packagz.replaceAll("\\.", "/");
+ String fileName = path + target + "/tbSerialize";
+ try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(fileName))) {
+ objectOutputStream.writeObject(object);
+ }
+ try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(fileName))) {
+ Object readObject = objectInputStream.readObject();
+ ObjectUtil objectUtil1 = new ObjectUtil(readObject);
+ Object id = objectUtil1.invoke("getId");
+ Assert.assertEquals(id, 1L);
+ }
+ }
+ });
+ }
+
+}
\ No newline at end of file
diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/tools/MyBatisGeneratorTool.java b/src/test/java/com/itfsw/mybatis/generator/plugins/tools/MyBatisGeneratorTool.java
index 144adfe4..f10a4910 100644
--- a/src/test/java/com/itfsw/mybatis/generator/plugins/tools/MyBatisGeneratorTool.java
+++ b/src/test/java/com/itfsw/mybatis/generator/plugins/tools/MyBatisGeneratorTool.java
@@ -30,6 +30,7 @@
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
+import org.mybatis.generator.internal.util.StringUtility;
import javax.sql.DataSource;
import javax.tools.JavaCompiler;
@@ -49,8 +50,9 @@
/**
* ---------------------------------------------------------------------------
- *
+ *
* ---------------------------------------------------------------------------
+ *
* @author: hewei
* @time:2017/7/4 16:14
* ---------------------------------------------------------------------------
@@ -64,6 +66,7 @@ public class MyBatisGeneratorTool {
/**
* 创建
+ *
* @param resource
* @return
*/
@@ -75,12 +78,13 @@ public static MyBatisGeneratorTool create(String resource) throws IOException, X
ConfigurationParser cp = new ConfigurationParser(tool.warnings);
tool.config = cp.parseConfiguration(Resources.getResourceAsStream(resource));
// 修正配置目标
- tool.fixConfigToTarget();
+ tool.fixConfigToTarget(null);
return tool;
}
/**
* 执行MyBatisGenerator
+ *
* @param before
* @param callback
* @return
@@ -98,6 +102,7 @@ public MyBatisGenerator generate(IBeforeCallback before, AbstractShellCallback c
/**
* 执行MyBatisGenerator
+ *
* @param callback
* @return
* @throws SQLException
@@ -105,13 +110,14 @@ public MyBatisGenerator generate(IBeforeCallback before, AbstractShellCallback c
* @throws InterruptedException
*/
public MyBatisGenerator generate(AbstractShellCallback callback) throws Exception {
- return this.generate(() -> {
+ return this.generate(() -> {
- }, callback);
+ }, callback);
}
/**
* 执行MyBatisGenerator
+ *
* @param before
* @return
* @throws SQLException
@@ -127,6 +133,7 @@ public MyBatisGenerator generate(IBeforeCallback before) throws Exception {
/**
* 执行MyBatisGenerator(不生成文件)
+ *
* @return
* @throws SQLException
* @throws IOException
@@ -138,8 +145,25 @@ public MyBatisGenerator generate() throws InvalidConfigurationException, Interru
return myBatisGenerator;
}
+ /**
+ * 执行MyBatisGenerator(生成文件)
+ *
+ * @return
+ * @throws InvalidConfigurationException
+ * @throws InterruptedException
+ * @throws SQLException
+ * @throws IOException
+ */
+ public MyBatisGenerator generateAndWriteFiles(String dir) throws InvalidConfigurationException, InterruptedException, SQLException, IOException {
+ MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, new DefaultShellCallback(true), warnings);
+ fixConfigToTarget(dir);
+ myBatisGenerator.generate(null, null, null, true);
+ return myBatisGenerator;
+ }
+
/**
* 编译项目并返回 SqlSession
+ *
* @return
*/
public SqlSession compile() throws IOException, ClassNotFoundException {
@@ -152,6 +176,7 @@ public SqlSession compile() throws IOException, ClassNotFoundException {
/**
* 获取目标目录的ClassLoader
+ *
* @return
*/
public ClassLoader getTargetClassLoader() throws MalformedURLException {
@@ -162,6 +187,7 @@ public ClassLoader getTargetClassLoader() throws MalformedURLException {
/**
* 获取SqlSession
+ *
* @return
* @throws IOException
*/
@@ -186,6 +212,7 @@ public SqlSession getSqlSession() throws IOException, ClassNotFoundException {
/**
* 动态编译java文件
+ *
* @param files
*/
private void compileJavaFiles(List files) {
@@ -206,6 +233,7 @@ private void compileJavaFiles(List files) {
/**
* 获取指定后缀的文件
+ *
* @param file
* @return
*/
@@ -227,9 +255,14 @@ private List getGeneratedFiles(File file, String ext) {
/**
* 修正配置到指定target
*/
- private void fixConfigToTarget() {
+ private void fixConfigToTarget(String dir) {
this.targetProject = this.getClass().getClassLoader().getResource("").getPath();
- this.targetPackage = DAO_PACKAGE + ".s" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
+ if (StringUtility.stringHasValue(dir)) {
+ this.targetPackage = DAO_PACKAGE + "." + dir;
+ } else {
+ this.targetPackage = DAO_PACKAGE + ".s" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
+ }
+
for (Context context : config.getContexts()) {
context.getJavaModelGeneratorConfiguration().setTargetProject(targetProject);
context.getJavaModelGeneratorConfiguration().setTargetPackage(targetPackage);
@@ -242,6 +275,7 @@ private void fixConfigToTarget() {
/**
* Getter method for property warnings.
+ *
* @return property value of warnings
* @author hewei
*/
@@ -251,6 +285,7 @@ public List getWarnings() {
/**
* Getter method for property config.
+ *
* @return property value of config
* @author hewei
*/
@@ -260,6 +295,7 @@ public Configuration getConfig() {
/**
* Getter method for property targetPackage.
+ *
* @return property value of targetPackage
* @author hewei
*/
diff --git a/src/test/resources/scripts/OverwrittenMapperXmlPlugin/init.sql b/src/test/resources/scripts/OverwrittenMapperXmlPlugin/init.sql
new file mode 100644
index 00000000..e59c6eca
--- /dev/null
+++ b/src/test/resources/scripts/OverwrittenMapperXmlPlugin/init.sql
@@ -0,0 +1,27 @@
+/*
+Navicat MySQL Data Transfer
+
+Source Server : localhost
+Source Server Version : 50617
+Source Host : localhost:3306
+Source Database : mybatis-generator-plugin
+
+Target Server Type : MYSQL
+Target Server Version : 50617
+File Encoding : 65001
+
+Date: 2017-06-26 17:30:13
+*/
+
+SET FOREIGN_KEY_CHECKS=0;
+
+-- ----------------------------
+-- Table structure for tb
+-- ----------------------------
+DROP TABLE IF EXISTS `tb`;
+CREATE TABLE `tb` (
+ `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
+ `field1` varchar(255) DEFAULT NULL COMMENT '注释2',
+ `field2` bigint(20) NOT NULL COMMENT '注释3',
+ PRIMARY KEY (`id`)
+);
\ No newline at end of file
diff --git a/src/test/resources/scripts/OverwrittenMapperXmlPlugin/mybatis-generator.xml b/src/test/resources/scripts/OverwrittenMapperXmlPlugin/mybatis-generator.xml
new file mode 100644
index 00000000..62c771e7
--- /dev/null
+++ b/src/test/resources/scripts/OverwrittenMapperXmlPlugin/mybatis-generator.xml
@@ -0,0 +1,57 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/test/resources/scripts/SelectForUpdatePlugin/init.sql b/src/test/resources/scripts/SelectForUpdatePlugin/init.sql
new file mode 100644
index 00000000..1f0955c4
--- /dev/null
+++ b/src/test/resources/scripts/SelectForUpdatePlugin/init.sql
@@ -0,0 +1,70 @@
+/*
+Navicat MySQL Data Transfer
+
+Source Server : localhost
+Source Server Version : 50617
+Source Host : localhost:3306
+Source Database : mybatis-generator-plugin
+
+Target Server Type : MYSQL
+Target Server Version : 50617
+File Encoding : 65001
+
+Date: 2017-07-03 17:34:11
+*/
+
+SET FOREIGN_KEY_CHECKS=0;
+
+-- ----------------------------
+-- Table structure for tb
+-- ----------------------------
+DROP TABLE IF EXISTS `tb`;
+CREATE TABLE `tb` (
+ `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
+ `field1` varchar(255) DEFAULT NULL COMMENT '注释2',
+ `field2` int(11) DEFAULT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
+
+-- ----------------------------
+-- Records of tb
+-- ----------------------------
+INSERT INTO `tb` VALUES ('1', 'fd1', null);
+INSERT INTO `tb` VALUES ('2', null, '2');
+INSERT INTO `tb` VALUES ('3', 'fd3', '3');
+
+-- ----------------------------
+-- Table structure for tb_blobs
+-- ----------------------------
+DROP TABLE IF EXISTS `tb_blobs`;
+CREATE TABLE `tb_blobs` (
+ `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
+ `field1` varchar(255) DEFAULT NULL,
+ `field2` longtext COMMENT '注释2',
+ `field3` longtext,
+ PRIMARY KEY (`id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8;
+
+-- ----------------------------
+-- Records of tb_blobs
+-- ----------------------------
+INSERT INTO `tb_blobs` VALUES ('1', 'fd1', 'fd2', null);
+
+-- ----------------------------
+-- Table structure for tb_keys
+-- ----------------------------
+DROP TABLE IF EXISTS `tb_keys`;
+CREATE TABLE `tb_keys` (
+ `key1` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
+ `key2` varchar(255) NOT NULL,
+ `field1` varchar(255) DEFAULT NULL COMMENT '注释2',
+ `field2` int(11) DEFAULT NULL,
+ PRIMARY KEY (`key1`,`key2`)
+) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
+
+-- ----------------------------
+-- Records of tb_keys
+-- ----------------------------
+INSERT INTO `tb_keys` VALUES ('1', '2', 'fd1', null);
+INSERT INTO `tb_keys` VALUES ('2', '3', null, '2');
+INSERT INTO `tb_keys` VALUES ('3', '4', 'fd2', '3');
diff --git a/src/test/resources/scripts/SelectForUpdatePlugin/mybatis-generator-limit.xml b/src/test/resources/scripts/SelectForUpdatePlugin/mybatis-generator-limit.xml
new file mode 100644
index 00000000..693fc686
--- /dev/null
+++ b/src/test/resources/scripts/SelectForUpdatePlugin/mybatis-generator-limit.xml
@@ -0,0 +1,56 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/test/resources/scripts/SelectForUpdatePlugin/mybatis-generator.xml b/src/test/resources/scripts/SelectForUpdatePlugin/mybatis-generator.xml
new file mode 100644
index 00000000..dc877899
--- /dev/null
+++ b/src/test/resources/scripts/SelectForUpdatePlugin/mybatis-generator.xml
@@ -0,0 +1,56 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/test/resources/scripts/SerializablePlugin/init.sql b/src/test/resources/scripts/SerializablePlugin/init.sql
new file mode 100644
index 00000000..1f0955c4
--- /dev/null
+++ b/src/test/resources/scripts/SerializablePlugin/init.sql
@@ -0,0 +1,70 @@
+/*
+Navicat MySQL Data Transfer
+
+Source Server : localhost
+Source Server Version : 50617
+Source Host : localhost:3306
+Source Database : mybatis-generator-plugin
+
+Target Server Type : MYSQL
+Target Server Version : 50617
+File Encoding : 65001
+
+Date: 2017-07-03 17:34:11
+*/
+
+SET FOREIGN_KEY_CHECKS=0;
+
+-- ----------------------------
+-- Table structure for tb
+-- ----------------------------
+DROP TABLE IF EXISTS `tb`;
+CREATE TABLE `tb` (
+ `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
+ `field1` varchar(255) DEFAULT NULL COMMENT '注释2',
+ `field2` int(11) DEFAULT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
+
+-- ----------------------------
+-- Records of tb
+-- ----------------------------
+INSERT INTO `tb` VALUES ('1', 'fd1', null);
+INSERT INTO `tb` VALUES ('2', null, '2');
+INSERT INTO `tb` VALUES ('3', 'fd3', '3');
+
+-- ----------------------------
+-- Table structure for tb_blobs
+-- ----------------------------
+DROP TABLE IF EXISTS `tb_blobs`;
+CREATE TABLE `tb_blobs` (
+ `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
+ `field1` varchar(255) DEFAULT NULL,
+ `field2` longtext COMMENT '注释2',
+ `field3` longtext,
+ PRIMARY KEY (`id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8;
+
+-- ----------------------------
+-- Records of tb_blobs
+-- ----------------------------
+INSERT INTO `tb_blobs` VALUES ('1', 'fd1', 'fd2', null);
+
+-- ----------------------------
+-- Table structure for tb_keys
+-- ----------------------------
+DROP TABLE IF EXISTS `tb_keys`;
+CREATE TABLE `tb_keys` (
+ `key1` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
+ `key2` varchar(255) NOT NULL,
+ `field1` varchar(255) DEFAULT NULL COMMENT '注释2',
+ `field2` int(11) DEFAULT NULL,
+ PRIMARY KEY (`key1`,`key2`)
+) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
+
+-- ----------------------------
+-- Records of tb_keys
+-- ----------------------------
+INSERT INTO `tb_keys` VALUES ('1', '2', 'fd1', null);
+INSERT INTO `tb_keys` VALUES ('2', '3', null, '2');
+INSERT INTO `tb_keys` VALUES ('3', '4', 'fd2', '3');
diff --git a/src/test/resources/scripts/SerializablePlugin/mybatis-generator.xml b/src/test/resources/scripts/SerializablePlugin/mybatis-generator.xml
new file mode 100644
index 00000000..f928e0e6
--- /dev/null
+++ b/src/test/resources/scripts/SerializablePlugin/mybatis-generator.xml
@@ -0,0 +1,54 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file