Mybatis-Generator配置文档记录. 發布於 2 年前 (2017年11月04日) – 1680 次检阅

Mybatis-Generator的配置文件生成…下次自己复制修改..

emmm

generatorConfig.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>      <!--数据库驱动-->  	<!-- 如果IDE(eclipse或者idea) 项目里导入了jar包,那么就不需要配置了jar包的绝对路径了  	<classPathEntry location="e:/project/mybatis/lib/mysql-connector-java-5.0.8-bin.jar"/> -->      <context id="mysqlContent" targetRuntime="MyBatis3" defaultModelType="flat">          <commentGenerator>          	<!-- 该属性用于指定MBG是否将在生成的注释中包含生成时间戳。默认为false -->              <property name="suppressDate" value="true"/>              <!-- 该属性用于指定MBG是否将在生成的代码中包含任何注释。默认为false即提供 -->              <property name="suppressAllComments" value="true"/>          </commentGenerator>          <!--数据库链接地址账号密码-->          <jdbcConnection driverClass="com.mysql.jdbc.Driver"          				connectionURL="jdbc:mysql://localhost/mybatis"          				userId="root"          				password="root">          </jdbcConnection>          <!-- JDBC类型和JAVA类型如何转换 -->          <javaTypeResolver>              <property name="forceBigDecimals" value="false"/>          </javaTypeResolver>          <!--生成Model类存放位置-->          <javaModelGenerator targetPackage="com.glj.pojo" targetProject="src">              <property name="enableSubPackages" value="true"/>              <property name="trimStrings" value="true"/>          </javaModelGenerator>          <!--生成映射文件存放位置-->          <sqlMapGenerator targetPackage="com.glj.mapper" targetProject="src">              <property name="enableSubPackages" value="true"/>          </sqlMapGenerator>          <!--生成Dao类存放位置-->          <javaClientGenerator type="XMLMAPPER"  	targetPackage="com.glj.mapper" targetProject="src">              <property name="enableSubPackages" value="true"/>          </javaClientGenerator>          <!--生成对应表及类名-->          <table tableName="tb_user" domainObjectName="User"  	enableCountByExample="false" enableUpdateByExample="false"  	enableDeleteByExample="false" enableSelectByExample="true"  	selectByExampleQueryId="false">             <!--  <property name="my.isgen.usekeys" value="true"/> -->              <generatedKey column="id" sqlStatement="MySql" />          </table>      </context>  </generatorConfiguration>
<?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>     	<context id="mysqlTable" targetRuntime="MyBatis3" defaultModelType="flat">  		<commentGenerator>  			<!-- 关闭自动生成注解 -->  			<property name="suppressAllComments" value="true" />  		</commentGenerator>  		<!-- 配置数据库连接 -->  		<jdbcConnection driverClass="com.mysql.jdbc.Driver"  			connectionURL="jdbc:mysql://localhost:3306/ssm_shiro" userId="root"  			password="root">  		</jdbcConnection>  		<!-- JDBC类型和JAVA类型如何转换 -->  		<javaTypeResolver>  			<property name="forceBigDecimals" value="false" />  		</javaTypeResolver>  		<!-- 指定entity生成的位置 -->  		<javaModelGenerator targetPackage="com.glj.entity"  			targetProject=".srcmainjava">  			<property name="enableSubPackages" value="true" />  			<property name="trimStrings" value="true" />  		</javaModelGenerator>  		<!--指定sql映射文件生成的位置 -->  		<sqlMapGenerator targetPackage="mapper" targetProject=".srcmainresources">  			<property name="enableSubPackages" value="true" />  		</sqlMapGenerator>  		<!-- 指定dao接口生成的位置,mapper接口 -->  		<javaClientGenerator type="XMLMAPPER"  			targetPackage="com.glj.dao" targetProject=".srcmainjava">  			<property name="enableSubPackages" value="true" />  		</javaClientGenerator>  		<!-- table指定每个表的生成策略 -->  		<table tableName="t_human" domainObjectName="Human"></table>  		<table tableName="t_permission" domainObjectName="Permission"></table>  		<table tableName="t_role" domainObjectName="Role"></table>  		<table tableName="t_user" domainObjectName="User"></table>  	</context>  </generatorConfiguration>

TestMybatisGenerator.java

package com.glj.test;    import java.io.File;  import java.io.InputStream;  import java.util.ArrayList;  import java.util.List;    import org.mybatis.generator.api.MyBatisGenerator;  import org.mybatis.generator.config.Configuration;  import org.mybatis.generator.config.xml.ConfigurationParser;  import org.mybatis.generator.internal.DefaultShellCallback;    public class TestMybatisGenerator {        public static void main(String[] args) throws Exception {            	  //mbg执行过程中的警告信息      		List<String> warnings = new ArrayList<String>();      	  //当生成的代码重复时,覆盖源代码      	   boolean overwrite = true;      	  //读取mbg配置文件      	   File configFile = new File("generator-config.xml");      	   ConfigurationParser cp = new ConfigurationParser(warnings);      	   Configuration config = cp.parseConfiguration(configFile);      	   DefaultShellCallback callback = new DefaultShellCallback(overwrite);            //创建mbg      	   MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);            //执行生成代码      	   myBatisGenerator.generate(null);      	   System.out.println("生成代码成功,刷新项目,查看文件,然后执行TestMybatis.java");                }  }