MyBatis映射器mapper快速入門教程
通用mapper簡(jiǎn)介
通用 Mapper 是一個(gè)可以實(shí)現(xiàn)任意 MyBatis 通用方法的框架,項(xiàng)目提供了常規(guī)的增刪改查操作以及Example相關(guān)的單表操作,與mybatisplus相似,對(duì)mybatis制作增強(qiáng)不做修改。為什么要用通用mapper?我們這里列舉一下原生Mybatis的痛點(diǎn):
1、mapper.xml文件里有大量的sql,當(dāng)數(shù)據(jù)庫(kù)表字段變動(dòng),配置文件就要修改
2、需要自己實(shí)現(xiàn)sql分頁(yè),select * from table where . . . limit 1,3
自己手寫(xiě)分頁(yè),除了傳參page、pageSize,還需要返回條目總數(shù)count。
3、數(shù)據(jù)庫(kù)可移植性差:如果項(xiàng)目更換數(shù)據(jù)庫(kù),比如oracle-->mysql,mapper.xml中的
sql要重新寫(xiě),因?yàn)镺racle的PLSQL 和mysql 支持的函數(shù)是不同的。
4、生成的代碼量過(guò)大。
5、批量操作,批量插入,批量更新,需要自寫(xiě)。而這些,通過(guò)通用mapper就可以很輕松的解決了。
通用mapper快速入門(文檔)
在線官方文檔:https://gitee.com/free/Mapper/wikis/Home
官方的文檔中介紹了通用mapper的三種使用方式 ,純java使用方式、與Spring集成方式、與SpringBoot集成方式。我們這里給大家介紹的是與Spring集成方式,其他方式可自行學(xué)習(xí)。
添加依賴
在開(kāi)始配置前,先添加相關(guān)的依賴。
正常情況下,Spring 和 MyBatis 的集成環(huán)境中,應(yīng)該已經(jīng)存在下面的依賴:
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>版本號(hào)</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>版本號(hào)</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>版本號(hào)</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>版本號(hào)</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>版本號(hào)</version> </dependency>
通用 Mapper 支持 MyBatis 3.2.4+
集成通用 Mapper 在上面的基礎(chǔ)上添加下面的依賴:
<dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>最新版本</version> </dependency>
tk.mybatis:mapper 依賴包含了通用 Mapper 的基礎(chǔ)代碼以及和 Spring 集成必須的代碼
和Spring集成
和 Spring 進(jìn)行集成時(shí),分為 XML 和注解配置兩種方式,每種方式又有不同的配置方式。
這里提供了很多配置方式,使用時(shí)選擇一種改動(dòng)最小的方式即可!
XML 配置
1.使用 MapperScannerConfigurer
和通用 Mapper 以前版本一樣,可以直接使用 tk.mybatis 提供的 tk.mybatis.spring.mapper.MapperScannerConfigurer
進(jìn)行配置,這個(gè)配置和 MyBatis 官方提供的 org.mybatis.spring.mapper.MapperScannerConfigurer
區(qū)別只是第一層的包名,tk
和 org
。所以使用這種方式時(shí),如果你項(xiàng)目已經(jīng)使用 org.
進(jìn)行了配置,只需要改成 tk.
即可。
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="掃描包名"/> </bean>
如果你需要對(duì)通用 Mapper 進(jìn)行特殊配置,可以按下面的方式進(jìn)行配置:
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="tk.mybatis.mapper.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="properties"> <value> 參數(shù)名=值 參數(shù)名2=值2 ... </value> </property> </bean>
可用配置的參數(shù)請(qǐng)看后續(xù)的配置文檔,配置參數(shù)時(shí)一行寫(xiě)一個(gè)值。
2.XML配置使用 Configuration
如果某些第三方也需要特殊的 MapperScannerConfigurer
時(shí),就不能用上面的方式進(jìn)行配置了,此時(shí)可以選擇下面這種方式,這種方式要求使用MyBatis (3.4.0+) 和 mybatis-spring (1.3.0+),配置方式如下:
<!--使用 Configuration 方式進(jìn)行配置--> <bean id="mybatisConfig" class="tk.mybatis.mapper.session.Configuration"> <!-- 配置通用 Mapper,有三種屬性注入方式 --> <property name="mapperProperties"> <value> notEmpty=true </value> </property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configuration" ref="mybatisConfig"/> </bean> <!-- 不需要考慮下面這個(gè),注意這里是 org 的 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="tk.mybatis.mapper.configuration"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>
這里使用了 tk.mybatis.mapper.session.Configuration
,也就是不能通過(guò)讀取 mybatis-config.xml
進(jìn)行配置,上面這種配置更直接,使用 Spring setter 配置屬性更方便。當(dāng)需要配置通用 Mapper 時(shí),使用 mapperProperties
屬性配置即可,配置方式和前面的相同,一行一個(gè)配置即可。
配置了一個(gè) mybatisConfig 的 bean 后,在 SqlSessionFactoryBean
中注入即可。
后面的 MapperScannerConfigurer
只是為了說(shuō)明這里不需要使用 tk.
開(kāi)頭的類進(jìn)行配置。
這種配置方式基本上和任何第三方都不會(huì)沖突,如果你遇到了第三方重寫(xiě) SqlSessionFactoryBean
的情況,就使用前一種方式配置即可。
實(shí)體類映射
@Table(name = "tb_brand") public class Brand implements Serializable { @Id private Integer id; private String name; private String image; private String letter; private Integer seq; //getter and setter .... }
@Table 是指定實(shí)體類對(duì)應(yīng)的數(shù)據(jù)庫(kù)表 @Id 指的是主鍵映射。經(jīng)過(guò)上面簡(jiǎn)單的配置后,相 當(dāng)于就有了 MyBatis 中的關(guān)系映射了
創(chuàng)建Mapper接口
public interface BrandMapper extends Mapper<Brand> { }
這里繼承了 tk.mybatis.mapper.common.Mapper 接口,在接口上指定了泛型類 型 Brand 。當(dāng)你繼承了 Mapper 接口后,此時(shí)就已經(jīng)有了針對(duì) Brand 的大量方法,方 法如下:
這些方法中和 MBG 生成的大部分方法都一致,還有一部分 MBG 之外的常用方法。 基礎(chǔ)接口 select
List<T> select(T record)
根據(jù) T 對(duì)象中的屬性名稱查詢 , 類似于 select * from table where t.name=#{name} and t.password = #{password}
T selectOne(T record)
根據(jù)實(shí)體中的屬性進(jìn)行查詢,只能有一個(gè)返回值,有多個(gè)結(jié)果是拋出異常,查詢條件使用等號(hào)
T selectByPrimaryKey(Object key)
根據(jù)主鍵查詢 說(shuō)明:根據(jù)主鍵字段進(jìn)行查詢,方法參數(shù)必須包含完整的主鍵屬性,查詢條 件使用等號(hào)
int selectCount(T record);
說(shuō)明:根據(jù)實(shí)體中的屬性查詢總數(shù),查詢條件使用等號(hào)
基礎(chǔ)接口 insert
int insert(T record);
說(shuō)明:保存一個(gè)實(shí)體, null 的屬性也會(huì)保存,不會(huì)使用數(shù)據(jù)庫(kù)默認(rèn)值
int insertSelective(T record);
說(shuō)明:保存一個(gè)實(shí)體, null 的屬性不會(huì)保存,會(huì)使用數(shù)據(jù)庫(kù)默認(rèn)值
基礎(chǔ)接口 Update
int updateByPrimaryKey(T record);
說(shuō)明:根據(jù)主鍵更新實(shí)體全部字段, null 值會(huì)被更新
int updateByPrimaryKeySelective(T record);
說(shuō)明:根據(jù)主鍵更新屬性不為 null 的值
基礎(chǔ)接口 delete
int delete(T record);
說(shuō)明:根據(jù)實(shí)體屬性作為條件進(jìn)行刪除,查詢條件使用等號(hào)
int deleteByPrimaryKey(Object key);
說(shuō)明:根據(jù)主鍵字段進(jìn)行刪除,方法參數(shù)必須包含完整的主鍵屬性
到此這篇關(guān)于MyBatis mapper快速入門教程的文章就介紹到這了,更多相關(guān)MyBatis mapper內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章

淺談Servlet開(kāi)發(fā)技術(shù)基礎(chǔ)

分布式開(kāi)發(fā)醫(yī)療掛號(hào)系統(tǒng)數(shù)據(jù)字典模塊前后端實(shí)現(xiàn)

mybatis中<if>標(biāo)簽bool值類型為false判斷方法

java自定義切面增強(qiáng)方式(關(guān)于自定義注解aop)

Spring Cache整合Redis實(shí)現(xiàn)方法詳解

java對(duì)接支付寶支付接口簡(jiǎn)單步驟記錄