Commit 5e3e9880 authored by hewei's avatar hewei

<package name="xxx"/>时xml必须和Mapper.java在同一目录下

parent e1bf5266
...@@ -18,8 +18,10 @@ package com.itfsw.mybatis.generator.plugins; ...@@ -18,8 +18,10 @@ package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.tools.AbstractShellCallback; import com.itfsw.mybatis.generator.plugins.tools.AbstractShellCallback;
import com.itfsw.mybatis.generator.plugins.tools.DBHelper; import com.itfsw.mybatis.generator.plugins.tools.DBHelper;
import com.itfsw.mybatis.generator.plugins.tools.SqlHelper;
import com.itfsw.mybatis.generator.plugins.tools.Util; import com.itfsw.mybatis.generator.plugins.tools.Util;
import org.apache.ibatis.io.Resources; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
...@@ -58,9 +60,17 @@ public class BatchInsertPluginTest { ...@@ -58,9 +60,17 @@ public class BatchInsertPluginTest {
helper = DBHelper.getHelper("scripts/BatchInsertPlugin/init.sql"); helper = DBHelper.getHelper("scripts/BatchInsertPlugin/init.sql");
} }
/**
* 测试插件依赖
* @throws IOException
* @throws XMLParserException
* @throws InvalidConfigurationException
* @throws SQLException
* @throws InterruptedException
*/
@Test @Test
public void testWarnings1() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { public void testWarnings1() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>(); List<String> warnings = new ArrayList<>();
ConfigurationParser cp = new ConfigurationParser(warnings); ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(Resources.getResourceAsStream("scripts/BatchInsertPlugin/mybatis-generator-without-model-column-plugin.xml")); Configuration config = cp.parseConfiguration(Resources.getResourceAsStream("scripts/BatchInsertPlugin/mybatis-generator-without-model-column-plugin.xml"));
...@@ -72,9 +82,17 @@ public class BatchInsertPluginTest { ...@@ -72,9 +82,17 @@ public class BatchInsertPluginTest {
Assert.assertEquals(warnings.get(0), "itfsw:插件com.itfsw.mybatis.generator.plugins.BatchInsertPlugin插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!"); Assert.assertEquals(warnings.get(0), "itfsw:插件com.itfsw.mybatis.generator.plugins.BatchInsertPlugin插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!");
} }
/**
* 测试错误的支持driver
* @throws IOException
* @throws XMLParserException
* @throws InvalidConfigurationException
* @throws SQLException
* @throws InterruptedException
*/
@Test @Test
public void testWarnings2() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { public void testWarnings2() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>(); List<String> warnings = new ArrayList<>();
ConfigurationParser cp = new ConfigurationParser(warnings); ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(Resources.getResourceAsStream("scripts/BatchInsertPlugin/mybatis-generator-with-error-driver.xml")); Configuration config = cp.parseConfiguration(Resources.getResourceAsStream("scripts/BatchInsertPlugin/mybatis-generator-with-error-driver.xml"));
...@@ -86,6 +104,16 @@ public class BatchInsertPluginTest { ...@@ -86,6 +104,16 @@ public class BatchInsertPluginTest {
Assert.assertEquals(warnings.get(1), "itfsw:插件com.itfsw.mybatis.generator.plugins.BatchInsertPlugin插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键!"); Assert.assertEquals(warnings.get(1), "itfsw:插件com.itfsw.mybatis.generator.plugins.BatchInsertPlugin插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键!");
} }
/**
* 测试生成的方法
* @throws IOException
* @throws XMLParserException
* @throws InvalidConfigurationException
* @throws SQLException
* @throws InterruptedException
* @throws ClassNotFoundException
* @throws NoSuchMethodException
*/
@Test @Test
public void testMethods() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException, ClassNotFoundException, NoSuchMethodException { public void testMethods() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException, ClassNotFoundException, NoSuchMethodException {
List<String> warnings = new ArrayList<>(); List<String> warnings = new ArrayList<>();
...@@ -99,12 +127,12 @@ public class BatchInsertPluginTest { ...@@ -99,12 +127,12 @@ public class BatchInsertPluginTest {
// 1. 普通Mapper参数中List泛型为普通Model // 1. 普通Mapper参数中List泛型为普通Model
Class clsTbMapper = loader.loadClass("com.itfsw.mybatis.generator.plugins.dao.TbMapper"); Class clsTbMapper = loader.loadClass("com.itfsw.mybatis.generator.plugins.dao.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]), "com.itfsw.mybatis.generator.plugins.dao.model.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]), "com.itfsw.mybatis.generator.plugins.dao.model.Tb");
Assert.assertEquals(method.getGenericParameterTypes()[1].getTypeName(), "com.itfsw.mybatis.generator.plugins.dao.model.Tb$Column[]"); Assert.assertEquals(method.getGenericParameterTypes()[1].getTypeName(), "com.itfsw.mybatis.generator.plugins.dao.model.Tb$Column[]");
count++; count++;
...@@ -115,12 +143,12 @@ public class BatchInsertPluginTest { ...@@ -115,12 +143,12 @@ public class BatchInsertPluginTest {
// 2. 带有WithBlobs // 2. 带有WithBlobs
Class clsTbBlobsMapper = loader.loadClass("com.itfsw.mybatis.generator.plugins.dao.TbBlobsMapper"); Class clsTbBlobsMapper = loader.loadClass("com.itfsw.mybatis.generator.plugins.dao.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]), "com.itfsw.mybatis.generator.plugins.dao.model.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]), "com.itfsw.mybatis.generator.plugins.dao.model.TbBlobsWithBLOBs");
Assert.assertEquals(method.getGenericParameterTypes()[1].getTypeName(), "com.itfsw.mybatis.generator.plugins.dao.model.TbBlobsWithBLOBs$Column[]"); Assert.assertEquals(method.getGenericParameterTypes()[1].getTypeName(), "com.itfsw.mybatis.generator.plugins.dao.model.TbBlobsWithBLOBs$Column[]");
count++; count++;
...@@ -136,8 +164,44 @@ public class BatchInsertPluginTest { ...@@ -136,8 +164,44 @@ public class BatchInsertPluginTest {
myBatisGenerator.generate(null, null, null, true); myBatisGenerator.generate(null, null, null, true);
} }
/**
* 测试生成的sql
*/
@Test
public void testSql() throws Exception {
DBHelper.cleanDao();
List<String> warnings = new ArrayList<>();
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(Resources.getResourceAsStream("scripts/BatchInsertPlugin/mybatis-generator.xml"));
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, new AbstractShellCallback(true) {
@Override
public void reloadProject(ClassLoader loader) {
try {
SqlSession sqlSession = helper.getSqlSession();
Object obj = sqlSession.getMapper(loader.loadClass("com.itfsw.mybatis.generator.plugins.dao.TbMapper"));
List<Object> params = new ArrayList<>();
String sql = SqlHelper.getMapperSql(
sqlSession,
loader.loadClass("com.itfsw.mybatis.generator.plugins.dao.TbMapper"),
"batchInsert",
params
);
System.out.println(sql);
} catch (Exception e) {
e.printStackTrace();
Assert.assertTrue(false);
}
}
}, warnings);
myBatisGenerator.generate(null, null, null, true);
}
@AfterClass @AfterClass
public static void clean(){ public static void clean() {
DBHelper.reset(); // DBHelper.reset();
} }
} }
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package com.itfsw.mybatis.generator.plugins.tools; package com.itfsw.mybatis.generator.plugins.tools;
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;
...@@ -24,6 +25,8 @@ import javax.tools.JavaFileObject; ...@@ -24,6 +25,8 @@ import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager; import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider; 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.ArrayList;
import java.util.List; import java.util.List;
import java.util.StringTokenizer; import java.util.StringTokenizer;
...@@ -59,29 +62,77 @@ public abstract class AbstractShellCallback implements ShellCallback { ...@@ -59,29 +62,77 @@ public abstract class AbstractShellCallback implements ShellCallback {
*/ */
@Override @Override
public void refreshProject(String project) { public void refreshProject(String project) {
List<File> files = getJavaFiles(new File(project + "/com/itfsw/mybatis/generator/plugins/dao")); File daoDir = new File(project + "/com/itfsw/mybatis/generator/plugins/dao");
List<File> files = getJavaFiles(daoDir);
if (!files.isEmpty()) { if (!files.isEmpty()) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); compileJavaFiles(files);
copyMappings(daoDir, project);
//获取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();
} }
reloadProject(this.getClass().getClassLoader()); reloadProject(this.getClass().getClassLoader());
} }
/**
* 拷贝xml
* @param file
* @param project
*/
private void copyMappings(File file, String project) {
if (file.exists()) {
File[] files = file.listFiles();
for (File childFile : files) {
if (childFile.isDirectory()) {
copyMappings(childFile, project);
} else if (childFile.getName().endsWith(".xml")) {
try {
FileReader fileReader = new FileReader(childFile);
// 目标路径
String path = childFile.getPath();
path = path.replaceAll("\\\\", "/");
String target = this.getClass().getClassLoader().getResource("").getPath() + path.replace(project, "");
File targetFile = new File(target);
if (!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
FileWriter fileWriter = new FileWriter(targetFile);
int ch = 0;
while ((ch = fileReader.read()) != -1) {
fileWriter.write(ch);
}
fileReader.close();
fileWriter.close();
} catch (Exception e) {
e.printStackTrace();
Assert.assertTrue(false);
}
}
}
}
}
/**
* 动态编译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");
// 设置输出目录
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); public abstract void reloadProject(ClassLoader loader);
/** /**
......
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
<!--<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.mapping" targetProject="src/test/java" /> <sqlMapGenerator targetPackage="com.itfsw.mybatis.generator.plugins.dao" targetProject="src/test/java" />
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码 <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
......
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
<!--<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.mapping" targetProject="src/test/java" /> <sqlMapGenerator targetPackage="com.itfsw.mybatis.generator.plugins.dao" targetProject="src/test/java" />
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码 <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
......
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
<!--<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.mapping" targetProject="src/test/java" /> <sqlMapGenerator targetPackage="com.itfsw.mybatis.generator.plugins.dao" targetProject="src/test/java" />
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码 <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
......
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
<!--<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.mapping" targetProject="src/test/java" /> <sqlMapGenerator targetPackage="com.itfsw.mybatis.generator.plugins.dao" targetProject="src/test/java" />
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码 <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
......
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
<!--<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.mapping" targetProject="src/test/java" /> <sqlMapGenerator targetPackage="com.itfsw.mybatis.generator.plugins.dao" targetProject="src/test/java" />
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码 <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment