第一步 在pom文件中添加 插件
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <goals> <goal>generate</goal> </goals> <configuration> <configurationFile>src/main/resources/generator-mysql.xml</configurationFile> </configuration> </plugin>
第二步 将 generator-mysql.xml 复制到上文配置的对应文件夹中
第三步 点击插件运行 (如果没有此插件 就刷新一下 maven插件)<?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> <!--配置文件 配置属性 可以从配置文件中读取配置<properties resource="mybatis-generator.properties"/>--> <!-- classPathEntry:数据库的JDBC驱动的jar包地址 --> <classPathEntry location="D:\path\mysql-connector-java-5.1.26.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 是否去除自动生成的注释 true:是,false:否 --> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 数据库连接的信息:驱动类、连接地址、用户名、密码 --> <!-- MSSQL: driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" connectionURL="jdbc:sqlserver://localhost:3306;DatabaseName=ibatis" --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://12.0.0.1/aaaa" userId="aaaa" password="cccc"> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!---Java 实体的生成 --> <!-- targetProject:自动生成代码的位置 --> <javaModelGenerator targetPackage="com.test.api.model" targetProject="doc\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="fasle"/> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="false"/> </javaModelGenerator> <!--sqlMapper XML文件的生成信息,包括生成路径等 --> <sqlMapGenerator targetPackage="sqlmap" targetProject="doc\java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!--应用接口的生成信息 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.test.api.dao" implementationPackage="com.test.api.dao.impl" targetProject="doc\java"> <property name="enableSubPackages" value="false"/> <property name="methodNameCalculator" value="extended"/> </javaClientGenerator> <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 --> <!-- 系统管理员表--> <table schema="article_type" tableName="article_type" domainObjectName="ArticleType" enableInsert="true" enableSelectByPrimaryKey="true" enableSelectByExample="false" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false" modelType="flat"> <generatedKey column="id" sqlStatement="MySql" identity="true"/> </table> </context> </generatorConfiguration>
然后就可以在项目中看到生成的 java文件以及mapper文件
<properties> 元素用于指定一个需要在配置中解析使用的外部属性文件。 属性配置中的任何属性将接受 ${property} 这种形式的属性。 从指定的属性文件中搜索匹配的值,配置的值将会被替换。 属性文件时正常的Java属性文件的格式。
<properties>元素是 <generatorConfiguration> 元素的子元素。
必选属性
以下属性是必需的,并且只能有其中一个。
属性 描述
resource 属性文件的全限定名称。当指定了resource属性,将会从classpath下面搜索属性文件。 当指定为 com/myproject/generatorConfig.properties 时,他必须存在 com.myproject 包下面。
url 属性文件的URL值。这可以用于指定一个属性文件在文件系统上的特定位置,例如 file:///C:/myfolder/generatorConfig.properties。