Mybatis参数传值问题

1.一个参数

//一个值的查询
Student  find01(String id); //接口

   <!--
        当数据库列名和实体类属性名不一样的时候不能使用resultType返回数据
        当输入参数为一个的时候 取值的名字可以随便写
        #{}
        ${}取值可以随便写吗?可以只是不能写特殊符号

区别

#{} 先生成SQL,再传值也就是  jdbc中的   ?

${}先放值后生成sql语句!!(很危险,如果放入1=1永远成立。俗称sql注入)
    -->
<select id="find01" parameterType="string" resultMap="stuMap">
   select stu_id,stu_name from student where stu_id=#{value }
</select>

<resultMap id="stuMap" type="com.sun.pojo.Student">
    <id property="stuId" column="stu_id"></id>
    <result property="stuName" column="stu_name"></result>
</resultMap>

2.多个参数

Student  find02(int id,  String name);

<!--
多参数查询
   mybtis 把参数放到数组中

按下标取值:

arg0 表示取第一个参数,

  arg1 表示取第二参数。   

按参数位置:

param1 表示第一个参数

param2 按第二个参数
-->
<select id="find02"   resultMap="stuMap">
    SELECT stu_id,stu_name from  student where stu_id=#{arg0} and stu_name=#{arg1}
</select>

<select id="find02"   resultMap="stuMap">
    SELECT stu_id,stu_name from  student where stu_id=#{param1} and stu_name=#{param2}
</select>

3.多参数注解

Student  find03(@Param("id") int id, @Param("name") String name);

<select id="find03"   resultMap="stuMap">
    SELECT stu_id,stu_name from  student where stu_id=#{id} and stu_name=#{name}
</select>

多参数推荐使用注解

4.50个参数怎么办

//有50个参数怎么办?使用数组
List<Student>  find03(int []id);

<select id="find03"   resultMap="stuMap">
    SELECT stu_id,stu_name from  student where stu_id in
    <foreach collection="arg0" item="item" open="(" close=")" separator="," index="index">
            #{item}
    </foreach>
</select>

5.多参数多类型

List<Student>  find04(@Param("id") int []id,@Param("sex") String sex);

<select id="find04"   resultMap="stuMap">
    SELECT stu_id,stu_name from  student where stu_id in
    <foreach collection="id" item="item" open="(" close=")" separator="," index="index">
        #{item}
    </foreach>
    and stu_sex=#{sex}
</select>

————————————————————————————————————————

2个整形一个对象

List<Student>findAll(@Param("page") int page,@Param("row") int row,@Param("stu") Student stu);

<select id="findAll"  resultMap="stuMap">
    select stu_id,stu_name,stu_age,stu_sex,stu_brth,stu_idcard,cls_id
    from student
    <where>
            <if test="stu !=null ">
                  <if test="stu.stuName != null and stu.stuName !='' ">
                    and stu_name=#{stu.stuName}
                  </if>
                  <if test="stu.stuAge != null and stu.stuAge != ''">
                     and stu_age=#{stu.stuAge}
                  </if>
                  <if test="stu.stuSex !=null and stu.stuSex !=''">
                    and stu_sex=#{stu.stuSex}
                  </if>
            </if>
    </where>
    limit #{page},#{row}
</select>

6.map传值:

不推荐参数没语义,

通过看参数不知道需要什么值,必须看sql才清楚具体值。

取值用map的key取就可以了

//用map传值
List<Student>  find05(Map<String,Object> map);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

日有所思的暖树

对您有帮助给点鼓励吧!

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

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

打赏作者

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

抵扣说明:

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

余额充值