您好,登錄后才能下訂單哦!
這篇文章主要介紹了Mybatis如何獲取參數(shù)值和查詢功能的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Mybatis如何獲取參數(shù)值和查詢功能文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
<!--int insertUser();--> <insert id="insertUser"> insert into t_user values(null,'admin','123456',23,'男') </insert>
<!--int deleteUser();--> <delete id="deleteUser"> delete from t_user where id = 1 </delete>
<!--int updateUser();--> <update id="updateUser"> update t_user set username='ybc',password='123' where id = 6 </update>
<!--User getUserById();--> <select id="getUserById" resultType="com.ssm.mybatis.bean.User"> select * from t_user where id = 2 </select>
<!--List<User> getUserList();--> <select id="getUserList" resultType="com.ssm.mybatis.bean.User"> select * from t_user </select>
注意: 查詢的標(biāo)簽select必須設(shè)置屬性resultType或resultMap,用于設(shè)置實(shí)體類和數(shù)據(jù)庫(kù)表的映射關(guān)系
resultType:自動(dòng)映射,用于屬性名和表中字段名一致的情況
resultMap:自定義映射,用于一對(duì)多或多對(duì)一或字段名和屬性名不一致的情況
MyBatis獲取參數(shù)值的兩種方式:${} 和 #{}
${} 的本質(zhì)就是字符串拼接,#{} 的本質(zhì)就是占位符賦值
${} 使用字符串拼接的方式拼接sql,若為字符串類型或日期類型的字段進(jìn)行賦值時(shí),需要手動(dòng)加單 引號(hào);
#{} 使用占位符賦值的方式拼接sql,此時(shí)為字符串類型或日期類型的字段進(jìn)行賦值時(shí), 可以自動(dòng)添加單引號(hào)
若 mapper 接口中的方法參數(shù)為單個(gè)的字面量類型
此時(shí)可以使用 ${} 和 #{} 以任意的名稱獲取參數(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>
若mapper接口中的方法參數(shù)為多個(gè)時(shí)
此時(shí)MyBatis會(huì)自動(dòng)將這些參數(shù)放在一個(gè)map集合中,以arg0,arg1...為鍵,以參數(shù)為值、以 param1,param2...為鍵,以參數(shù)為值;
因此只需要通過(guò) ${} 和 #{} 訪問(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>
若mapper接口中的方法需要的參數(shù)為多個(gè)時(shí),此時(shí)可以手動(dòng)創(chuàng)建map集合,將這些數(shù)據(jù)放在 map中
只需要通過(guò) ${} 和 #{} 訪問(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>
若mapper接口中的方法參數(shù)為實(shí)體類對(duì)象時(shí)
此時(shí)可以使用 ${} 和 #{} ,通過(guò)訪問(wèn)實(shí)體類對(duì)象中的屬性名獲取屬性值,注意 ${} 需要手動(dòng)加單引號(hào)
<!--void insertUser(User user);--> <insert id="insertUser" > insert into t_user values(null,#{username},#{password},#{age},#{gender},#{email}) </insert>
可以通過(guò) @Param 注解標(biāo)識(shí)mapper接口中的方法參數(shù)
此時(shí),會(huì)將這些參數(shù)放在map集合中,以@Param注解的value屬性值為鍵,以參數(shù)為值;以 param1,param2...為鍵,以參數(shù)為值;
只需要通過(guò) ${} 和 #{} 訪問(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>
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ù)用戶id查詢用戶信息 * @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); }
/** * 查詢所有用戶信息 * @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)查詢的數(shù)據(jù)為多條時(shí),不能使用實(shí)體類作為返回值,否則會(huì)拋出異常TooManyResultsException;
但是若查詢的數(shù)據(jù)只有一條,可以使用實(shí)體類或集合作為返回值
/** * 查詢用戶的總記錄數(shù) * @return */ Integer getCount();
<!--Integer getCount();--> <!-- 在MyBatis中,對(duì)于Java中常用的類型都設(shè)置了類型別名 例如: 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("用戶總數(shù)=" + count); }
/** * 根據(jù)用戶id查詢用戶信息為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=張三} }
方式一
/** * 查詢所有用戶信息為map集合 * @return * 將表中的數(shù)據(jù)以map集合的方式查詢,一條數(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); } }
方式二
/** * 查詢所有用戶信息為map集合 * @return * 可以將每條數(shù)據(jù)轉(zhuǎn)換的map集合放在一個(gè)大的map中, * 但是必須要通過(guò)@Mapkey注解將查詢的某個(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ù)值和查詢功能”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Mybatis如何獲取參數(shù)值和查詢功能”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。