五、MyBatis获取参数值的两种方式以及传参情况

本文详细介绍了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集合的键就可以获取相对应的值,注意${}需要手动加单引号。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

齊 天 大 聖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值