1.创建新项目Mybatis-Generator
2.新建相关文件夹
在main文件夹下新建文件夹java右键Mark Directory as设置为Sources Root然后在src文件夹下新建与main同级的文件夹test并在其中新建java和resources文件夹并右键设置其属性
3.配置相关配置文件
在src/test/resources文件夹里新建文件mybatis-generator.properties
(具体配置以自己的实际情况为主)
# 数据库驱动jar路径
jdbc.driverPath=/opt/Server/mysql-connector-java-5.1.40.jar
# 数据库连接参数
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/odmins?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=haoroot
# 包路径配置
model.package=cn.byhook.web.model
dao.package=cn.byhook.web.dao
xml.mapper.package=cn.byhook.web.mapper
# 目标路径
target.project=src/main/java
同级目录新建文件mybatis-generator.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 url="${mybatis.generator.generatorConfig.properties}"/>
<!--数据库驱动包路径-->
<classPathEntry location="${jdbc.driverPath}"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--对注释进行控制-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接信息-->
<jdbcConnection
driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}">
</jdbcConnection>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="${model.package}" targetProject="${target.project}">
<property name="enableSubPackages" value="ture"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="${xml.mapper.package}" targetProject="${target.project}">
<property name="enableSubPackages" value="ture"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator
type="XMLMAPPER"
targetPackage="${dao.package}"
targetProject="${target.project}">
<property name="enableSubPackages" value="ture"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<table tableName="user" domainObjectName="User"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
4.配置pom.xml文件
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.byhook.generator</groupId>
<artifactId>Generator</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>Generator Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<finalName>Generator</finalName>
<plugins>
<!--Mybatis generator代码生成插件配置-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${plugin.mybatis.generator}</version>
<configuration>
<configurationFile>${mybatis.generator.generatorConfig.xml}</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
<!--Maven编译插件配置-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${plugin.maven-compiler}</version>
<configuration>
<source>${project.build.jdk}</source>
<target>${project.build.jdk}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
<!--配置Maven对resources文件过滤-->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/test/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<!-- 基本设置 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.locales>zh_CN</project.build.locales>
<project.build.jdk>1.8</project.build.jdk>
<!--插件配置-->
<plugin.mybatis.generator>1.3.5</plugin.mybatis.generator>
<plugin.maven-compiler>3.1</plugin.maven-compiler>
<!--mybatis-generator配置文件-->
<mybatis.generator.generatorConfig.xml>${basedir}/src/test/resources/mybatis-generator.xml
</mybatis.generator.generatorConfig.xml>
<mybatis.generator.generatorConfig.properties>file:///${basedir}/src/test/resources/mybatis-generator.properties
</mybatis.generator.generatorConfig.properties>
<!-- 依赖版本 -->
<junit.version>4.12</junit.version>
<log4j.version>1.2.12</log4j.version>
</properties>
<dependencies>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
</project>
5.编辑配置选项(Edit Configurations)
配置参数
mybatis-generator:generate -e
6.点击生成
7.小结
整个配置过程比较简单,记录一下。
数据库odmins中user表的内容
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`uid` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`username` varchar(16) NOT NULL COMMENT '用户名',
`password` char(32) NOT NULL COMMENT '密码',
`salt` char(32) NOT NULL COMMENT '盐',
`nickname` varchar(32) DEFAULT NULL,
`email` varchar(32) DEFAULT NULL,
`phone` char(11) DEFAULT NULL,
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
SET FOREIGN_KEY_CHECKS=1;