在idea 中使用 mybatis的 mybatis-generator-maven-plugin 能夠根據數據庫 生成 dao層,pojo類,Mapper文件。java
一: 在 pom.xml 中添加相關插件依賴。mysql
1 <!-- MySQL 鏈接驅動依賴 --> 2 <dependency> 3 <groupId>mysql</groupId> 4 <artifactId>mysql-connector-java</artifactId> 5 </dependency> 6 7 <!-- Junit --> 8 <dependency> 9 <groupId>junit</groupId> 10 <artifactId>junit</artifactId> 11 <version>4.12</version> 12 </dependency> 13 <dependency> 14 <groupId>org.mybatis.generator</groupId> 15 <artifactId>mybatis-generator-core</artifactId> 16 <version>1.3.2</version> 17 </dependency> 18 <dependency> 19 <groupId>log4j</groupId> 20 <artifactId>log4j</artifactId> 21 <version>1.2.17</version> 22 </dependency> 23 <dependency> 24 <groupId>org.mybatis</groupId> 25 <artifactId>mybatis</artifactId> 26 <version>3.2.6</version> 27 </dependency> 28 </dependencies>
1 <plugin> 2 <groupId>org.mybatis.generator</groupId> 3 <artifactId>mybatis-generator-maven-plugin</artifactId> 4 <version>1.3.5</version> 5 <configuration> 6 <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> 7 <verbose>true</verbose> 8 <overwrite>true</overwrite> 9 </configuration> 10 11 </plugin>
二:建立 generatorConfig.xml 配置文件spring
<?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="generator.properties"></properties> <!--二.在MBG工做的時候,須要額外加載的依賴包 location屬性指明加載jar/zip包的全路徑--> <classPathEntry location="${jdbc.driverLocation}"/> <!-- 三:context:生成一組對象的環境 id:必選,上下文id,用於在生成錯誤時提示 defaultModelType:指定生成對象的樣式 1,conditional:相似hierarchical; 2,flat:全部內容(主鍵,blob)等所有生成在一個對象中; 3,hierarchical:主鍵生成一個XXKey對象(key class),Blob等單獨生成一個對象,其餘簡單屬性在一個對象中(record class) targetRuntime: 1,MyBatis3:默認的值,生成基於MyBatis3.x以上版本的內容,包括XXXBySample; 2,MyBatis3Simple:相似MyBatis3,只是不生成XXXBySample; introspectedColumnImpl:類全限定名,用於擴展MBG --> <context id="tables" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/> <!-- 不生成註釋 --> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--jdbc的數據庫鏈接 --> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userName}" password="${jdbc.password}"> <!-- 這裏面也能夠設置property屬性,每個property屬性都設置到配置的Driver上 --> </jdbcConnection> <!-- java類型處理器 用於處理DB中的類型到Java中的類型,默認使用JavaTypeResolverDefaultImpl; 注意一點,默認會先嚐試使用Integer,Long,Short等來對應DECIMAL和 NUMERIC數據類型; --> <javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl"> <!-- true:使用BigDecimal對應DECIMAL和 NUMERIC數據類型 false:默認, scale>0;length>18:使用BigDecimal; scale=0;length[10,18]:使用Long; scale=0;length[5,9]:使用Integer; scale=0;length<5:使用Short; --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- java模型建立器,是必需要的元素 負責:1,key類(見context的defaultModelType);2,java類;3,查詢類 targetPackage:生成的類要放的包,真實的包受enableSubPackages屬性控制; targetProject:目標項目,指定一個存在的目錄下,生成的內容會放到指定目錄中,若是目錄不存在,MBG不會自動建目錄 --> <javaModelGenerator targetPackage="com.springweekend.week.pojo" targetProject="src/main/java"> <!-- 是否對model添加 構造函數 --> <property name="constructorBased" value="true"/> <!--是否容許子包,即targetPackage.schemaName.tableName--> <!-- 在targetPackage的基礎上,根據數據庫的schema再生成一層package,最終生成的類放在這個package下,默認爲false --> <property name="enableSubPackages" value="false"/> <!-- 創建的Model對象是否 不可改變 即生成的Model對象不會有 setter方法,只有構造方法 --> <property name="immutable" value="true"/> <!-- 給Model添加一個父類 ,設置一個根對象, 若是設置了這個根對象,那麼生成的keyClass或者recordClass會繼承這個類;在Table的rootClass屬性中能夠覆蓋該選項 注意:若是在key class或者record class中有root class相同的屬性,MBG就不會從新生成這些屬性了,包括: 1,屬性名相同,類型相同,有相同的getter/setter方法; --> <!--<property name="rootClass" value="com.foo.louis.Hello"/>--> <!-- 是否對類CHAR類型的列的數據進行trim操做 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--Mapper映射文件生成所在的目錄 爲每個數據庫的表生成對應的SqlMap文件org.louis.hometutor.domain --> <sqlMapGenerator targetPackage="com.springweekend.week.sql" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 客戶端代碼,生成易於使用的針對Model對象和XML配置文件 的代碼 type="ANNOTATEDMAPPER",生成Java Model 和基於註解的Mapper對象 type="MIXEDMAPPER",生成基於註解的Java Model 和相應的Mapper對象 type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口 --> <javaClientGenerator targetPackage="com.springweekend.week.dao" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 指定數據庫表 此處還有不少自定義配置,根據我的需求進行設置便可 --> <!--<table tableName="user"></table>--> <!--<table tableName="order"></table>--> <!--<table tableName="detail"></table>--> <!--<table tableName="item"></table>--> <!-- 選擇一個table來生成相關文件,能夠有一個或多個table,必需要有table元素 選擇的table會生成一下文件: 1,SQL map文件 2,生成一個主鍵類; 3,除了BLOB和主鍵的其餘字段的類; 4,包含BLOB的類; 5,一個用戶生成動態查詢的條件類(selectByExample, deleteByExample),可選; 6,Mapper接口(可選) tableName(必要):要生成對象的表名; 注意:大小寫敏感問題。正常狀況下,MBG會自動的去識別數據庫標識符的大小寫敏感度,在通常狀況下,MBG會 根據設置的schema,catalog或tablename去查詢數據表,按照下面的流程: 1,若是schema,catalog或tablename中有空格,那麼設置的是什麼格式,就精確的使用指定的大小寫格式去查詢; 2,不然,若是數據庫的標識符使用大寫的,那麼MBG自動把表名變成大寫再查找; 3,不然,若是數據庫的標識符使用小寫的,那麼MBG自動把表名變成小寫再查找; 4,不然,使用指定的大小寫格式查詢; 另外的,若是在建立表的時候,使用的""把數據庫對象規定大小寫,就算數據庫標識符是使用的大寫,在這種狀況下也會使用給定的大小寫來建立表名; 這個時候,請設置delimitIdentifiers="true"便可保留大小寫格式; 可選: 1,schema:數據庫的schema; 2,catalog:數據庫的catalog; 3,alias:爲數據表設置的別名,若是設置了alias,那麼生成的全部的SELECT SQL語句中,列名會變成:alias_actualColumnName 4,domainObjectName:生成的domain類的名字,若是不設置,直接使用表名做爲domain類的名字;能夠設置爲somepck.domainName,那麼會自動把domainName類再放到somepck包裏面; 5,enableInsert(默認true):指定是否生成insert語句; 6,enableSelectByPrimaryKey(默認true):指定是否生成按照主鍵查詢對象的語句(就是getById或get); 7,enableSelectByExample(默認true):MyBatis3Simple爲false,指定是否生成動態查詢語句; 8,enableUpdateByPrimaryKey(默認true):指定是否生成按照主鍵修改對象的語句(即update); 9,enableDeleteByPrimaryKey(默認true):指定是否生成按照主鍵刪除對象的語句(即delete); 10,enableDeleteByExample(默認true):MyBatis3Simple爲false,指定是否生成動態刪除語句; 11,enableCountByExample(默認true):MyBatis3Simple爲false,指定是否生成動態查詢總條數語句(用於分頁的總條數查詢); 12,enableUpdateByExample(默認true):MyBatis3Simple爲false,指定是否生成動態修改語句(只修改對象中不爲空的屬性); 13,modelType:參考context元素的defaultModelType,至關於覆蓋; 14,delimitIdentifiers:參考tableName的解釋,注意,默認的delimitIdentifiers是雙引號,若是相似MYSQL這樣的數據庫,使用的是`(反引號,那麼還須要設置context的beginningDelimiter和endingDelimiter屬性) 15,delimitAllColumns:設置是否全部生成的SQL中的列名都使用標識符引發來。默認爲false,delimitIdentifiers參考context的屬性 注意,table裏面不少參數都是對javaModelGenerator,context等元素的默認屬性的一個複寫; --> <table tableName="student" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <!-- 若是設置爲true,生成的model類會直接使用column自己的名字,而不會再使用駝峯命名方法,好比BORN_DATE,生成的屬性名字就是BORN_DATE,而不會是bornDate --> <property name="useActualColumnNames" value="flase"/> </table> </context> </generatorConfiguration>
數據庫配置文件 application.propertiessql
jdbc.driverLocation=E:\\software\\maven3.5\\repository\\mysql\\mysql-connector-java\\8.0.15\\mysql-connector-java-8.0.15.jar jdbc.driverClass=com.mysql.cj.jdbc.Driver jdbc.connectionURL=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true jdbc.userName=root jdbc.password=123789
這是添加後控制器輸出日誌:數據庫
[INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.springweekend:week >----------------------- [INFO] Building week 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- mybatis-generator-maven-plugin:1.3.5:generate (default-cli) @ week --- [INFO] Connecting to the Database [INFO] Introspecting table student [INFO] Generating Record class for table student [INFO] Generating Mapper Interface for table student [INFO] Generating SQL Map for table student [INFO] Saving file StudentMapper.xml [INFO] Saving file Student.java [INFO] Saving file StudentMapper.java [WARNING] Existing file E:\software\week\src\main\java\com\springweekend\week\pojo\Student.java was overwritten [WARNING] Existing file E:\software\week\src\main\java\com\springweekend\week\dao\StudentMapper.java was overwritten [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.918 s [INFO] Finished at: 2019-04-12T18:06:46+08:00 [INFO] ------------------------------------------------------------------------
若是不添加的話,就會出現警告: mybatis
[INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.springweekend:week >----------------------- [INFO] Building week 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- mybatis-generator-maven-plugin:1.3.5:generate (default-cli) @ week --- [INFO] Connecting to the Database [INFO] Introspecting table student [INFO] Generating Record class for table student [INFO] Generating Mapper Interface for table student [INFO] Generating SQL Map for table student [INFO] Generating Record class for table student [INFO] Generating Mapper Interface for table student [INFO] Generating SQL Map for table student [INFO] Saving file StudentMapper.xml [INFO] Saving file StudentMapper.xml [INFO] Saving file Student.java [INFO] Saving file StudentMapper.java [INFO] Saving file Student.java [INFO] Saving file StudentMapper.java [WARNING] Table Configuration student matched more than one table (test..student,nba..student) [WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete [WARNING] Cannot obtain primary key information from the database, generated objects may be incomplete [WARNING] Existing file E:\software\week\src\main\java\com\springweekend\week\pojo\Student.java was overwritten [WARNING] Existing file E:\software\week\src\main\java\com\springweekend\week\dao\StudentMapper.java was overwritten [WARNING] Existing file E:\software\week\src\main\java\com\springweekend\week\pojo\Student.java was overwritten [WARNING] Existing file E:\software\week\src\main\java\com\springweekend\week\dao\StudentMapper.java was overwritten [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.197 s [INFO] Finished at: 2019-04-12T18:03:51+08:00 [INFO] ------------------------------------------------------------------------ Process finished with exit code 0
另外,添加命令行:-Dmybatis.generator.overwrite=true(若是數據庫中表改動了,就能夠對建立的dao ,pojo,Mapper文件重載)app
mybatis-generator:generate(顯示運行的詳細信息)dom
啓動:maven