Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x


Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x

 

==============================

蕃薯耀 2018年3月15日

http://www.cnblogs.com/fanshuyao/

 

一、Mybatis通用Mapper項目介紹:

https://github.com/abel533/Mapper

http://www.mybatis.tk/

 

Maven方式下載:

Xml代碼   收藏代碼
  1. <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper -->  
  2. <dependency>  
  3.     <groupId>tk.mybatis</groupId>  
  4.     <artifactId>mapper</artifactId>  
  5.     <version>3.5.3</version>  
  6. </dependency>  

 

 

 二、整合步驟:

 

1、第1步:

當然是引用相應的Jar包依賴,項目采用Maven方式,在pom.xml文件加上如下配置:

其中有Mybatis依賴Jar包,Mapper依賴Jar包,pagehelper分頁依賴Jar包(可選)

 

Xml代碼   收藏代碼
  1. <!-- mybatis-->  
  2.         <dependency>  
  3.             <groupId>org.mybatis</groupId>  
  4.             <artifactId>mybatis</artifactId>  
  5.             <version>${mybatis.version}</version>  
  6.         </dependency>  
  7.         <dependency>  
  8.             <groupId>org.mybatis</groupId>  
  9.             <artifactId>mybatis-spring</artifactId>  
  10.             <version>${mybatis.spring.version}</version>  
  11.         </dependency>  
  12.         <dependency>  
  13.             <groupId>org.mybatis.generator</groupId>  
  14.             <artifactId>mybatis-generator-core</artifactId>  
  15.             <version>${mybatis.generator.version}</version>  
  16.             <scope>provided</scope>  
  17.         </dependency>  
  18.         <!-- mybatis分頁插件 使用見:https://pagehelper.github.io/docs/howtouse/-->  
  19.         <dependency>  
  20.             <groupId>com.github.pagehelper</groupId>  
  21.             <artifactId>pagehelper</artifactId>  
  22.             <version>${pagehelper.version}</version>  
  23.         </dependency>  
  24.           
  25.         <!--通用Mapper,見:http://www.mybatis.tk/-->  
  26.         <dependency>  
  27.             <groupId>tk.mybatis</groupId>  
  28.             <artifactId>mapper</artifactId>  
  29.             <version>${mybatis.mapper.version}</version>  
  30.         </dependency>  

 

 2、第2步:與Spring整合

在Spring.xml配置文件加上掃描器,這個掃描器和Mybatis默認是不一樣的:

使用的類是:tk.mybatis.spring.mapper.MapperScannerConfigurer,包名不一樣。

 

Xml代碼   收藏代碼
  1. <!--配置掃描器,將Mybatis接口的實現加入到ioc容器 -->  
  2.  <!-- 使用通用Mapper,見:https://mapperhelper.github.io/docs/1.integration/ -->  
  3.  <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">  
  4.     <property name="basePackage" value="com.lqy.ssm.dao"></property>  
  5. </bean>  

 

 Spring.xml配置文件見附件

 

3、第3步:自動生成代碼文件的mybatis-mapper-generator.xml配置

 

在項目根目錄下建立一個mybatis-mapper-generator.xml文件,內容如下:

 

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.   
  6. <!-- 此文件為通用mapper生成代碼的配置 -->  
  7. <!-- 配置文件信息見:https://mapperhelper.github.io/docs/3.usembg/ -->  
  8. <generatorConfiguration>  
  9.   <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">  
  10.   
  11.     <plugin type="tk.mybatis.mapper.generator.MapperPlugin">  
  12.       <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>  
  13.       <!-- caseSensitive默認false,當數據庫表名區分大小寫時,可以將該屬性設置為true -->  
  14.       <!-- <property name="caseSensitive" value="true"/> -->  
  15.     </plugin>  
  16.   
  17.     <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
  18.                     connectionURL="jdbc:mysql://localhost:3306/study"  
  19.                     userId="root"  
  20.                     password="xxx">  
  21.     </jdbcConnection>  
  22.       
  23.     <!-- type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl" -->  
  24.     <javaTypeResolver>  
  25.         <property name="forceBigDecimals" value="false" />  
  26.     </javaTypeResolver>  
  27.   
  28.     <javaModelGenerator targetPackage="com.lqy.ssm.bean" targetProject=".\src\main\java"/>  
  29.   
  30.     <sqlMapGenerator targetPackage="mapper"  targetProject=".\src\main\resources"/>  
  31.   
  32.     <javaClientGenerator targetPackage="com.lqy.ssm.dao" targetProject=".\src\main\java" type="XMLMAPPER" />  
  33.   
  34.     <table tableName="%" >  
  35.         <generatedKey column="id" sqlStatement="Mysql" identity="true"/>  
  36.     </table>  
  37.       
  38.   </context>  
  39.     
  40. </generatorConfiguration>  

 

注意:需要根據自己的實際要求修改的配置項

jdbcConnection:修改為自己的數據庫連接

javaModelGenerator:實體bean生成的包名及位置,其中targetProject=".\src\main\java"的.\表示當前項目的位置

sqlMapGenerator:mapper.xml文件生成的位置,其中targetPackage表示目錄

javaClientGenerator:通用dao生成的位置

table:生成的表名,其中<table tableName="%" >使用通配符%表示所有表

 

4、第4步:自動生成代碼Java類

 

在項目的src建立一個代碼生成類,如:MybatisGenerator,代碼內容為:

Java代碼   收藏代碼
  1. import java.io.File;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4.   
  5. import org.mybatis.generator.api.MyBatisGenerator;  
  6. import org.mybatis.generator.config.Configuration;  
  7. import org.mybatis.generator.config.xml.ConfigurationParser;  
  8. import org.mybatis.generator.internal.DefaultShellCallback;  
  9.   
  10. /** 
  11.  * 官網代碼見:http://www.mybatis.org/generator/running/runningWithJava.html 
  12.  * 
  13.  */  
  14. public class MybatisGenerator {  
  15.   
  16.     public static void main(String[] args) throws Exception {  
  17.         List<String> warnings = new ArrayList<String>();  
  18.         boolean overwrite = true;  
  19.         File configFile = new File("mybatis-mapper-generator.xml");  
  20.         ConfigurationParser cp = new ConfigurationParser(warnings);  
  21.         Configuration config = cp.parseConfiguration(configFile);  
  22.         DefaultShellCallback callback = new DefaultShellCallback(overwrite);  
  23.         MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);  
  24.         myBatisGenerator.generate(null);  
  25.         System.out.println("===============生成代碼執行完畢=================");  
  26.     }  
  27.       
  28. }  

 

主要是File configFile = new File("mybatis-mapper-generator.xml");,對應上面的配置文件

 

 

5、第5步:結果查看

Java代碼運行完后,就會在項目中自動生成相應的代碼

 

生成的mapper文件:只有resultMap,沒有方法,因為提供了通用方法

Xml代碼   收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  3. <mapper namespace="com.lqy.ssm.dao.UserExtMapper">  
  4.   <resultMap id="BaseResultMap" type="com.lqy.ssm.bean.UserExt">  
  5.     <!-- 
  6.       WARNING - @mbg.generated 
  7.     -->  
  8.     <id column="id" jdbcType="INTEGER" property="id" />  
  9.     <result column="user_id" jdbcType="INTEGER" property="userId" />  
  10.     <result column="qq" jdbcType="VARCHAR" property="qq" />  
  11.     <result column="address" jdbcType="VARCHAR" property="address" />  
  12.   </resultMap>  
  13. </mapper>  

 

 生成的dao接口文件:繼承了Mapper接口,里面實現了很多方法

Java代碼   收藏代碼
  1. import com.lqy.ssm.bean.UserExt;  
  2. import tk.mybatis.mapper.common.Mapper;  
  3.   
  4. public interface UserExtMapper extends Mapper<UserExt> {  
  5. }  

 

 使用方式:直接注入即可使用

Java代碼   收藏代碼
  1. @Autowired  
  2. private UserExtMapper userExtMapper;  

 

 6、補充:所有接口方法見:

https://mapperhelper.github.io/all/

 

 

Mapper3接口有兩種形式,一種是提供了一個方法的接口。還有一種是不提供方法,但是繼承了多個單方法的接口,一般是某類方法的集合。

例如SelectMapper<T>是一個單方法的接口,BaseSelectMapper<T>是一個繼承了4個基礎查詢方法的接口。

基礎接口

Select

接口:SelectMapper<T>
方法:List<T> select(T record);
說明:根據實體中的屬性值進行查詢,查詢條件使用等號

接口:SelectByPrimaryKeyMapper<T>
方法:T selectByPrimaryKey(Object key);
說明:根據主鍵字段進行查詢,方法參數必須包含完整的主鍵屬性,查詢條件使用等號

接口:SelectAllMapper<T>
方法:List<T> selectAll();
說明:查詢全部結果,select(null)方法能達到同樣的效果

接口:SelectOneMapper<T>
方法:T selectOne(T record);
說明:根據實體中的屬性進行查詢,只能有一個返回值,有多個結果是拋出異常,查詢條件使用等號

接口:SelectCountMapper<T>
方法:int selectCount(T record);
說明:根據實體中的屬性查詢總數,查詢條件使用等號

Insert

接口:InsertMapper<T>
方法:int insert(T record);
說明:保存一個實體,null的屬性也會保存,不會使用數據庫默認值

接口:InsertSelectiveMapper<T>
方法:int insertSelective(T record);
說明:保存一個實體,null的屬性不會保存,會使用數據庫默認值

Update

接口:UpdateByPrimaryKeyMapper<T>
方法:int updateByPrimaryKey(T record);
說明:根據主鍵更新實體全部字段,null值會被更新

接口:UpdateByPrimaryKeySelectiveMapper<T>
方法:int updateByPrimaryKeySelective(T record);
說明:根據主鍵更新屬性不為null的值

Delete

接口:DeleteMapper<T>
方法:int delete(T record);
說明:根據實體屬性作為條件進行刪除,查詢條件使用等號

接口:DeleteByPrimaryKeyMapper<T>
方法:int deleteByPrimaryKey(Object key);
說明:根據主鍵字段進行刪除,方法參數必須包含完整的主鍵屬性

base 組合接口

接口:BaseSelectMapper<T>
方法:包含上面Select的4個方法

接口:BaseInsertMapper<T>
方法:包含上面Insert的2個方法

接口:BaseUpdateMapper<T>
方法:包含上面Update的2個方法

接口:BaseDeleteMapper<T>
方法:包含上面Delete的2個方法

CRUD 組合接口

接口:BaseMapper<T>
方法:繼承了base組合接口中的4個組合接口,包含完整的CRUD方法

Example 方法

接口:SelectByExampleMapper<T>
方法:List<T> selectByExample(Object example);
說明:根據Example條件進行查詢
重點:這個查詢支持通過Example類指定查詢列,通過selectProperties方法指定查詢列

接口:SelectCountByExampleMapper<T>
方法:int selectCountByExample(Object example);
說明:根據Example條件進行查詢總數

接口:UpdateByExampleMapper<T>
方法:int updateByExample(@Param("record") T record, @Param("example") Object example);
說明:根據Example條件更新實體record包含的全部屬性,null值會被更新

接口:UpdateByExampleSelectiveMapper<T>
方法:int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
說明:根據Example條件更新實體record包含的不是null的屬性值

接口:DeleteByExampleMapper<T>
方法:int deleteByExample(Object example);
說明:根據Example條件刪除數據

Example 組合接口

接口:ExampleMapper<T>
方法:包含上面Example中的5個方法

Condition 方法

Condition方法和Example方法作用完全一樣,只是為了避免Example帶來的歧義,提供的的Condition方法

接口:SelectByConditionMapper<T>
方法:List<T> selectByCondition(Object condition);
說明:根據Condition條件進行查詢

接口:SelectCountByConditionMapper<T>
方法:int selectCountByCondition(Object condition);
說明:根據Condition條件進行查詢總數

接口:UpdateByConditionMapper<T>
方法:int updateByCondition(@Param("record") T record, @Param("example") Object condition);
說明:根據Condition條件更新實體record包含的全部屬性,null值會被更新

接口:UpdateByConditionSelectiveMapper<T>
方法:int updateByConditionSelective(@Param("record") T record, @Param("example") Object condition);
說明:根據Condition條件更新實體record包含的不是null的屬性值

接口:DeleteByConditionMapper<T>
方法:int deleteByCondition(Object condition);
說明:根據Condition條件刪除數據

Condition 組合接口

接口:ConditionMapper<T>
方法:包含上面Condition中的5個方法

RowBounds

默認為內存分頁,可以配合PageHelper實現物理分頁

接口:SelectRowBoundsMapper<T>
方法:List<T> selectByRowBounds(T record, RowBounds rowBounds);
說明:根據實體屬性和RowBounds進行分頁查詢

接口:SelectByExampleRowBoundsMapper<T>
方法:List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds);
說明:根據example條件和RowBounds進行分頁查詢

接口:SelectByConditionRowBoundsMapper<T>
方法:List<T> selectByConditionAndRowBounds(Object condition, RowBounds rowBounds);
說明:根據example條件和RowBounds進行分頁查詢,該方法和selectByExampleAndRowBounds完全一樣,只是名字改成了Condition

RowBounds 組合接口

接口:RowBoundsMapper<T>
方法:包含上面RowBounds中的前兩個方法,不包含selectByConditionAndRowBounds

special 特殊接口

這些接口針對部分數據庫設計,不是所有數據庫都支持

接口:InsertListMapper<T>
方法:int insertList(List<T> recordList);
說明:批量插入,支持批量插入的數據庫可以使用,例如MySQL,H2等,另外該接口限制實體包含id屬性並且必須為自增列

接口:InsertUseGeneratedKeysMapper<T>
方法:int insertUseGeneratedKeys(T record);
說明:插入數據,限制為實體包含id屬性並且必須為自增列,實體配置的主鍵策略無效

MySQL 專用

接口:MySqlMapper<T>
繼承方法:int insertList(List<T> recordList);
繼承方法:int insertUseGeneratedKeys(T record);
說明:該接口不包含方法,繼承了special中的InsertListMapper<T>InsertUseGeneratedKeysMapper<T>

SqlServer 專用

由於sqlserver中插入自增主鍵時,不能使用null插入,不能在insert語句中出現id

注意SqlServer的兩個特有插入方法都使用了

@Options(useGeneratedKeys = true, keyProperty = "id")

這就要求表的主鍵為id,且為自增,如果主鍵不叫id可以看高級教程中的解決方法。

另外這倆方法和base中的插入方法重名,不能同時存在!

如果某種數據庫和SqlServer這里類似,也可以使用這些接口(需要先測試)。

接口:InsertMapper
方法:int insert(T record);
說明:插入數據庫,null值也會插入,不會使用列的默認值

接口:InsertSelectiveMapper
方法:int insertSelective(T record);
說明:插入數據庫,null的屬性不會保存,會使用數據庫默認值

接口:SqlServerMapper
說明:這是上面兩個接口的組合接口。

Ids 接口

通過操作ids字符串進行操作,ids 如 “1,2,3” 這種形式的字符串,這個方法要求實體類中有且只有一個帶有@Id注解的字段,否則會拋出異常。

接口:SelectByIdsMapper
方法:List<T> selectByIds(String ids)
說明:根據主鍵字符串進行查詢,類中只有存在一個帶有@Id注解的字段

接口:DeleteByIdsMapper
方法:int deleteByIds(String ids)
說明:根據主鍵字符串進行刪除,類中只有存在一個帶有@Id注解的字段

Ids 組合接口

接口:IdsMapper<T>
方法:包含上面Ids中的前兩個方法

Mapper<T> 接口

接口:Mapper<T>
該接口兼容Mapper2.x版本,繼承了BaseMapper<T>ExampleMapper<T>RowBoundsMapper<T>三個組合接口。

 

 

福利,福利,福利

分享一個springMvc+spring+Mybatis+pageHelper+druid整合后的完整項目:

ssm-mapper-整合通用mapper插件.zip:見http://fanshuyao.iteye.com/blog/2413143

 

 

==============================

蕃薯耀 2018年3月15日

http://www.cnblogs.com/fanshuyao/


免責聲明!

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



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