mybatis generator(基於maven、mysql)
使用maven外掛和mybatis generator 生成mybatis需要的model、mapper-xml、mapper-interface.
話不多說,直接上配置檔案
一、配置pom.xml檔案
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.test.my</groupId>
<artifactId>my-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>my-dal</artifactId>
<dependencies>
<dependency>
<groupId>cn.ticai.common</groupId>
<artifactId>ticai-commons-dal</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
</dependencies>
<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!-- mybatis-generator.xml 存放路徑>
<configurationFile>src/test/resources/generator/mybatis-generator.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
二、配置 mybatis-generator.xml 檔案
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>
<!--資料庫驅動 -->
<context id="generatorTables" targetRuntime="MyBatis3">
<property name="autoDelimitKeywords" value="false" />
<property name="javaFileEncoding" value="UTF-8" />
<property name="javaFormatter"
value="org.mybatis.generator.api.dom.DefaultJavaFormatter" />
<property name="xmlFormatter"
value="org.mybatis.generator.api.dom.DefaultXmlFormatter" />
<!-- mapper存放路徑-->
<plugin type="org.mybatis.generator.plugins.MapperConfigPlugin">
<property name="fileName" value="mybatis-config.xml" />
<property name="targetPackage" value="/" />
<property name="targetProject" value="src/main/resources" />
</plugin>
<!-- <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
<property name="searchString" value="Example" /> <property name="replaceString"
value="Criteria" /> </plugin> -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/db_test" userId="test"
password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- model生成路徑 -->
<javaModelGenerator targetPackage="cn.test.my.dal.audit.dao.mybatis.model"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- mapper-xml 生成路徑 -->
<sqlMapGenerator targetPackage="cn.test.my.dal.audit.dao.mybatis.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- mapper-interface 生成路徑 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="cn.test.my.dal.audit.dao.mybatis" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 需要自動生成相關檔案的表 -->
<table tableName="t_auditinfo" domainObjectName="AuditInfo"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true" />
<property name="mergeable" value="true" />
</table>
</context>
</generatorConfiguration>
三、建立 mybatis-config.xml 檔案
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--
This file is generated by MyBatis Generator.
This file is the shell of a Mapper Config file - in many cases you will need to add
to this file before it is usable by MyBatis.
This file was generated on Wed Mar 15 14:45:43 CST 2017.
-->
<mappers>
<mapper resource="cn/ticai/tcgl/dal/audit/dao/mybatis/mapper/AuditInfoMapper.xml" />
</mappers>
</configuration>
四、執行命令
方式1:
(1)、右鍵專案 -> Run as -> maven build ->
goals引數:mybatis-generator:generate -> Run
(2)、右鍵 專案 -> Refresh 重新整理專案
方式2:
(1)、cmd,切換到專案目錄下,執行命令:
mvn mybatis-generator:generate
(2)、右鍵 專案 -> Refresh 重新整理專案