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;
import com.itfsw.mybatis.generator.plugins.tools.AbstractShellCallback;
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 org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
......@@ -58,9 +60,17 @@ public class BatchInsertPluginTest {
helper = DBHelper.getHelper("scripts/BatchInsertPlugin/init.sql");
}
/**
* 测试插件依赖
* @throws IOException
* @throws XMLParserException
* @throws InvalidConfigurationException
* @throws SQLException
* @throws InterruptedException
*/
@Test
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);
Configuration config = cp.parseConfiguration(Resources.getResourceAsStream("scripts/BatchInsertPlugin/mybatis-generator-without-model-column-plugin.xml"));
......@@ -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插件使用!");
}
/**
* 测试错误的支持driver
* @throws IOException
* @throws XMLParserException
* @throws InvalidConfigurationException
* @throws SQLException
* @throws InterruptedException
*/
@Test
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);
Configuration config = cp.parseConfiguration(Resources.getResourceAsStream("scripts/BatchInsertPlugin/mybatis-generator-with-error-driver.xml"));
......@@ -86,6 +104,16 @@ public class BatchInsertPluginTest {
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
public void testMethods() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException, ClassNotFoundException, NoSuchMethodException {
List<String> warnings = new ArrayList<>();
......@@ -99,12 +127,12 @@ public class BatchInsertPluginTest {
// 1. 普通Mapper参数中List泛型为普通Model
Class clsTbMapper = loader.loadClass("com.itfsw.mybatis.generator.plugins.dao.TbMapper");
int count = 0;
for (Method method : clsTbMapper.getDeclaredMethods()){
if (method.getName().equals("batchInsert")){
for (Method method : clsTbMapper.getDeclaredMethods()) {
if (method.getName().equals("batchInsert")) {
Assert.assertEquals(Util.getListActualType(method.getGenericParameterTypes()[0]), "com.itfsw.mybatis.generator.plugins.dao.model.Tb");
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(method.getGenericParameterTypes()[1].getTypeName(), "com.itfsw.mybatis.generator.plugins.dao.model.Tb$Column[]");
count++;
......@@ -115,12 +143,12 @@ public class BatchInsertPluginTest {
// 2. 带有WithBlobs
Class clsTbBlobsMapper = loader.loadClass("com.itfsw.mybatis.generator.plugins.dao.TbBlobsMapper");
count = 0;
for (Method method : clsTbBlobsMapper.getDeclaredMethods()){
if (method.getName().equals("batchInsert")){
for (Method method : clsTbBlobsMapper.getDeclaredMethods()) {
if (method.getName().equals("batchInsert")) {
Assert.assertEquals(Util.getListActualType(method.getGenericParameterTypes()[0]), "com.itfsw.mybatis.generator.plugins.dao.model.TbBlobsWithBLOBs");
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(method.getGenericParameterTypes()[1].getTypeName(), "com.itfsw.mybatis.generator.plugins.dao.model.TbBlobsWithBLOBs$Column[]");
count++;
......@@ -136,8 +164,44 @@ public class BatchInsertPluginTest {
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
public static void clean(){
DBHelper.reset();
public static void clean() {
// DBHelper.reset();
}
}
......@@ -16,6 +16,7 @@
package com.itfsw.mybatis.generator.plugins.tools;
import org.junit.Assert;
import org.mybatis.generator.api.ShellCallback;
import org.mybatis.generator.exception.ShellException;
......@@ -24,6 +25,8 @@ import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
......@@ -59,8 +62,59 @@ public abstract class AbstractShellCallback implements ShellCallback {
*/
@Override
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()) {
compileJavaFiles(files);
copyMappings(daoDir, project);
}
reloadProject(this.getClass().getClassLoader());
}
/**
* 拷贝xml
* @param file
* @param project
*/
private void copyMappings(File file, String project) {
if (file.exists()) {
File[] files = file.listFiles();
for (File childFile : files) {
if (childFile.isDirectory()) {
copyMappings(childFile, project);
} else if (childFile.getName().endsWith(".xml")) {
try {
FileReader fileReader = new FileReader(childFile);
// 目标路径
String path = childFile.getPath();
path = path.replaceAll("\\\\", "/");
String target = this.getClass().getClassLoader().getResource("").getPath() + path.replace(project, "");
File targetFile = new File(target);
if (!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
FileWriter fileWriter = new FileWriter(targetFile);
int ch = 0;
while ((ch = fileReader.read()) != -1) {
fileWriter.write(ch);
}
fileReader.close();
fileWriter.close();
} catch (Exception e) {
e.printStackTrace();
Assert.assertTrue(false);
}
}
}
}
}
/**
* 动态编译java文件
* @param files
*/
private void compileJavaFiles(List<File> files) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
//获取java文件管理类
......@@ -77,9 +131,6 @@ public abstract class AbstractShellCallback implements ShellCallback {
JavaCompiler.CompilationTask task = compiler.getTask(null, manager, null, ops, null, it);
//执行编译任务
task.call();
}
reloadProject(this.getClass().getClassLoader());
}
public abstract void reloadProject(ClassLoader loader);
......
......@@ -40,7 +40,7 @@
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--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配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
......
......@@ -38,7 +38,7 @@
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--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配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
......
......@@ -40,7 +40,7 @@
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--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配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
......
......@@ -36,7 +36,7 @@
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--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配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
......
......@@ -41,7 +41,7 @@
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--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配置文件 的代码
type="ANNOTATEDMAPPER",生成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