使用java代码动态配置与xml文件结合的方式使用mybatis-generator生成代码配置
1、使用java代码动态配置与xml文件结合的方式使用mybatis-generator生成代码配置
2、上代码:在resources目录下新建:generatorConfiguration.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <properties resource="jdbc.properties" /> <context id="sqlserverTables" targetRuntime="MyBatis3"> <!-- 生成的 Java 文件的编码 --> <property name="javaFileEncoding" value="UTF-8" /> <!-- 格式化 Java 代码 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter" /> <!-- 格式化 XML 代码 --> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter" /> <!-- 生成的pojo,将implements Serializable --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="false" /> </commentGenerator> <!-- 数据库链接URL、用户名、密码 --> <jdbcConnection driverClass="${jdbc.driverClassName}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成model模型,对应的包路径,以及文件存放路径(targetProject),targetProject可以指定具体的路径,如./src/main/java, 也可以使用“MAVEN”来自动生成,这样生成的代码会在target/generatord-source目录下 --> <!--<javaModelGenerator targetPackage="com.joey.mybaties.test.pojo" targetProject="MAVEN"> --> <javaModelGenerator targetPackage="com.yuebing.zhongqiu.manage.system.bean" targetProject="./src/main/java"> <property name="enableSubPackages" value="true" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--对应的mapper.xml文件 --> <sqlMapGenerator targetPackage="mappers" targetProject="com.yuebing.zhongqiu.manage.system.dao.mapper"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 对应的Mapper接口类文件 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.yuebing.zhongqiu.manage.system.dao.mapper" targetProject="./src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 列出要生成代码的所有表,这里配置的是不生成Example文件 --> <table tableName="sys_user" domainObjectName="SysUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <property name="useActualColumnNames" value="false" /> </table> </context> </generatorConfiguration>
3、新建Java类动态配置:
/** * */ package com.yuebing.zhongqiu.utils; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.api.ProgressCallback; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.Context; import org.mybatis.generator.config.JavaClientGeneratorConfiguration; import org.mybatis.generator.config.JavaModelGeneratorConfiguration; import org.mybatis.generator.config.SqlMapGeneratorConfiguration; import org.mybatis.generator.config.TableConfiguration; 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; /** * @author IT * */ public class GeneratorUtil { /** * 使用java文件和xml文件结合的方式来配置org.mybatis.generator 自动生成代码 * @param configFileName 配置文件的名称 * @param tableConfig 配置项 * @param pcallbacks 回调接口 * @return 是否成功 * @throws IOException * @throws XMLParserException * @throws InvalidConfigurationException * @throws SQLException * @throws InterruptedException */ public static boolean setJaveFile(String configFileName,HashMap<String,String> tableConfig,ProgressCallback pcallbacks) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; String rfp = GeneratorUtil.class.getResource("/"+configFileName).getFile(); File configFile = new File(rfp); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = null; //hashmap setConfiguration(config, tableConfig); myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(pcallbacks ); return true; } public static void setConfiguration(Configuration cft,HashMap<String,String> tableConfig) { if(cft != null) { List<Context> contextList = cft.getContexts(); for(Context context :contextList) { List<TableConfiguration> tableConfList = context.getTableConfigurations(); for(TableConfiguration tableConf :tableConfList) { //设置表名 String tableName = tableConfig.get("tableName"); if(tableName != null && !"".equals(tableName)) tableConf.setTableName(tableName); //设置生成的实体类的名称 String domainObjectName = tableConfig.get("domainObjectName"); if(domainObjectName != null && !"".equals(domainObjectName)) tableConf.setDomainObjectName(domainObjectName); } //java实体类配置 JavaModelGeneratorConfiguration javaMode = context.getJavaModelGeneratorConfiguration(); //设置model实体类对应的包路径,以及文件存放路径 targetProject可以指定具体的路径,如./src/main/java String targetPackage = tableConfig.get("targetPackage"); String targetProject = tableConfig.get("targetProject"); if(targetPackage != null && !"".equals(targetPackage)) javaMode.setTargetPackage(targetPackage); if(targetProject != null && !"".equals(targetProject)) javaMode.setTargetProject(targetProject); //对应的mapper.xml文件 SqlMapGeneratorConfiguration sqlMap = context.getSqlMapGeneratorConfiguration(); String xml_targetPackage = tableConfig.get("xml_targetPackage"); String xml_targetProject = tableConfig.get("xml_targetProject"); if(xml_targetPackage != null && !"".equals(xml_targetPackage)) sqlMap.setTargetPackage(xml_targetPackage); if(xml_targetProject != null && !"".equals(xml_targetProject)) sqlMap.setTargetProject(xml_targetProject); //对应的Mapper接口类文件 JavaClientGeneratorConfiguration javaClient = context.getJavaClientGeneratorConfiguration(); String Java_map_targetPackage = tableConfig.get("Java_map_targetPackage"); String Java_map_targetProject = tableConfig.get("Java_map_targetProject"); if(Java_map_targetPackage != null && !"".equals(Java_map_targetPackage)) javaClient.setTargetPackage(Java_map_targetPackage); if(Java_map_targetProject != null && !"".equals(Java_map_targetProject)) javaClient.setTargetProject(Java_map_targetProject); //context //CommentGenerator commGen = context.getCommentGenerator(); } } } public static void main(String[] ag) { String configFileName = "generatorConfiguration.xml"; HashMap<String,String> tableConfig = new HashMap<String, String>(); tableConfig.put("tableName", "sys_menu"); tableConfig.put("domainObjectName", "SysMenu"); tableConfig.put("targetPackage", "com.yuebing.zhongqiu.manage.system.bean"); tableConfig.put("targetProject", "./src/main/java"); tableConfig.put("xml_targetPackage", "mappers"); tableConfig.put("xml_targetProject", "./src/main/resources/static/mappers"); tableConfig.put("Java_map_targetPackage", "com.yuebing.zhongqiu.manage.system.dao.mapper"); tableConfig.put("Java_map_targetProject", "./src/main/java"); ProgressCallback pcallbacks = null; try { setJaveFile(configFileName,tableConfig,pcallbacks); } catch (IOException e) { e.printStackTrace(); } catch (XMLParserException e) { e.printStackTrace(); } catch (InvalidConfigurationException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
4、测试:
public static void main(String[] ag) { String configFileName = "generatorConfiguration.xml"; HashMap<String,String> tableConfig = new HashMap<String, String>(); tableConfig.put("tableName", "sys_menu"); tableConfig.put("domainObjectName", "SysMenu"); tableConfig.put("targetPackage", "com.yuebing.zhongqiu.manage.system.bean"); tableConfig.put("targetProject", "./src/main/java"); tableConfig.put("xml_targetPackage", "mappers"); tableConfig.put("xml_targetProject", "./src/main/resources/static/mappers"); tableConfig.put("Java_map_targetPackage", "com.yuebing.zhongqiu.manage.system.dao.mapper"); tableConfig.put("Java_map_targetProject", "./src/main/java"); ProgressCallback pcallbacks = null; try { setJaveFile(configFileName,tableConfig,pcallbacks); } catch (IOException e) { e.printStackTrace(); } catch (XMLParserException e) { e.printStackTrace(); } catch (InvalidConfigurationException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
5、测试结果ok
6、结束。
用自己所知道的去帮助他人,就像别人当初帮助自己一样!