mybatis 逆向工程(通過數據庫表針對單表自動生成mybatis執行所需要的代碼)


mybatis需要程序員自己編寫sql語句,mybatis官方提供逆向工程,可以針對單表自動生成mybatis執行所需要的代碼(mapper.java、mapper.xml、pojo…),可以讓程序員將更多的精力放在繁雜的業務邏輯上。

        企業實際開發中,常用的逆向工程方式:由數據庫的表生成java代碼。

        之所以強調單表兩個字,是因為Mybatis逆向工程生成的Mapper所進行的操作都是針對單表的,也許你可能會覺得那這就有點雞肋了,但是在大型項目中,很少有復雜的多表關聯查詢,所以作用還是很大的。

這是項目最終的效果:

 

 

一、注入maven依賴  ==》

 

二、生成  generatorConfig.xml ==》 

 

 

 三、運行

 

 

源碼:

generatorConfig.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE generatorConfiguration
 3         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 4         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 5 <generatorConfiguration>
 6     <context id="test" targetRuntime="MyBatis3">
 7         <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
 8         <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
 9         <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
10         <commentGenerator>
11             <!-- 這個元素用來去除指定生成的注釋中是否包含生成的日期 false:表示保護 -->
12             <!-- 如果生成日期,會造成即使修改一個字段,整個實體類所有屬性都會發生變化,不利於版本控制,所以設置為true -->
13             <property name="suppressDate" value="true" />
14             <!-- 是否去除自動生成的注釋 true:是 : false:否 -->
15             <property name="suppressAllComments" value="true" />
16         </commentGenerator>
17         <!--數據庫鏈接URL,用戶名、密碼 -->
18         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
19                         connectionURL="jdbc:mysql://localhost:3310/mybatis" userId="root"
20                         password="123456">
21         </jdbcConnection>
22         <javaTypeResolver>
23             <!-- This property is used to specify whether MyBatis Generator should
24                 force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
25             <property name="forceBigDecimals" value="false" />
26         </javaTypeResolver>
27         <!-- 生成模型的包名和位置 -->
28         <javaModelGenerator targetPackage="com.etc.entity"
29                             targetProject="target">
30             <property name="enableSubPackages" value="true" />
31             <property name="trimStrings" value="true" />
32         </javaModelGenerator>
33         <!-- 生成映射文件的包名和位置 -->
34         <sqlMapGenerator targetPackage="com.etc.dao"
35                          targetProject="target">
36             <property name="enableSubPackages" value="true" />
37         </sqlMapGenerator>
38         <!-- 生成DAO的包名和位置 -->
39         <javaClientGenerator type="XMLMAPPER"
40                              targetPackage="com.etc.dao"
41                              targetProject="target">
42             <property name="enableSubPackages" value="true" />
43         </javaClientGenerator>
44 
45         <!-- 要生成哪些表 -->
46         <table tableName="t_house" domainObjectName="House"></table>
47 
48     </context>
49 </generatorConfiguration>

 

pom.xml:

 1    <plugins>
 2       <plugin>
 3         <groupId>org.mybatis.generator</groupId>
 4         <artifactId>mybatis-generator-maven-plugin</artifactId>
 5         <version>1.3.2</version>
 6         <dependencies>
 7           <dependency>
 8             <groupId>mysql</groupId>
 9             <artifactId>mysql-connector-java</artifactId>
10             <version>5.1.22</version>
11           </dependency>
12         </dependencies>
13         <configuration>
14           <!--配置文件的路徑-->
15           <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
16           <overwrite>true</overwrite>
17         </configuration>
18       </plugin>
19     </plugins>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM