今天在弄mybatis-generator自動生成代碼的測試,但最後發現只能生成mapper、entities兩層代碼,後面又換成了mybatisplus,它可以生成mapper、entities、services、Controller層,比mybatis-generator更完善,但還是記錄下mybatis-generator的配置
一、pom.xml引入相關包
<build> <plugins> <!-- 自動生成代碼 插件 --> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-maven-plugin --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>
二、在src/main/resources下添加generatorConfig.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> <!--從application.properties導入屬性配置--> <properties resource="application.properties"/> <!-- 數據庫連接器的jar包位置--> <classPathEntry location="/Users/qc/.m2/repository/org/postgresql/postgresql/42.2.8/postgresql-42.2.8.jar"/> <!-- 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="context" targetRuntime="MyBatis3Simple"> <commentGenerator> <!-- 是否取消註釋 --> <property name="suppressAllComments" value="true"/> <!-- 是否生成註釋代時間戳--> <property name="suppressDate" value="true"/> </commentGenerator> <!--數據源配置--> <jdbcConnection userId="${spring.datasource.username}" password="${spring.datasource.password}" driverClass="${spring.datasource.driverClassName}" connectionURL="${spring.datasource.url}"/> <!-- java類型處理器 用於處理DB中的類型到Java中的類型,默認使用JavaTypeResolverDefaultImpl; 注意一點,默認會先嚐試使用Integer,Long,Short等來對應DECIMAL和 NUMERIC數據類型; --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- java模型創建器,是必須要的元素 負責:1,key類(見context的defaultModelType);2,java類;3,查詢類 targetPackage:生成的類要放的包,真實的包受enableSubPackages屬性控制; targetProject:目標項目,指定一個存在的目錄下,生成的內容會放到指定目錄中,如果目錄不存在,MBG不會自動建目錄 --> <javaModelGenerator targetPackage="com.auto.entities" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成SQL map的XML文件生成器, 注意,在Mybatis3之後,我們可以使用mapper.xml文件+Mapper接口(或者不用mapper接口), 或者只使用Mapper接口+Annotation,所以,如果 javaClientGenerator配置中配置了需要生成XML的話,這個元素就必須配置 targetPackage/targetProject:同javaModelGenerator --> <sqlMapGenerator targetPackage="com.auto.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 對於mybatis來說,即生成Mapper接口,注意,如果沒有配置該元素,那麼默認不會生成Mapper接口 targetPackage/targetProject:同javaModelGenerator type:選擇怎麼生成mapper接口(在MyBatis3/MyBatis3Simple下): 1,ANNOTATEDMAPPER:會生成使用Mapper接口+Annotation的方式創建(SQL生成在annotation中),不會生成對應的XML; 2,MIXEDMAPPER:使用混合配置,會生成Mapper接口,並適當添加合適的Annotation,但是XML會生成在XML中; 3,XMLMAPPER:會生成Mapper接口,接口完全依賴XML; 注意,如果context是MyBatis3Simple:只支持ANNOTATEDMAPPER和XMLMAPPER--> <javaClientGenerator targetPackage="com.auto.mapper" type="ANNOTATEDMAPPER" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 選擇一個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 schema="public" tableName="sys_user" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> </table> </context> </generatorConfiguration>