五、MyBatis获取参数值的两种方式
MyBatis获取参数值的两种方式:${}和#{}
(1) ${}的本质就是字符串拼接,#{}的本质就是占位符赋值。
(2) ${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号。
<select id="selectPersonByName" resultType="Person">
select * from t_person where name = '${name}'
</select>
(3) #{}使用占位符赋值的方式拼接sql,此时为字符串类型或日期类型的字段进行赋值时,可以自动添加单引号。
<select id="selectPersonByName" resultType="Person">
<!--select * from t_person where name = '${name}'-->
select * from t_person where name = #{name}
</select>
传参情况
演示环境
以下演示均已#{}方式获取参数。
数据库表

pojo

映射接口

1.单个字面量类型的参数
以查询为例,通过name查出一条数据。
接口方法
public interface PersonMapper {
//按name查询
Person selectPersonByName(String name);
}
映射文件
<select id="selectPersonByName" resultType="Person">
select * from t_person where name = #{name}
</select>
测试方法
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
mapper.selectPersonByName("zhangsan");
说明
当参数是一个的时候#{}中可以填写任意值,比如下方的写法也是可以获取的,但最好写的见名知意。
<select id="selectPersonByName" resultType="Person">
select * from t_person where name = #{aaa}
</select>
2.多个字面量类型的参数
以按name和age两个参数进行查询,当然多个参数也是类似的写法,只是我演示所用的表只有两个数据项。
接口方法
public interface PersonMapper {
//按name和age进行查询
Person selectPersonByNameAge(String name,Integer age);
}
映射文件
(1)方式一
<select id="selectPersonByNameAge" resultType="Person">
select * from t_person where name = #{arg0} and age=#{arg1}
</select>
(2)方式二
<select id="selectPersonByNameAge" resultType="Person">
select * from t_person where name = #{param1} and age=#{param2}
</select>
(3)方式三
<select id="selectPersonByNameAge" resultType="Person">
select * from t_person where name = #{arg0} and age=#{param2}
</select>
测试方法
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
mapper.selectPersonByNameAge("zhangsan",20);
说明
若mapper接口中的方法参数为多个时,此时MyBatis会自动将这些参数放在一个map集合中,以arg0,arg1…为键,以参数为值;以param1,param2…为键,以参数为值;
因此只需要通过#{}访问map集合的键就可以获取相对应的值。
注意${}需要手动加单引号,param是从1开始的。
3.map集合类型的参数
这次以插入一条数据为例,插入name=“李四” age=23。
接口方法
public interface PersonMapper {
//Map
int insertPersonMap(Map<String,Object> map);
}
映射文件
<insert id="insertPersonMap">
insert into t_person values(#{key1},#{key2})
</insert>
测试方法
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("key1","李四");
map.put("key2",23);
mapper.insertPersonMap(map);
说明
若mapper接口中的方法需要的参数为多个时,此时可以手动创建map集合,将这些数据放在map中只需要通过和#{}访问map集合的键就可以获取相对应的值,注意${}需要手动加单引号
4.实体类类型的参数
以插入一个Person对象为例,new Person(“wangwu”,66)
接口方法
public interface PersonMapper {
//传参对象
int insertPerson(Person person);
}
映射文件
<insert id="insertPerson">
insert into t_person values(#{name},#{age})
</insert>
测试方法
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
mapper.insertPerson(new Person("wangwu",66));
说明
若mapper接口中的方法参数为实体类对象时,此时可以使用#{},通过访问实体类对象中的属性名获取属性值,注意${}需要手动加单引号。
5.使用@Param标识参数
删除年龄为66岁的。
接口方法
public interface PersonMapper {
//@param
int deletePersonByAge(@Param("Abb") Integer age);
}
映射文件
(1)方式一
<delete id="deletePersonByAge">
<!--最好见名知意,这里为了演示-->
delete from t_person where age=#{Abb}
</delete>
(2)方式二
<delete id="deletePersonByAge">
delete from t_person where age=#{param1}
</delete>
测试方法
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
mapper.deletePersonByAge(66);
说明
可以通过@Param注解标识mapper接口中的方法参数。
(1)此时,会将这些参数放在map集合中,以@Param注解的value属性值为键,以参数为值;
(2)以param1,param2…为键,以参数为值;只需要通过和#{}访问map集合的键就可以获取相对应的值,注意${}需要手动加单引号。
本文详细介绍了MyBatis框架中参数传递的多种方式,包括使用${}
7197

被折叠的 条评论
为什么被折叠?



