mybatis 自動生成實體類、mapper.xml檔案
阿新 • • 發佈:2019-02-06
tip:
mybatis-generator不支援一對多、多對多對映,需要額外新增
在maven pom中新增 mybatis外掛
<!-- mybatis生成DAO外掛 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!--允許移動生成的檔案--> <verbose>true</verbose> <!--允許覆蓋生成的檔案--> <overwrite>true</overwrite> </configuration> </plugin>
在專案的/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> <!-- 引入配置檔案 --> <properties resource="init.properties"/> <!-- 指定資料連線驅動jar地址 --> <classPathEntry location="${classPath}" /> <!-- 一個數據庫一個context --> <context id="infoGuardian"> <!-- 註釋 --> <commentGenerator > <property name="suppressAllComments" value="false"/><!-- 是否取消註釋 --> <property name="suppressDate" value="true" /> <!-- 是否生成註釋代時間戳--> </commentGenerator> <!-- jdbc連線 --> <jdbcConnection driverClass="${jdbc_driver}" connectionURL="${jdbc_url}" userId="${jdbc_user}" password="${jdbc_password}" /> <!-- 型別轉換 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自動轉化以下型別(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成實體類地址 --> <javaModelGenerator targetPackage="com.oop.eksp.user.model" targetProject="${project}" > <!-- 是否在當前路徑下新加一層schema,eg:fase路徑com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false"/> <!-- 是否針對string型別的欄位在set的時候進行trim呼叫 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapxml檔案 --> <sqlMapGenerator targetPackage="com.oop.eksp.user.data" targetProject="${project}" > <!-- 是否在當前路徑下新加一層schema,eg:fase路徑com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- 生成mapxml對應client,也就是介面dao --> <javaClientGenerator targetPackage="com.oop.eksp.user.data" targetProject="${project}" type="XMLMAPPER" > <!-- 是否在當前路徑下新加一層schema,eg:fase路徑com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 配置表資訊 --> <table schema="${jdbc_user}" tableName="s_user" domainObjectName="UserEntity" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> <!-- schema即為資料庫名 tableName為對應的資料庫表 domainObjectName是要生成的實體類 enable*ByExample 是否生成 example類 --> <!-- 忽略列,不生成bean 欄位 --> <ignoreColumn column="FRED" /> <!-- 指定列的java資料型別 --> <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> </table> </context> </generatorConfiguration>
專案 右鍵-->run as --> maven bulid -->在goals中輸入mybatis-generator:generate