mybatis xml参数传递 多种方法

StudentMapper.java

StudentMapper.xml

  方法一、传入多个String类型参数, 使用@Param注解

1、StudentMapper.java中写法:

List<Student> getStudent(@Param("grade")String grade, @Param("class")String class,@Param("name")String name);

2、StudentMapper.xml中写法:

<select id="getStudent" parameterType="java.lang.String" resultType="Student">
        select nn_grade, nn_class, nn_name, nn_age, nn_sex, nn_birthdate, nn_address from student
        <where>
        <if test="grade != null and grade != ''">
            and nn_grade=#{grade}
        </if>
        <if test="class != null and class != ''">
            and nn_class=#{class}
        </if>
        <if test="name != null and name != ''">
            and nn_name=#{name}
        </if>
        </where>
    </select>

  方法二、 传入多个String类型参数, 使用#{0},#{1},#{2}……

1、StudentMapper.java中写法:
List<Student> getStudent(String grade, String class,String name);

2、StudentMapper.xml中写法:

<select id="getStudent" parameterType="java.lang.String" resultType="Student">
        select nn_grade, nn_class, nn_name, nn_age, nn_sex, nn_birthdate, nn_address from student
        <where>
        <if test="#{0} != null and #{0} != ''">
            and nn_grade=#{0}
        </if>
        <if test="#{1} != null and #{1} != ''">
            and nn_class=#{1}
        </if>
        <if test="#{2} != null and #{2} != ''">
            and nn_name=#{2}
        </if>
        </where>
    </select>

   方法三、 传入多个String类型参数, 使用Map

1、StudentMapper.java中写法:grade,class,name是Map的key
List<Student> getStudent(Map param);

2、StudentMapper.xml中写法:

<select id="getStudent" parameterType="java.util.Map" resultType="Student">
        select nn_grade, nn_class, nn_name, nn_age, nn_sex, nn_birthdate, nn_address from student
        <where>
        <if test="grade != null and grade != ''">
            and nn_grade=#{grade}
        </if>
        <if test="class!=null and class!=''">
            and nn_class=#{class}
        </if>
        <if test="name!=null and name!=''">
            and nn_name=#{name}
        </if>
        </where>
    </select>

  方法四、grade,class,name是Student对象的属性

1、StudentMapper.java中写法:grade,class,name是Student对象的属性;
List<Student> getStudent(Student stu);

2、StudentMapper.xml中写法:

<select id="getStudent" parameterType="Student" resultType="Student">
        select nn_grade, nn_class, nn_name, nn_age, nn_sex, nn_birthdate, nn_address from student
        <where>
        <if test="grade != null and grade != ''">
            and nn_grade=#{grade}
        </if>
        <if test="class!=null and class!=''">
            and nn_class=#{class}
        </if>
        <if test="name!=null and name!=''">
            and nn_name=#{name}
        </if>
        </where>
    </select>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis 中,可以在 mapper.xml 文件中使用 `<select>`, `<insert>`, `<update>`, `<delete>` 标签来定义 SQL 语句。这些标签中的 SQL 语句可以使用参数,参数的传递方式有多种,以下是其中几种常用的方式: 1. 使用 `#{}` 占位符来传递参数。例如: ```xml <select id="getUserById" resultType="User"> SELECT * FROM user WHERE id = #{userId} </select> ``` 在这个例子中,`#{userId}` 表示一个占位符,MyBatis 会在执行 SQL 语句时将其替换为实际的参数值。 2. 使用 `${}` 占位符来传递参数。例如: ```xml <select id="getUserByName" resultType="User"> SELECT * FROM user WHERE name = '${userName}' </select> ``` 在这个例子中,`${userName}` 表示一个占位符,MyBatis 会在执行 SQL 语句时将其替换为实际的参数值。需要注意的是,使用 `${}` 传递参数存在 SQL 注入的风险,因为实际的参数值直接拼接到 SQL 语句中,建议尽量避免使用。 3. 使用 `@Param` 注解来传递参数。例如: ```java public interface UserMapper { List<User> getUserByNameAndAge(@Param("name") String name, @Param("age") int age); } ``` ```xml <select id="getUserByNameAndAge" resultType="User"> SELECT * FROM user WHERE name = #{name} AND age = #{age} </select> ``` 在这个例子中,`@Param` 注解用于指定参数的名称,MyBatis 会根据参数名称来匹配 SQL 语句中的占位符。 这些方式都可以用于传递参数,具体选择哪种方式取决于具体的情况。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值