Mybatis獲取參數(shù)值和查詢(xún)功能的案例詳解
一、MyBatis的增刪改查
1.1、新增
<!--int insertUser();-->
<insert id="insertUser">
insert into t_user values(null,'admin','123456',23,'男')
</insert>1.2、刪除
<!--int deleteUser();-->
<delete id="deleteUser">
delete from t_user where id = 1
</delete>1.3、修改
<!--int updateUser();-->
<update id="updateUser">
update t_user set username='ybc',password='123' where id = 6
</update>1.4、查詢(xún)一個(gè)實(shí)體類(lèi)對(duì)象
<!--User getUserById();-->
<select id="getUserById" resultType="com.ssm.mybatis.bean.User">
select * from t_user where id = 2
</select>1.5、查詢(xún)list集合
<!--List<User> getUserList();-->
<select id="getUserList" resultType="com.ssm.mybatis.bean.User">
select * from t_user
</select>注意: 查詢(xún)的標(biāo)簽select必須設(shè)置屬性resultType或resultMap,用于設(shè)置實(shí)體類(lèi)和數(shù)據(jù)庫(kù)表的映射關(guān)系
resultType:自動(dòng)映射,用于屬性名和表中字段名一致的情況
resultMap:自定義映射,用于一對(duì)多或多對(duì)一或字段名和屬性名不一致的情況
二、MyBatis獲取參數(shù)值的兩種方式
MyBatis獲取參數(shù)值的兩種方式:${} 和 #{}
${} 的本質(zhì)就是字符串拼接,#{} 的本質(zhì)就是占位符賦值
${} 使用字符串拼接的方式拼接sql,若為字符串類(lèi)型或日期類(lèi)型的字段進(jìn)行賦值時(shí),需要手動(dòng)加單 引號(hào);
#{} 使用占位符賦值的方式拼接sql,此時(shí)為字符串類(lèi)型或日期類(lèi)型的字段進(jìn)行賦值時(shí), 可以自動(dòng)添加單引號(hào)
2.1、單個(gè)字面量類(lèi)型的參數(shù)
若 mapper 接口中的方法參數(shù)為單個(gè)的字面量類(lèi)型
此時(shí)可以使用 ${} 和 #{} 以任意的名稱(chēng)獲取參數(shù)的值,注意 ${} 需要手動(dòng)加單引號(hào)
<!--User getUserByUsername(String username);-->
<select id="getUserByUsername" resultType="User">
<!--select * from t_user where username = '${username}'-->
select * from t_user where username = #{username}
</select>2.2、多個(gè)字面量類(lèi)型的參數(shù)
若mapper接口中的方法參數(shù)為多個(gè)時(shí)
此時(shí)MyBatis會(huì)自動(dòng)將這些參數(shù)放在一個(gè)map集合中,以arg0,arg1...為鍵,以參數(shù)為值、以 param1,param2...為鍵,以參數(shù)為值;
因此只需要通過(guò) ${} 和 #{} 訪(fǎng)問(wèn)map集合的鍵就可以獲取相 對(duì)應(yīng)的值,注意 ${} 需要手動(dòng)加單引號(hào)
<!--User checkLogin(String username, String password);-->
<select id="checkLogin" resultType="User">
<!--select * from t_user where username = '${arg0}' and password = '${arg1}'-->
<!--select * from t_user where username = #{param1} and password = #{param2}-->
select * from t_user where username = #{arg0} and password = #{arg1}
</select>2.3、map集合類(lèi)型的參數(shù)
若mapper接口中的方法需要的參數(shù)為多個(gè)時(shí),此時(shí)可以手動(dòng)創(chuàng)建map集合,將這些數(shù)據(jù)放在 map中
只需要通過(guò) ${} 和 #{} 訪(fǎng)問(wèn)map集合的鍵就可以獲取相對(duì)應(yīng)的值,注意 ${} 需要手動(dòng)加單引號(hào)
<!--User checkLoginByMap(Map<String ,Object> map);-->
<select id="checkLoginByMap" resultType="User">
<!--select * from t_user where username = '${username}' and password = '${password}'-->
select * from t_user where username = #{username} and password = #{password}
</select>2.4、實(shí)體類(lèi)類(lèi)型的參數(shù)
若mapper接口中的方法參數(shù)為實(shí)體類(lèi)對(duì)象時(shí)
此時(shí)可以使用 ${} 和 #{} ,通過(guò)訪(fǎng)問(wèn)實(shí)體類(lèi)對(duì)象中的屬性名獲取屬性值,注意 ${} 需要手動(dòng)加單引號(hào)
<!--void insertUser(User user);-->
<insert id="insertUser" >
insert into t_user values(null,#{username},#{password},#{age},#{gender},#{email})
</insert>2.5、使用@Param標(biāo)識(shí)參數(shù)
可以通過(guò) @Param 注解標(biāo)識(shí)mapper接口中的方法參數(shù)
此時(shí),會(huì)將這些參數(shù)放在map集合中,以@Param注解的value屬性值為鍵,以參數(shù)為值;以 param1,param2...為鍵,以參數(shù)為值;
只需要通過(guò) ${} 和 #{} 訪(fǎng)問(wèn)map集合的鍵就可以獲取相對(duì)應(yīng) 的值, 注意 ${} 需要手動(dòng)加單引號(hào)
<!--User checkLoginByParam(@Param("username") String username, @Param("password") String password);-->
<select id="checkLoginByParam" resultType="User">
<!--select * from t_user where username = '${username}' and password = '${password}'-->
select * from t_user where username = #{username} and password = #{password}
</select>三、MyBatis的各種查詢(xún)功能
3.1、查詢(xún)一個(gè)實(shí)體類(lèi)對(duì)象
package com.ssm.mybatis.mapper;
import com.ssm.mybatis.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* Author:wy
* Date:2023/3/18
*/
public interface SelectMapper {
/**
* 根據(jù)用戶(hù)id查詢(xún)用戶(hù)信息
* @param id
* @return
*/
User getUserById(@Param("id") int id);
}<!--User getUserById(@Param("id") int id);-->
<select id="getUserById" resultType="User">
select * from t_user where id = #{id}
</select> @Test
public void testGetUserById() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
}3.2、查詢(xún)一個(gè)list集合
/**
* 查詢(xún)所有用戶(hù)信息
* @return
*/
List<User> getUserList(); <!--List<User> getUserList();-->
<select id="getUserList" resultType="User">
select * from t_user
</select> @Test
public void testGetUserList() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
}當(dāng)查詢(xún)的數(shù)據(jù)為多條時(shí),不能使用實(shí)體類(lèi)作為返回值,否則會(huì)拋出異常TooManyResultsException;
但是若查詢(xún)的數(shù)據(jù)只有一條,可以使用實(shí)體類(lèi)或集合作為返回值
3.3、查詢(xún)單個(gè)數(shù)據(jù)
/**
* 查詢(xún)用戶(hù)的總記錄數(shù)
* @return
*/
Integer getCount(); <!--Integer getCount();-->
<!--
在MyBatis中,對(duì)于Java中常用的類(lèi)型都設(shè)置了類(lèi)型別名
例如: Integer: int, integer
例如: int: _int, _integer
例如: Map: map
List: list
-->
<select id="getCount" resultType="Integer">
select count(*) from t_user
</select>
@Test
public void testGetCount() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Integer count = mapper.getCount();
System.out.println("用戶(hù)總數(shù)=" + count);
}
3.4、查詢(xún)一條數(shù)據(jù)為map集合
/**
* 根據(jù)用戶(hù)id查詢(xún)用戶(hù)信息為map集合
* @param id
* @return
*/
Map<String, Object> getUserByIdToMap(@Param("id") Integer id);
<!--Map<String, Object> getUserByIdToMap(@Param("id") int id);-->
<select id="getUserByIdToMap" resultType="map">
select * from t_user where id = #{id}
</select>
@Test
public void testGetUserByIdToMap() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map<String, Object> map = mapper.getUserByIdToMap(1);
System.out.println(map);
//結(jié)果:{password=123456, gender=男, id=1, age=21, email=123456@qq.com, username=張三}
}
3.5、查詢(xún)多條數(shù)據(jù)為map集合
方式一
/**
* 查詢(xún)所有用戶(hù)信息為map集合
* @return
* 將表中的數(shù)據(jù)以map集合的方式查詢(xún),一條數(shù)據(jù)對(duì)應(yīng)一個(gè)map;若有多條數(shù)據(jù),就會(huì)產(chǎn)生多個(gè)map集合
此時(shí)可以將這些map放在一個(gè)list集合中獲取
*/
@MapKey("id")
List<Map<String, Object>> getAllUserToMap();
<!--Map<String, Object> getAllUserToMap();-->
<select id="getAllUserToMap" resultType="map">
select * from t_user
</select>
@Test
public void testGetAllUserToMap() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List<Map<String, Object>> allUserToMap = mapper.getAllUserToMap();
for (Map<String, Object> map : allUserToMap) {
System.out.println(map);
}
}
方式二
/**
* 查詢(xún)所有用戶(hù)信息為map集合
* @return
* 可以將每條數(shù)據(jù)轉(zhuǎn)換的map集合放在一個(gè)大的map中,
* 但是必須要通過(guò)@Mapkey注解將查詢(xún)的某個(gè)字段的值作為大的map的鍵
*/
@MapKey("id")
Map<String, Object> getAllUserToMap();
<!--Map<String, Object> getAllUserToMap();-->
<select id="getAllUserToMap" resultType="map">
select * from t_user
</select>
@Test
public void testGetAllUserToMap() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
Map<String, Object> map = mapper.getAllUserToMap();
System.out.println(map);
//{
// 1={password=123456, gender=男, id=1, age=21, email=123456@qq.com, username=張三},
// 2={password=123456, gender=女, id=2, age=19, email=123456@qq.com, username=老六}
// }
}到此這篇關(guān)于Mybatis --- 獲取參數(shù)值和查詢(xún)功能的文章就介紹到這了,更多相關(guān)Mybatis獲取參數(shù)值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java使用POI實(shí)現(xiàn)html和word相互轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了java使用POI實(shí)現(xiàn)html和word的相互轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Mybatis詳解在注解sql時(shí)報(bào)錯(cuò)的解決方法
MyBatis-Plus 是一個(gè) Mybatis 增強(qiáng)版工具,在 MyBatis 上擴(kuò)充了其他功能沒(méi)有改變其基本功能,為了簡(jiǎn)化開(kāi)發(fā)提交效率而存在,本篇文章帶你看看在注解sql時(shí)所報(bào)出的錯(cuò)誤解決2022-03-03
java集合——Java中的equals和hashCode方法詳解
本篇文章詳細(xì)介紹了Java中的equals和hashCode方法詳解,Object 類(lèi)是所有類(lèi)的父類(lèi),非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-10-10
mybatis/mybatis-plus模糊查詢(xún)語(yǔ)句特殊字符轉(zhuǎn)義攔截器的實(shí)現(xiàn)
在開(kāi)發(fā)中,我們通常會(huì)遇到這樣的情況。用戶(hù)在錄入信息是錄入了‘%’,而在查詢(xún)時(shí)無(wú)法精確匹配‘%’。究其原因,‘%’是MySQL的關(guān)鍵字,如果我們想要精確匹配‘%’,那么需要對(duì)其進(jìn)行轉(zhuǎn)義,本文就詳細(xì)的介紹一下2021-11-11
Mybatis注解sql時(shí)出現(xiàn)的一個(gè)錯(cuò)誤及解決
這篇文章主要介紹了Mybatis注解sql時(shí)出現(xiàn)的一個(gè)錯(cuò)誤及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Springboot 整合 Java DL4J 打造文本摘要生成系統(tǒng)
本文介紹了如何使用SpringBoot整合JavaDeeplearning4j構(gòu)建文本摘要生成系統(tǒng),該系統(tǒng)能夠自動(dòng)從長(zhǎng)篇文本中提取關(guān)鍵信息,生成簡(jiǎn)潔的摘要,幫助用戶(hù)快速了解文本的主要內(nèi)容,技術(shù)實(shí)現(xiàn)包括使用LSTM神經(jīng)網(wǎng)絡(luò)進(jìn)行模型構(gòu)建和訓(xùn)練,并通過(guò)SpringBoot集成RESTfulAPI接口2024-11-11
Spring?中使用?Validation?注解校驗(yàn)參數(shù)的方法
本文介紹了如何在Spring中使用Validation注解進(jìn)行參數(shù)校驗(yàn),包括引入依賴(lài)、簡(jiǎn)單示例、常見(jiàn)校驗(yàn)注解分類(lèi)與說(shuō)明、分組校驗(yàn)和自定義校驗(yàn),通過(guò)這些方法,可以方便地對(duì)Controller、Service等層面的參數(shù)進(jìn)行校驗(yàn),確保數(shù)據(jù)的合法性和一致性,感興趣的朋友跟隨小編一起看看吧2024-11-11
解決import包時(shí)報(bào) Java 程序包不存在的問(wèn)題
你是否也有過(guò)在import包時(shí)idea報(bào)錯(cuò)說(shuō)這個(gè)包不存在,可是這個(gè)包我們看得到確實(shí)存在的情況,不要慌,今天這篇文章帶你徹底告別這個(gè)問(wèn)題2021-10-10

