Day 79

_Mybati条件查询

  1. 需求:在这里插入图片描述

  2. 在这里插入图片描述
    为了避免后面的@Pram注解出现不必要的bug,初学时还是将实体类对象Brand.java中的变量名都与数据库表中的列名保持一致

  3. 在这里插入图片描述

  4. 在这里插入图片描述

  5. 在这里插入图片描述

  6.     // 条件查询
        public void conditionSelect() {
            int status = 0;
            String brand_name = "字节";
            String address = "北京";
    
            // 模糊查询参数操作
            brand_name = "%" + brand_name + "%";
            address = "%" + address + "%";
            InputStream is = null;
            try {
                is = Resources.getResourceAsStream("mybatis-config.xml");
                SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
                SqlSessionFactory sf = sqlSessionFactoryBuilder.build(is);
                SqlSession sqlSession = sf.openSession(true);
                // 获取mapper接口
                BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
                // 执行sql语句
                List<Brand> result = mapper.selectByCondition(status, brand_name, address);
    
                // 返回执行结果
                System.out.println("执行结果为:"+result);
    
                // 释放资源
                sqlSession.close();
    
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    ======================================================================
    执行结果为:[Brand{id=4, brand_name='字节跳动', ordered=20, address='北京', states=0}]
    
    Process finished with exit code 0
    

_查询,多条件,动态查询

  1. SQL语句会随着用户的输入或者外部条件的变化而变化,我们称之为动态SQL

  2. <select id =" ***  "  resultMap = " *** ">
    select *
        from tb_brand
        <where>
            <if test = "abc != null and abc != '' ">
                abc like #{abc}
            </if>
        </where>
    </select>
    
  3. 在这里插入图片描述

  4. 如果不是动态的条件查询,加入用户所输入的信息有缺失,则程序将无法查询,比如:在这里插入图片描述
    查询到的结果为:在这里插入图片描述

  5. 但是如果用动态的条件查询的话:在这里插入图片描述

  6. 注意:此时的查询是将需要查询的参数封装到一个list集合中的,具体的test问价的java代码如下所示:

    • package com.atguigudb.mybatis.test;
      
      import com.atguigudb.mybatis.mapper.BrandMapper;
      import com.atguigudb.mybatis.pojo.Brand;
      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.IOException;
      import java.io.InputStream;
      import java.util.List;
      
      public class MyBatisBrand_Test {
      
          @Test
      //
      //    public void testMyBatis() {
      //        InputStream is = null;
      //        try {
      //            is = Resources.getResourceAsStream("mybatis-config.xml");
      //            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
      //            SqlSessionFactory sf = sqlSessionFactoryBuilder.build(is);
      //            SqlSession sqlSession = sf.openSession(true);
      //
      //            // 获取Mapper接口
      //            BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
      //
      //            // 执行sql语句
      //            List<Brand> brands = mapper.selectAll();
      //
      //            // 返回结果在控制台输出
      //            System.out.println("result:" + brands);
      //
      //            // 释放资源
      //            sqlSession.close();
      //
      //
      //        } catch (IOException e) {
      //            e.printStackTrace();
      //        }finally {
      //            try {
      //                is.close();
      //            } catch (IOException e) {
      //                e.printStackTrace();
      //            }
      //        }
      //
      //
      //    }
      
      
      /*
          @Test
          public void selectOne() {
              int id = 1;
              InputStream is = null;
              try {
                  is = Resources.getResourceAsStream("mybatis-config.xml");
                  SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
                  SqlSessionFactory sf = sqlSessionFactoryBuilder.build(is);
                  SqlSession sqlSession = sf.openSession(true);
      
                  // 获取Mapper接口
                  BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
      
                  // 执行sql语句
                  Brand selectByid = mapper.selectByid(id);
      
                  // 返回结果在控制台输出
                  System.out.println("您所查询的为:" + selectByid);
      
                  // 释放资源
                  sqlSession.close();
      
      
              } catch (IOException e) {
                  e.printStackTrace();
              }finally {
                  try {
                      is.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
      
      
          }
          */
      
          // 条件查询
          public void conditionSelect() {
              int status = 0;
              String brand_name = "字节";
              String address = "北京";
      
              // 模糊查询参数操作
              brand_name = "%" + brand_name + "%";
              address = "%" + address + "%";
      
              // 封装参数
              Brand brand = new Brand();
              brand.setStates(status);
      //        brand.setName(brand_name);
              brand.setAddress(address);
              InputStream is = null;
              try {
                  is = Resources.getResourceAsStream("mybatis-config.xml");
                  SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
                  SqlSessionFactory sf = sqlSessionFactoryBuilder.build(is);
                  SqlSession sqlSession = sf.openSession(true);
                  // 获取mapper接口
                  BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
                  // 执行sql语句
                  List<Brand> result = mapper.selectByCondition(brand);
      //            List<Brand> result = mapper.selectByCondition(status, brand_name, address);
                  // 返回执行结果
                  System.out.println("执行结果为:"+result);
      
                  // 释放资源
                  sqlSession.close();
      
              } catch (IOException e) {
                  e.printStackTrace();
              }finally {
                  try {
                      is.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      
      }
      ===========================================
      执行结果为:[Brand{id=4, brand_name='字节跳动', ordered=20, address='北京', states=0}]
      
      Process finished with exit code 0
      
  7. 因为要封装封装查询参数,所以接口文件中也需要修改,具体如图所示,该方法条件查询的第二种方法:在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值