1.使用
如下主要介绍在IDEA 中使用MyBatis Generator 更多想使用请参考官网: http://www.mybatis.org/generator/
1. 新建一个Maven 项目在POM中添加
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
- 新建generatorConfig.xml 配置内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--本地数据库驱动-->
<classPathEntry location="E:.m2
epositorymysqlmysql-connector-java6.0.6mysql-connector-java-6.0.6.jar"/>
<context id="MysqlTables" targetRuntime="MyBatis3">
<!--去除注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/junli?useUnicode=true&characterEncoding=utf-8&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--实体存放位置-->
<javaModelGenerator targetPackage="com.junly.mybatis.beans"
targetProject="G:learnworkspacelearn-mybatissrcmainjava">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--XML存放位置-->
<sqlMapGenerator targetPackage="xml" targetProject="G:learnworkspacelearn-mybatissrcmain
esources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--Mapper存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.junly.mybatis.mapper"
targetProject="G:learnworkspacelearn-mybatis\srcmainjava">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--需要生成的表-->
<table schema="junli" tableName="posts" domainObjectName="Posts">
<property name="useActualColumnNames" value="false"/>
</table>
<table schema="junli" tableName="test" domainObjectName="Test">
<property name="useActualColumnNames" value="false"/>
</table>
<table schema="junli" tableName="blog" domainObjectName="Blog">
<property name="useActualColumnNames" value="false"/>
</table>
<table schema="junli" tableName="author" domainObjectName="Author">
<property name="useActualColumnNames" value="false"/>
</table>
</context>
</generatorConfiguration>
- 生成代码
在完成以上两步之后。就会在idea中看到:直接点击mybatis-generator:generate就可生成。