【记录】mybatis-generator如何使用(maven方式)
1、首先在pom.xml中添加插件
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> </dependencies> </plugin>
2、然后编写generatorConfig.xml,才能存放地址默认是 src/main/resources/ ,你在pom.xml自定义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 location="D:\ruanjian\mysql-connector-java-5.1.44/mysql-connector-java-5.1.44-bin.jar" /> <context id="context1" targetRuntime="MyBatis3"> <property name="autoDelimitKeywords" value="false"/> <!-- 生成的Java文件的编码 --> <property name="javaFileEncoding" value="UTF-8"/> <!-- 格式化java代码 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> <!-- 格式化XML代码 --> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/user" userId="root" password="123456" /> <javaModelGenerator targetPackage="com.lkb.domain" targetProject="src\main\java" /> <sqlMapGenerator targetPackage="mapper" targetProject="src\main\resources" /> <javaClientGenerator targetPackage="com.lkb.mapper" targetProject="src\main\java" type="XMLMAPPER" /> <table schema="user" tableName="student" domainObjectName="Student" enableCountByExample="false" enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"/> </context> </generatorConfiguration>
标签很简单,大家看看就明白。
3、右键点击pom.xml -> run as -> Maven build... -> goals -> 填写mybatis-generator:generate -> 点击run。
完成
可以看到生成自动的domain mapper.java mapper.xml文件。