Mybatis传多个参数的五个方法

本文详细介绍了Mybatis中传入多个参数的五种方法,包括使用实体类结合@Param注解、直接使用实体类、单独使用@Param注解、顺序传参以及通过Map传参。重点讲解了@Param注解的使用,以及如何在XML中根据注解选择合适的参数进行查询操作。
摘要由CSDN通过智能技术生成

Mybatis传多个参数的五个方法

一、实体类+@Param注解传参(难点,重点,基本会这个就行了)

在我们拥有多个参数的时候,怎么让传入xml文件的参数,选中我们需要的实体类部分,就需要加入@Param注解,在xml中使用注解里面的值,如下面代码,我在@Param中加入了competitionVo就需要在xml中,点出来,如competitionVo.singerName#{competitionVo.singerName}

Mapper

    Page<Competition> competitionInfoPart(Page page,@Param("competitionVo") CompetitionVo competitionVo);

Mapper.xml(不需要看我的SQL,主要理解@Param注解+实体类的使用)

    <select id="competitionInfoPart" resultMap="info" parameterType="com.zhao.pojo.CompetitionVo">
        select c.*,
               s1.singer_name singer1Name,s2.singer_name singer2Name
        FROM competition c
            INNER JOIN singer s1 on c.singer1_id=s1.singer_id
            INNER JOIN singer s2 on c.singer2_id=s2.singer_id
        where c.deleted = 0
        <if test="competitionVo.singerName!=null">
            and (s1.singer_name like CONCAT('%',#{competitionVo.singerName},'%') or s2.singer_name like CONCAT('%',#{competitionVo.singerName},'%'))
        </if>
        <if test="competitionVo.song!=null">
            and (c.singer2_song like CONCAT('%',#{competitionVo.song},'%') or c.singer1_song like CONCAT('%',#{competitionVo.song},'%') )
        </if>
        <if test="competitionVo.status!=null">
            and (c.competition_state = #{competitionVo.status} or c.competition_state = #{competitionVo.status})
        </if>
        order by c.competition_id desc
    </select>

二、实体类传参(重点,经常使用

Mapper

User selectUser(User user);

Mapper.xml

<select id="selectUser" parameterType="com.zhao.User" resultType="com.zhao.User">
    select * from user
    where user_name = #{userName} and id = #{id}
</select>

三、@Param传参(重点

基本类型(int、String…)这样的不需要写 parameterType 写也可以

Mapper

User selectUser(@Param("userName") String name, @Param("id") int id);

Mapper.xml

<select id="selectUser"  resultType="com.zhao.User">
    select * from user
    where user_name = #{userName} and id = #{id}
</select>

四、顺序传参(不建议)

Mapper

User selectUser(String name, int id);

Mapper.xml

<select id="selectUser"  resultType="com.zhao.User">
    select * from user
    where user_name = #{name} and id = #{id}
</select>

五、Map传参(不常用)

Mapper

User selectUser(Map<String, Object> user);

Mapper.xml

<select id="selectUser" parameterType="Map" resultType="com.zhao.User">
    select * from user
    where user_name = #{name} and id = #{id}
</select>

参考:Mybatis传递多个参数的4种方式(干货)

解决BindingException: Parameter ‘name‘ not found. Available parameters are [xxxx, param1, param2]

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis是一个优秀的Java持久化框架,它可以轻松地将数据库表和Java对象之间进行映射。在开发过程中,我们通常需要向MyBatisSQL语句中多个参数。下面我们来介绍MyBatis多个参数方法。 1. 使用Map参数。 通过使用Map来多个参数,可以将多个参数打包到一个Map对象中。在MyBatis的Mapper.xml文件中,使用#{key}来引用Map中的参数值。例如,如果我们要递两个参数,一个是username,一个是age,可以使用如下的方式: ``` <select id="getUser" parameterType="map" resultType="User"> SELECT * FROM user WHERE username = #{username} AND age = #{age} </select> ``` 在Java中使用Map参数的例子如下: ``` Map<String, Object> parameterMap = new HashMap<>(); parameterMap.put("username", "张三"); parameterMap.put("age", 18); User user = sqlSession.selectOne("getUser", parameterMap); ``` 2. 使用@Param注解参数。 @Param注解可以用来指定参数的名称,从而在Mapper.xml文件中和Java中使用相同的参数。在Mapper.xml文件中,使用#{参数名}来引用参数值。例如,如果我们要递两个参数,一个是username,一个是age,可以使用如下的方式: ``` <select id="getUser" parameterType="map" resultType="User"> SELECT * FROM user WHERE username = #{username} AND age = #{age} </select> ``` 在Java中使用@Param注解参数的例子如下: ``` public User getUser(@Param("username") String username, @Param("age") int age); ``` 3. 使用JavaBean参数。 在Java中,我们可以使用JavaBean来封装多个参数,然后在Mapper.xml文件中使用#{属性名}来引用JavaBean属性的值。例如,如果我们要递两个参数,一个是username,一个是age,可以使用如下的JavaBean: ``` public class UserInfo { private String username; private int age; // getter/setter } ``` 在Mapper.xml文件中,可以如下使用JavaBean参数: ``` <select id="getUser" parameterType="UserInfo" resultType="User"> SELECT * FROM user WHERE username = #{username} AND age = #{age} </select> ``` 在Java中使用JavaBean参数的例子如下: ``` UserInfo userInfo = new UserInfo(); userInfo.setUsername("张三"); userInfo.setAge(18); User user = sqlSession.selectOne("getUser", userInfo); ``` 总之,MyBatis多个参数方法有很多,主要是使用Map、@Param注解和JavaBean来封装参数。在使用的时候,我们需要根据具体情况,选择最适合的方法参数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我认不到你

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

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

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

打赏作者

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

抵扣说明:

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

余额充值