用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:Userscao.m2
epositorymysqlmysql-connector-java5.1.46mysql-connector-java-5.1.46.jar" />-->
	<classPathEntry location="C:Userscao.m2
epositorycomoracleojdbc611.2.0.1.0ojdbc6-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。

原文地址:https://www.cnblogs.com/caozengling/p/9503337.html