mybatis xml参数传递详解

StudentMapper.java

StudentMapper.xml

1. 传入多个String类型参数, 使用@Param注解

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

复制代码

<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>

复制代码

 

2. 传入多个String类型参数, 使用#{0},#{1},#{2}……

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

复制代码

<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>

复制代码

 

3. 传入多个String类型参数, 使用Map

grade,class,name是Map的key

List<Student> getStudent(Map param);

复制代码

<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>

复制代码

 

4. 传入多个String类型参数, 使用对象Student

grade,class,name是Student对象的属性

List<Student> getStudent(Student stu);

复制代码

<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>

复制代码

 

5. 传入单个基本类型和String类型等参数, 使用_parameter或#{0}

List<Student> getStudentByName(Student stu);

复制代码

<select id="getStudentByName" 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="_parameter!=null and _parameter!=''">
            and nn_name=#{_parameter}
        </if>
        </where>
    </select>

复制代码

复制代码

<select id="getStudentByName" 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_name=#{0}
        </if>
        </where>
    </select>

复制代码

 

6. 传入单个基本类型和String类型等参数, 使用_parameter或#{0}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Mybatis是一种Java持久化框架,它可以将Java对象映射到数据库中的表中。Mybatis的配置文件主要包括两个部分:mybatis-config.xml和mapper.xml。其中mapper.xmlMybatis的核心配置文件,它定义了SQL语句、映射关系、参数映射等信息,下面详细介绍mapper.xml文件的各个部分。 1、mapper.xml的命名空间 每个mapper.xml文件都应该设置一个命名空间,命名空间用来定义映射的SQL语句和参数映射。命名空间的格式为:mapper namespace="com.xxx.mapper.XxxMapper",其中com.xxx.mapper是mapper接口所在的包名,XxxMapper是mapper接口的类名。 2、映射SQL语句 在mapper.xml中,可以定义各种SQL语句,例如查询、插入、更新和删除等。SQL语句的格式为<select|insert|update|delete>,其中<select>表示查询语句,<insert>表示插入语句,<update>表示更新语句,<delete>表示删除语句。具体的SQL语句可以根据需要自行定义,例如: <select id="selectUserById" parameterType="int" resultType="com.xxx.model.User"> select * from user where id=#{id} </select> 在上面的例子中,id表示SQL语句的唯一标识符,parameterType表示传入参数的类型,resultType表示返回结果的类型。 3、参数映射 在mapper.xml中,可以定义参数映射,将Java对象映射到SQL语句中的参数,例如: <select id="selectUserByName" parameterType="java.lang.String" resultType="com.xxx.model.User"> select * from user where name=#{name} </select> 在上面的例子中,parameterType表示传入参数的类型,#{name}表示将Java对象中的name属性映射到SQL语句中的参数。 4、结果集映射 在mapper.xml中,可以定义结果集映射,将SQL语句返回的结果封装成Java对象,例如: <select id="selectAllUser" resultType="com.xxx.model.User"> select * from user </select> 在上面的例子中,resultType表示返回结果的类型,Mybatis会将SQL语句返回的结果封装成com.xxx.model.User对象。 5、动态SQL语句 在mapper.xml中,可以使用动态SQL语句来构建复杂的SQL语句。动态SQL语句可以根据不同的条件来生成不同的SQL语句,例如: <select id="selectUser" parameterType="com.xxx.model.User" resultType="com.xxx.model.User"> select * from user where 1=1 <if test="id != null"> and id=#{id} </if> <if test="name != null and name != ''"> and name=#{name} </if> </select> 在上面的例子中,<if>标签用来判断条件,根据条件生成不同的SQL语句。 6、其它元素 在mapper.xml中,还可以使用其它元素来完成更多的功能,例如: (1)<resultMap>元素:定义结果集映射关系; (2)<include>元素:引入其它的SQL语句; (3)<where>、<set>等元素:用于构建复杂的SQL语句; (4)<foreach>元素:用于遍历集合或数组等。 总之,mapper.xml文件是Mybatis框架中非常重要的配置文件,它定义了SQL语句、映射关系、参数映射等信息,是Mybatis实现持久化操作的核心。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

万事俱备,就差一个程序员了

谢谢您,赏俺根辣条,尝尝鲜.谢

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

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

打赏作者

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

抵扣说明:

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

余额充值