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)文章
Java實現(xiàn)讀取項目中文件(.json或.properties)的方法詳解
這篇文章主要為大家詳細介紹了Java實現(xiàn)讀取項目中文件的方法,例如.json或.properties,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-04-04
關(guān)于MyBatis結(jié)果映射的實例總結(jié)
結(jié)果集映射主要是為了解決屬性名和類型名不一致的問題,下面這篇文章主要給大家介紹了關(guān)于MyBatis結(jié)果映射的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-05-05
java判斷l(xiāng)ist不為空的實現(xiàn),和限制條數(shù)不要在一起寫
這篇文章主要介紹了java判斷l(xiāng)ist不為空的實現(xiàn),和限制條數(shù)不要在一起寫。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
El表達式使用問題javax.el.ELException:Failed to parse the expression
今天小編就為大家分享一篇關(guān)于Jsp El表達式使用問題javax.el.ELException:Failed to parse the expression的解決方式,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12

