Commit 8a15c348 authored by hewei's avatar hewei

重构测试框架,JAVA类转载不能实现按需卸载

parent 645a961f
...@@ -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
...@@ -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();
} }
} }
...@@ -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.BeforeClass;
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 @BeforeClass
public void init() throws IOException, SQLException { 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();
}
} }
...@@ -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 overwrite; return true;
} }
/* (non-Javadoc) /* (non-Javadoc)
......
...@@ -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";
private static DBHelper helper; // helper public 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 IOException * @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();
}
}
}
} }
/*
* 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;
}
}
...@@ -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);
}
} }
...@@ -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&amp;allowMultiQueries=true url=jdbc:mysql://localhost:3306/mybatis-generator-plugin?characterEncoding=UTF-8&amp;allowMultiQueries=true
db.username=root username=root
db.password=root password=root
\ No newline at end of file \ No newline at end of file
...@@ -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
...@@ -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">
......
...@@ -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="${db.driver}" connectionURL="${db.url}" userId="${db.username}" password="${db.password}" /> <jdbcConnection driverClass="${driver}" 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">
......
...@@ -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="${db.driver}" connectionURL="${db.url}" userId="${db.username}" password="${db.password}" /> <jdbcConnection driverClass="${driver}" 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">
......
...@@ -25,23 +25,23 @@ ...@@ -25,23 +25,23 @@
<!-- 插件 --> <!-- 插件 -->
<!--jdbc的数据库连接 --> <!--jdbc的数据库连接 -->
<jdbcConnection driverClass="${db.driver}" connectionURL="${db.url}" userId="${db.username}" password="${db.password}" /> <jdbcConnection driverClass="${driver}" 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">
......
...@@ -30,23 +30,23 @@ ...@@ -30,23 +30,23 @@
</plugin> </plugin>
<!--jdbc的数据库连接 --> <!--jdbc的数据库连接 -->
<jdbcConnection driverClass="${db.driver}" connectionURL="${db.url}" userId="${db.username}" password="${db.password}" /> <jdbcConnection driverClass="${driver}" 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">
......
...@@ -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="${db.driver}" connectionURL="${db.url}" userId="${db.username}" password="${db.password}" /> <jdbcConnection driverClass="${driver}" 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">
......
...@@ -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="${db.driver}" connectionURL="${db.url}" userId="${db.username}" password="${db.password}" /> <jdbcConnection driverClass="${driver}" 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">
......
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