在src/main/resource目录下创建generatorConfig.xml文件
(官方配置以及说明:http://mybatis.github.io/generator/configreference/xmlconfig.html)
<?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> <!--数据库驱动jar --> <classPathEntry location="C:\Users\Tidus\.m2\repository\mysql\mysql-connector-java\5.1.33\mysql-connector-java-5.1.33.jar" /> <context id="DB2Tables" targetRuntime="Ibatis2Java5"> <!--去除注释 (true好像不起作用) --> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库连接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/tidus" userId="root" password="root"> </jdbcConnection> <!--默认false Java type resolver will always use java.math.BigDecimal if the database column is of type DECIMAL or NUMERIC. --> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建 使用Maven生成在target目录下,会自动创建) --> <javaModelGenerator targetPackage="com.tidus.model" targetProject="D:\MyCodes\java\web\StudentWeb\src\main\java"> <property name="enableSubPackages" value="false" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--生成SQLMAP文件 --> <sqlMapGenerator targetPackage="com.tidus.persistence.ibatis" targetProject="D:\MyCodes\java\web\StudentWeb\src\main\java"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!--生成Dao文件 可以配置 type="XMLMAPPER"生成xml的dao实现 context id="DB2Tables" 修改targetRuntime="MyBatis3" --> <javaClientGenerator type="SPRING" targetPackage="com.tidus.persistence.dao" targetProject="D:\MyCodes\java\web\StudentWeb\src\main\java"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等--> <table schema="tidus" tableName="user_tbl" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table schema="tidus" tableName="student_tbl" domainObjectName="Student" /> </context> </generatorConfiguration>
table其他属性:
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
schema即为数据库名, tableName为对应的数据库表, domainObjectName是要生成的实体类,
如果想要mapper配置文件加入sql的where条件查询, 可以将enableCountByExample等设为true,
这样就会生成一个对应domainObjectName的Example类, enableCountByExample等设为false时,
就不会生成对应的Example类了.
编写完成后命令行执行C:\Users\Tidus>java -jar C:\Users\Tidus\.m2\repository\org\mybatis\generator\mybatis-generator-core\1.3.2\mybatis-generator-core-1.3.2.jar -configfile D:\MyCodes\java\web\StudentWeb\src\main\resources\generatorConfig.xml
即可生成对应的类