用org.mybatis.generator 生成代码

Posted 曹增令

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用org.mybatis.generator 生成代码相关的知识,希望对你有一定的参考价值。

1:引入pom

  

 

2:增加生成配置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>
	<!-- 制定mysql的驱动包的路径 千万别放中文路径下 -->
	<!--<classPathEntry location="C:\\Users\\cao\\.m2\\repository\\mysql\\mysql-connector-java\\5.1.46\\mysql-connector-java-5.1.46.jar" />-->
	<classPathEntry location="C:\\Users\\cao\\.m2\\repository\\com\\oracle\\ojdbc6\\11.2.0.1.0\\ojdbc6-11.2.0.1.0.jar" />
	<!-- 配置数据源和生成的代码所存放的位置 -->
	<context id="context1">
		<commentGenerator>
			<property name="suppressAllComments" value="true"/>
			<property name="suppressDate" value="true"/>
		</commentGenerator>
		<!--<jdbcConnection driverClass="com.mysql.jdbc.Driver"-->
			<!--connectionURL="jdbc:mysql://localhost:3306/test" userId="root"-->
			<!--password="root" />-->
		<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
			connectionURL="jdbc:oracle:thin:@101.201.148.185:1521:xx" userId="xxxx"
			password="xxxxx" />

		<!-- !!!! Model Configurations !!!! -->
		<javaModelGenerator targetPackage="cn.tianjin.unifiedfee.wa.outerweb.entity" targetProject="src/main/java">
			<property name="enableSubPackages" value="false"/>
			<property name="trimStrings" value="true"/>
		</javaModelGenerator>

		<!-- !!!! Mapper XML Configurations !!!! -->
		<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
			<property name="enableSubPackages" value="false"/>
		</sqlMapGenerator>

		<!-- !!!! Mapper Interface Configurations !!!! -->
		<javaClientGenerator targetPackage="cn.tianjin.unifiedfee.wa.outerweb.mapper" targetProject="src/main/java" type="XMLMAPPER">
			<property name="enableSubPackages" value="false"/>
		</javaClientGenerator>
		
		<table schema="" tableName="tb_log" enableCountByExample="false"
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false"
		>
		</table>
	</context>
</generatorConfiguration>

  

 

 

 

  3:增加java类:

  

 

代码:
 List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File directory = new File("");// 参数为空
        String courseFile = directory.getCanonicalPath();
        File configFile = new File(courseFile+"/src/test/java/generatorConfig.xml");//mbg.xml为上面创建的配置文件。
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
  

 

ok。

以上是关于用org.mybatis.generator 生成代码的主要内容,如果未能解决你的问题,请参考以下文章

mybatis-generator 自动生成mybatis代码与xml

Mybatis-generator生成

mybatis代码生成器——MyBatis Generator

Mybatis代码自动生成

mybatis-generator逆向工程,自动生成dao层和mapper配置

mybatis之generator生成代码