MyBatis獲取參數(shù)值的五種情況分析(推薦)
MyBatis獲取參數(shù)值的兩種方式:${}和#{}
${}本質(zhì):字符串拼接
#{}本質(zhì):占位符賦值
MyBatis獲取參數(shù)值的各種情況:
1. mapper接口方法的參數(shù)為單個的字面量類型:
可以通過${}和#{}以任意的名稱獲取參數(shù)值,但是需要注意${}的單引號問題
ParameterMapper接口:
User getUserByUsername(String username);
ParameterMapper.xml:
<select id="getUserByUsername" resultType="User"> <!-- select * from t_user where username = #{username};--> select * from t_user where username = '${username}'; </select>
ParameterMapperTest:
@Test public void testGetUserByUsername() { SqlSession sqlSession = SqlSessionUtils.getSqlSession(); ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class); User user = mapper.getUserByUsername("admin"); System.out.println(user); }
2. mapper接口方法的參數(shù)為多個時:
此時MyBatis會將這些參數(shù)放在一個map集合中,以兩種方式進行存儲
a>以arg0,arg1...為鍵,以參數(shù)為值
b>以param1,param2...為鍵,以參數(shù)為值
一次只需要通過#{}和${}以鍵的方式訪問即可,但是需要注意${}的單引號問題
ParameterMapper接口:
User checkLogin(String username, String password);
ParameterMapper.xml:
<select id="checkLogin" resultType="User"> <!-- select * from t_user where username = '${arg0}' and password = '${arg1}';--> select * from t_user where username = #{arg0} and password = #{arg1}; </select>
ParameterMapperTest:
@Test public void testCheckLogin() { SqlSession sqlSession = SqlSessionUtils.getSqlSession(); ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class); User user = mapper.checkLogin("admin", "123456"); System.out.println(user); }
3. 若mapper接口方法的參數(shù)有多個時,可以手動將這些參數(shù)放在一個map中存儲:
只需要通過#{}和${}以鍵的方式訪問值即可,但是需要注意${}的單引號問題
ParameterMapper接口:
User checkLoginByMap(Map<String, Object> map);
ParameterMapper.xml:
<select id="checkLoginByMap" resultType="User"> select * from t_user where username = #{username} and password = #{password}; </select>
ParameterMapperTest:
@Test public void testCheckLoginByMap() { SqlSession sqlSession = SqlSessionUtils.getSqlSession(); ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class); HashMap<String, Object> map = new HashMap<>(); map.put("username", "admin"); map.put("password", "123456"); User user = mapper.checkLoginByMap(map); System.out.println(user); }
4. mapper接口方法的參數(shù)是實體類類型的參數(shù):
只需要通過#{}和${}以屬性值的方式訪問值即可,但是需要注意${}的單引號問題
ParameterMapper接口:
int insertUser(User user);
ParameterMapper.xml:
<insert id="insertUser"> insert into t_user values (null, #{username}, #{password}, #{age}, #{sex}, #{email}); </insert>
ParameterMapperTest:
@Test public void testInsertUser() { SqlSession sqlSession = SqlSessionUtils.getSqlSession(); ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class); int num = mapper.insertUser(new User(null, "李四", "789789", 25, "男", "456123@qq.com")); System.out.println(num); }
5. 使用@Param注解命名參數(shù):
此時MyBatis會將這些參數(shù)放在一個map集合中,以兩種方式進行存儲
a>以@Param注解的值為鍵,以參數(shù)為值
b>以param1,param2...為鍵,以參數(shù)為值
因此只需要通過#{}和${}以鍵的方式訪問值即可,但是需要注意${}的單引號問題
ParameterMapper接口:
User checkLoginByParam(@Param("username") String username, @Param("password") String password);
ParameterMapper.xml:
<!--User checkLoginByParam(@Param("username") String username, @Param("password") String password);--> <select id="checkLoginByParam"> select * from t_user where username = #{username} and password = #{password}; </select>
ParameterMapperTest:
@Test public void testCheckLoginByParam() { SqlSession sqlSession = SqlSessionUtils.getSqlSession(); ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class); User user = mapper.checkLoginByParam("admin", "123456"); System.out.println(user); }
總結(jié):
上面的五種情況想要記清楚還是比較難的,所以我們可以分為兩種情況:
一種是在參數(shù)中加上@Param
一種是使用屬性值
到此這篇關(guān)于MyBatis獲取參數(shù)值的五種情況的文章就介紹到這了,更多相關(guān)MyBatis獲取參數(shù)值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring如何集成ibatis項目并實現(xiàn)dao層基類封裝
這篇文章主要介紹了Spring如何集成ibatis項目并實現(xiàn)dao層基類封裝,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09Spring Boot整合FTPClient線程池的實現(xiàn)示例
這篇文章主要介紹了Spring Boot整合FTPClient線程池的實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12Java?C++題解leetcode902最大為N的數(shù)字組合數(shù)位DP
這篇文章主要為大家介紹了Java?C++題解leetcode902最大為N的數(shù)字組合數(shù)位DP,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10