开发环境:
JDK:8u102
Maven:3.3.9
MySQL:5.7.10
MySQL Connector:5.1.40
IDE:IntelliJ IDEA 2016
MyBatis:3.4.1
MyBatis Generator:1.3.5
项目案例:
个人博客
项目结构:
blog-dao-impl模块:只是考虑到多个实现层而已。象征性的创建在这里。
项目依赖关系:
数据库详细:
blog-dao-api pom.xml 详细:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>blog</artifactId>
<groupId>pers.kaloquan</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>blog-dao-api</artifactId> <properties>
<!-- MySQL Connector 版本 -->
<mysql-connector.version>5.1.40</mysql-connector.version>
<!-- MyBatis Generator 版本 -->
<mybatis-generator.version>1.3.5</mybatis-generator.version> <!-- 指定MyBatis Generator插件的配置文件 -->
<plugin.generator.configurationFile>${project.basedir}\src\main\resources\mybatis-generator.xml</plugin.generator.configurationFile>
<!-- 是否重写已存在的文件 -->
<plugin.generator.overwrite>true</plugin.generator.overwrite>
<!-- 生成器targetProject的根目录 -->
<generator.project.root>D:\IDEAProjects\blog</generator.project.root>
<!-- 生成器所需驱动包的路径 -->
<generator.classpath>D:\Maven repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar</generator.classpath> <!-- 驱动类 -->
<jdbc.driverClass>com.mysql.jdbc.Driver</jdbc.driverClass>
<!-- 链接地址 -->
<jdbc.url>jdbc:mysql://localhost:3306/blog?createDatabaseIfNotExist=true&useSSL=true&serverTimezone=UTC&passwordCharacterEncoding=UTF-8&characterEncoding=UTF-8</jdbc.url>
<!-- 用户名 -->
<jdbc.userId>root</jdbc.userId>
<!-- 密码 -->
<jdbc.password>root</jdbc.password>
</properties> <dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency> <dependency>
<groupId>pers.kaloquan</groupId>
<artifactId>blog-model</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${mybatis-generator.version}</version>
<!-- 插件配置 -->
<configuration>
<configurationFile>${plugin.generator.configurationFile}</configurationFile>
<overwrite>${plugin.generator.overwrite}</overwrite>
</configuration>
<!-- 编译之前需要执行的命令 -->
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
查看配置
*注意:在properties标签中声明的属性值可以在mybatis-generator.xml中使用!
mybatis-generator.xml 详细:
位置:(根据自己需要放在某个地方,改变后在对应项目的pom.xml文件中配置MyBatis Generator插件)
<?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> <!-- !!!! Driver Class Path !!!! -->
<classPathEntry location="${generator.classpath}"/> <!-- 使用MyBatis3Simple,避免生成多余的代码 -->
<context id="context" targetRuntime="MyBatis3Simple">
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="true"/>
</commentGenerator> <!-- !!!! Database Configurations !!!! -->
<jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.url}" userId="${jdbc.userId}" password="${jdbc.password}"/> <javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver> <!-- !!!! Model Configurations !!!! -->
<javaModelGenerator targetPackage="blog.model" targetProject="${generator.project.root}\blog-model\src\main\java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator> <!-- !!!! Mapper XML Configurations !!!! -->
<sqlMapGenerator targetPackage="mapper" targetProject="${generator.project.root}\blog-dao-impl\src\main\resources" >
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator> <!-- !!!! Mapper Interface Configurations !!!! -->
<!-- 使用MyBatis3Simple运行时,type只能为“ANNOTATEDMAPPER”或者“XMLMAPPER” -->
<!-- 这里采用注解配置,而非XML配置 -->
<javaClientGenerator targetPackage="blog.dao" targetProject="${generator.project.root}\blog-dao-api\src\main\java" type="ANNOTATEDMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator> <!-- !!!! Table Configurations !!!! -->
<table tableName="user" schema="blog">
<columnOverride column="id" property="id"/>
<columnOverride column="name" property="name"/>
</table>
</context>
</generatorConfiguration>
查看配置
不明确的地方或者有错误的地方,请指出!
参考资料:
Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J