链接: 官方文档.
-
创建mybatis-generator.xml
我的mybatis-generator.xml放在pom.xml的同一级别地方,如果你想放在不同的地方,可以在pom.xml<plugin>
中修改地址<?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="/Program Files/IBM/SQLLIB/java/db2java.zip" />--> <!--targetRuntime 生成mapper映射文件的复杂程度--> <context id="js" targetRuntime="MyBatis3"> <!-- 是否不生成注释 --> <commentGenerator> <property name="suppressDate" value="true" /> <property name="suppressAllComments" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/transactiontest?useUnicode=true&characterEncoding=utf-8" userId="root" password="root"> <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection> <!--数据库BigDecimals字段是否强制在java中定义--> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--实体类生成信息 bean--> <javaModelGenerator targetPackage="com.jsu.bean.bak" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--mybatis 的xml文件地址--> <sqlMapGenerator targetPackage="com.jsu.dao.bak" targetProject=".\src\main\resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!--mybatis的mapper接口--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.jsu.dao.bak" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!--要生成的表结构--> <table schema="transactiontest" tableName="card" domainObjectName="Card" > <!--实体类的属性名和列名相同--> <property name="useActualColumnNames" value="true"/> <!--column主键 sqlStatement 数据库 identity 自增长 --> <generatedKey column="ID" sqlStatement="card" identity="card" /> <!--<columnOverride column="DATE_FIELD" property="startDate" />--> <!--忽略对象的属性 <ignoreColumn column="FRED" /> --> <!--长文本使用varchar覆盖 <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> --> </table> </context> </generatorConfiguration>
mybatis-generator.xml位置
-
pom.xml
<!--这里属于为逆向工程添加的plugin--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.4</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> </dependencies> <configuration> <overwrite>true</overwrite> <!--mybatis-generator.xml的地址--> <configurationFile>./mybatis-generator.xml</configurationFile> </configuration> </plugin> <!----> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin>
-
maven输入
mvn mybatis-generator:generate