使用反向生成器可以生成数据库表对应的实体类和mapper映射文件:
以下是具体介绍相应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 location="D:/DMCworkspace/z_DMC/src/mysql-connector-java-5.1.7-bin.jar" /> //用于指定加载内省数据库驱动的路径 <context id="aim-generator-mybatis3" defaultModelType="flat"> //defaultModelType指定如何生成实体类,flat:该模型为每一张表只生成一个实体类。这个实体类包含表中的所有字段 <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <property name="javaFileEncoding" value="UTF-8"/> <commentGenerator> <property name="suppressAllComments" value="false"/> <property name="suppressDate" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.17.11:3306/dmc" userId="dmc" password="dmc"> </jdbcConnection> //加载驱动 <javaModelGenerator targetPackage="bz.sunlight.dmcEntity" targetProject="z_DMC"> <property name="trimStrings" value="true" /> //trimStrings:是否对数据库查询结果进行trim操作,如果设置为true就会生成类似这样public void setUsername(String username) {this.username = username == null ? null : username.trim();}的setter方法。默认值为false。 </javaModelGenerator> <sqlMapGenerator targetPackage="bz.sunlight.dao" targetProject="z_DMC"> </sqlMapGenerator> <javaClientGenerator targetPackage="bz.sunlight.dao" targetProject="z_DMC" type="XMLMAPPER"> <property name="exampleMethodVisibility" value="public" /> </javaClientGenerator> <table schema="dmc" tableName="vehicle_list"> //tableName:指定要生成的表名,可以使用SQL通配符匹配多个表。生成所有的表可以这样设置<table tableName="%" />,可以指定的表明生成指定的实体类和映射 </table> <!-- <table schema="dmc" tableName="vehicle" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <columnOverride column="Id" property="id" /> </table> --> </context> </generatorConfiguration>