创建mybatis_generator工程并导入坐标
<dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>在resources目录下添加名为 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> <!-- classPathEntry:数据库的 JDBC驱动的jar 包地址 --> <classPathEntry location="F:\\HeiMa\\MyFolder\\FrameWork\\MyBatis\\mybatis_generator\\mysql-connector-java-5.1.32.jar" /> <context id="caigouTables" targetRuntime="MyBatis3"> <!-- 配置生成pojo的序列化的插件,mybatis支持很多插件,这些插件都在 org.mybatis.generator.plugins包下 --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/saas-export" userId="root" password="root"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成 POJO 类的位置 --> <javaModelGenerator targetPackage="cn.itcast.domain.cargo" targetProject="src/main/java"> <!-- enableSubPackages:是否让schema 作为包的后缀 --> <property name="enableSubPackages" value="true" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成 XML文件 --> <sqlMapGenerator targetPackage="cn.itcast.dao.cargo" targetProject="./src/main/resources"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!--生成接口--> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.itcast.dao.cargo" targetProject="./src/main/java"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定数据库表 --> <table tableName="co_contract" domainObjectName="Contract" mapperName="ContractDao" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/> <table tableName="co_contract_product" domainObjectName="ContractProduct" mapperName="ContractProductDao" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/> <table tableName="co_ext_cproduct" domainObjectName="ExtCproduct" mapperName="ExtCproductDao" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/> <table tableName="co_factory" domainObjectName="Factory" mapperName="FactoryDao" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/> </context> </generatorConfiguration>通过逆向工程生成的代码如下: