Mybatis各種查詢接口使用詳解
一、查詢一個(gè)實(shí)體類對(duì)象
①創(chuàng)建SelectMapper接口
若sql語(yǔ)句查詢的結(jié)果為多條時(shí),一定不能以實(shí)現(xiàn)類類型作為方法的返回值
否則會(huì)拋出異常TooManyResultsException
若sql語(yǔ)句查詢的結(jié)果為1條時(shí),此時(shí)可以使用實(shí)體類類型或list集合類型作為方法的返回值
/**
* 根據(jù)id查詢用戶信息
* @param id
* @return
*/
User getUserById(@Param("id") Integer id);②創(chuàng)建SelectMapper配置文件
<!-- User getUserById(@Param("id") Integer id);-->
<select id="getUserById" resultType="User">
select * from t_user where id = #{id}
</select>③創(chuàng)建測(cè)試類
@Test
public void testGetUserById(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
User user = mapper.getUserById(2);
System.out.println(user);
sqlSession.close();
}二、查詢一個(gè)list集合
①創(chuàng)建SelectMapper接口
/**
* 查詢所有的用戶信息
* @return
*/
List<User> getAllUser();②創(chuàng)建SelectMapper配置文件
<!-- List<User> getAllUser();-->
<select id="getAllUser" resultType="User">
select * from t_user
</select>③創(chuàng)建測(cè)試類
@Test
public void testGetAllUser(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List<User> allUser = mapper.getAllUser();
allUser.forEach(System.out::println);
sqlSession.close();
}三、查詢單個(gè)數(shù)據(jù)
①創(chuàng)建SelectMapper接口
/**
* 查詢用戶的總記錄數(shù)
* @return
*/
Integer getCount();②創(chuàng)建SelectMapper配置文件
在MyBatis中,對(duì)于Java中常用的類型都設(shè)置了類型別名
例如: java.lang.Integer -> int/integer
例如: int -> _int/_integer
例如: Map -> map,
例如: List -> list
<!-- Integer getCount();-->
<!-- <select id="getCount" resultType="java.lang.Integer">-->
<select id="getCount" resultType="Integer">
select count(*) from t_user
</select>③創(chuàng)建測(cè)試類
@Test
public void testGetCount(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Integer count = mapper.getCount();
System.out.println(count);
sqlSession.close();
}四、查詢一個(gè)數(shù)據(jù)為map集合
①創(chuàng)建SelectMapper接口
/**
* 根據(jù)用戶id查詢用戶信息為map集合
* @param id
* @return
*/
Map<String, Object> getUserToMap(@Param("id") int id);②創(chuàng)建SelectMapper配置文件
<!--Map<String, Object> getUserToMap(@Param("id") int id);-->
<!--結(jié)果: {password=123456, sex=男 , id=1, age=23, username=admin}-->
<select id="getUserToMap" resultType="map">
select * from t_user where id = #{id}
</select>③創(chuàng)建測(cè)試類
@Test
public void testGetUserToMap(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map<String, Object> userToMap = mapper.getUserToMap(2);
System.out.println(userToMap);
sqlSession.close();
}五、查詢多條數(shù)據(jù)為map集合
①創(chuàng)建SelectMapper接口
方式一:
/**
* 查詢所有用戶信息為map集合
* @return
* 將表中的數(shù)據(jù)以map集合的方式查詢,一條數(shù)據(jù)對(duì)應(yīng)一個(gè)map;若有多條數(shù)據(jù),就會(huì)產(chǎn)生多個(gè)map集合,此
時(shí)可以將這些map放在一個(gè)list集合中獲取
*/
// List<Map<String, Object>> getAllUserToMap();方式二:
/**
* 查詢所有用戶信息為map集合
* @return
* 將表中的數(shù)據(jù)以map集合的方式查詢,一條數(shù)據(jù)對(duì)應(yīng)一個(gè)map;若有多條數(shù)據(jù),就會(huì)產(chǎn)生多個(gè)map集合,并
且最終要以一個(gè)map的方式返回?cái)?shù)據(jù),此時(shí)需要通過(guò)@MapKey注解設(shè)置map集合的鍵,值是每條數(shù)據(jù)所對(duì)應(yīng)的
map集合
*/
@MapKey("id")
Map<String, Object> getAllUserToMap();②創(chuàng)建SelectMapper配置文件
<!--Map<String, Object> getAllUserToMap();-->
<select id="getAllUserToMap" resultType="map">
select * from t_user
</select>③創(chuàng)建測(cè)試類
@Test
public void testGetAllUserToMap(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
// List<Map<String, Object>> allUserToMap = mapper.getAllUserToMap();
// allUserToMap.forEach(System.out::println);
Map<String, Object> allUserToMap = mapper.getAllUserToMap();
System.out.println(allUserToMap);
sqlSession.close();
}到此這篇關(guān)于Mybatis各種查詢接口使用詳解的文章就介紹到這了,更多相關(guān)Mybatis查詢接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot+fileUpload獲取文件上傳進(jìn)度
這篇文章主要為大家詳細(xì)介紹了SpringBoot+fileUpload獲取文件上傳進(jìn)度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Java關(guān)鍵字instanceof用法及實(shí)現(xiàn)策略
instanceof 運(yùn)算符是用來(lái)在運(yùn)行時(shí)判斷對(duì)象是否是指定類及其父類的一個(gè)實(shí)例。這篇文章主要介紹了Java關(guān)鍵字instanceof用法解析,需要的朋友可以參考下2020-08-08
spring單元測(cè)試下模擬rabbitmq的實(shí)現(xiàn)
這篇文章主要介紹了spring單元測(cè)試下模擬rabbitmq的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
springboot啟動(dòng)不加載bootstrap.yml文件的問(wèn)題
這篇文章主要介紹了springboot啟動(dòng)不加載bootstrap.yml文件的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java Web Listener實(shí)現(xiàn)事件監(jiān)聽(tīng)與處理
Java Web開(kāi)發(fā)中的Listener是一種事件機(jī)制,通過(guò)監(jiān)聽(tīng)Web應(yīng)用程序的事件,實(shí)現(xiàn)對(duì)事件的處理,從而實(shí)現(xiàn)更加靈活和高效的應(yīng)用程序開(kāi)發(fā)。Listener能夠監(jiān)聽(tīng)的事件包括應(yīng)用程序啟動(dòng)和關(guān)閉、Session創(chuàng)建和銷毀、請(qǐng)求和響應(yīng)對(duì)象的創(chuàng)建和銷毀等2023-04-04

