【Java闭关修炼】MyBatis-动态SQL

【Java闭关修炼】MyBatis-动态SQL

介绍

在这里插入图片描述

有条件的查询 需要在映射配置文件中提前写好sql语句,需要配置好参数 同时mapper接口也需要补充一下该方法

<!--    参数从student对象中进行获取-->
    <select id="selectCondition1" resultType="student" parameterType="student">
        SELECT * FROM student WHERE id = #{id} AND name = #{name} AND age = #{age}
    </select>

    // 多条件查询
    public  abstract List<Student> selectCondition1(Student stu);

编写测试类

package com.itheima.dynamic;

import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class Test02 {
    @Test
    public void selectCondition1() throws Exception{
        // 测试一下 多条件查询语句
        // 获取核心配置文件连接数据库
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);

        // 获取SqlSession对象
        SqlSession sqlSession = build.openSession(true);// 自动提交事务

        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);// 获取指定接口的mapper对象

        // 调用多条件查询的方法
        Student stu = new Student();
        stu.setId(2);
        stu.setName("314");
        stu.setAge(2);

        List<Student> students = mapper.selectCondition1(stu);//调用多条件查询的方法

        // 增强for循环遍历
        for (Student student : students) {
            System.out.println(student);
        }

        sqlSession.close();


        is.close();


    }
}


if标签的使用

在这里插入图片描述

使用if标签改写映射配置文件中的sql语句

 <select id="selectCondition1" resultType="student" parameterType="student">
        SELECT * FROM student
        <where>
            <if test = "id != null">
                id = #{id}
            </if>

            <if test = "name != null">
                AND name = #{name}
            </if>

            <if test = "age != null">
                AND age = #{age}
            </if>
        </where>
</select>

foreach标签

  • 循环遍历标签,适用于多个参数或者的关系

在这里插入图片描述

<!--    根据多个id进行查询  参数类型 list 代表多个参数  返回值类型student-->
    <select id="selectByIds1" resultType="student" parameterType="list">
        SELECT * FROM student
        <where>
            <foreach collection="list" open = "id IN(" close=")" item = "id" separator=",">
                #{id}
            </foreach>
        </where>
    </select>

sql片段的抽取

将重复性的sql语句进行抽取 达到复用的效果

在这里插入图片描述

    <sql id="select" >SELECT * FROM student</sql>

    <!--
        select:查询功能的标签
        id属性:唯一标识
        resultType属性:指定结果映射对象类型  返回值类型
        parameterType属性:指定参数映射对象类型
    -->

<!--    id必须和方法名保持一致-->
    <select id="selectAll" resultType="student">
        <include refid="select"/>
    </select>

总结

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

少写代码少看论文多多睡觉

求打赏,求关注,求点赞

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

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

打赏作者

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

抵扣说明:

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

余额充值