1,首先,我们搭建好Maven环境后,配置pom文件。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.openlab</groupId>
<artifactId>MavenDemo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MavenDemo1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- MyBatis代码生成 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.16</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
2.接下来我们配置db.properties来配置数据库连接信息,接下来创建mybatis-config.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>
<properties resource="db.properties"></properties>
<context id="context1" targetRuntime="Mybatis3">
<jdbcConnection
driverClass="${jdbc.driverClassName}"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}"/>
<javaModelGenerator targetPackage="${mode.package}" targetProject="./src/main/java"></javaModelGenerator>
<sqlMapGenerator targetPackage="${xml.mapper.package}" targetProject="./src/main/java"></sqlMapGenerator>
<javaClientGenerator targetPackage="${dao.package}" targetProject="./src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<table schema="???" tableName="user"
enableSelectByExample="false" enableDeleteByExample="false" enableUpdateByExample="false"
enableCountByExample="false" selectByExampleQueryId="false">
</table>
<table schema="???" tableName="role"
enableSelectByExample="false" enableDeleteByExample="false" enableUpdateByExample="false"
enableCountByExample="false" selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
3.接下来我们配置一下参数。