開發環境:
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 詳細:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>blog</artifactId> 7 <groupId>pers.kaloquan</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>blog-dao-api</artifactId> 13 14 <properties> 15 <!-- MySQL Connector 版本 --> 16 <mysql-connector.version>5.1.40</mysql-connector.version> 17 <!-- MyBatis Generator 版本 --> 18 <mybatis-generator.version>1.3.5</mybatis-generator.version> 19 20 <!-- 指定MyBatis Generator插件的配置文件 --> 21 <plugin.generator.configurationFile>${project.basedir}\src\main\resources\mybatis-generator.xml</plugin.generator.configurationFile> 22 <!-- 是否重寫已存在的文件 --> 23 <plugin.generator.overwrite>true</plugin.generator.overwrite> 24 <!-- 生成器targetProject的根目錄 --> 25 <generator.project.root>D:\IDEAProjects\blog</generator.project.root> 26 <!-- 生成器所需驅動包的路徑 --> 27 <generator.classpath>D:\Maven repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar</generator.classpath> 28 29 <!-- 驅動類 --> 30 <jdbc.driverClass>com.mysql.jdbc.Driver</jdbc.driverClass> 31 <!-- 鏈接地址 --> 32 <jdbc.url>jdbc:mysql://localhost:3306/blog?createDatabaseIfNotExist=true&useSSL=true&serverTimezone=UTC&passwordCharacterEncoding=UTF-8&characterEncoding=UTF-8</jdbc.url> 33 <!-- 用戶名 --> 34 <jdbc.userId>root</jdbc.userId> 35 <!-- 密碼 --> 36 <jdbc.password>root</jdbc.password> 37 </properties> 38 39 <dependencies> 40 <dependency> 41 <groupId>org.mybatis</groupId> 42 <artifactId>mybatis</artifactId> 43 <version>3.4.1</version> 44 </dependency> 45 46 <dependency> 47 <groupId>pers.kaloquan</groupId> 48 <artifactId>blog-model</artifactId> 49 </dependency> 50 </dependencies> 51 52 <build> 53 <plugins> 54 <plugin> 55 <groupId>org.mybatis.generator</groupId> 56 <artifactId>mybatis-generator-maven-plugin</artifactId> 57 <version>${mybatis-generator.version}</version> 58 <!-- 插件配置 --> 59 <configuration> 60 <configurationFile>${plugin.generator.configurationFile}</configurationFile> 61 <overwrite>${plugin.generator.overwrite}</overwrite> 62 </configuration> 63 <!-- 編譯之前需要執行的命令 --> 64 <executions> 65 <execution> 66 <id>Generate MyBatis Artifacts</id> 67 <goals> 68 <goal>generate</goal> 69 </goals> 70 </execution> 71 </executions> 72 </plugin> 73 </plugins> 74 </build> 75 </project> 查看配置
*注意:在properties標簽中聲明的屬性值可以在mybatis-generator.xml中使用!
mybatis-generator.xml 詳細:
位置:(根據自己需要放在某個地方,改變後在對應項目的pom.xml文件中配置MyBatis Generator插件)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE generatorConfiguration PUBLIC 3 "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > 5 <generatorConfiguration> 6 7 <!-- !!!! Driver Class Path !!!! --> 8 <classPathEntry location="${generator.classpath}"/> 9 10 <!-- 使用MyBatis3Simple,避免生成多余的代碼 --> 11 <context id="context" targetRuntime="MyBatis3Simple"> 12 <commentGenerator> 13 <property name="suppressAllComments" value="false"/> 14 <property name="suppressDate" value="true"/> 15 </commentGenerator> 16 17 <!-- !!!! Database Configurations !!!! --> 18 <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.url}" userId="${jdbc.userId}" password="${jdbc.password}"/> 19 20 <javaTypeResolver> 21 <property name="forceBigDecimals" value="false"/> 22 </javaTypeResolver> 23 24 <!-- !!!! Model Configurations !!!! --> 25 <javaModelGenerator targetPackage="blog.model" targetProject="${generator.project.root}\blog-model\src\main\java"> 26 <property name="enableSubPackages" value="false"/> 27 <property name="trimStrings" value="true"/> 28 </javaModelGenerator> 29 30 <!-- !!!! Mapper XML Configurations !!!! --> 31 <sqlMapGenerator targetPackage="mapper" targetProject="${generator.project.root}\blog-dao-impl\src\main\resources" > 32 <property name="enableSubPackages" value="false"/> 33 </sqlMapGenerator> 34 35 <!-- !!!! Mapper Interface Configurations !!!! --> 36 <!-- 使用MyBatis3Simple運行時,type只能為“ANNOTATEDMAPPER”或者“XMLMAPPER” --> 37 <!-- 這裡采用注解配置,而非XML配置 --> 38 <javaClientGenerator targetPackage="blog.dao" targetProject="${generator.project.root}\blog-dao-api\src\main\java" type="ANNOTATEDMAPPER"> 39 <property name="enableSubPackages" value="false"/> 40 </javaClientGenerator> 41 42 <!-- !!!! Table Configurations !!!! --> 43 <table tableName="user" schema="blog"> 44 <columnOverride column="id" property="id"/> 45 <columnOverride column="name" property="name"/> 46 </table> 47 </context> 48 </generatorConfiguration> 查看配置
不明確的地方或者有錯誤的地方,請指出!
參考資料:
Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J
MyBatis GeneratorXML Configuration File Reference
Running MyBatis Generator With Maven