首页 > 其他分享 >MyBatis 六--动态条件查询

MyBatis 六--动态条件查询

时间:2022-09-05 16:36:39浏览次数:52  
标签:status companyName -- brand 查询 brandName MyBatis ndash

查询多条件——动态查询

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

  修改SQL语句即可,有两种方法:

        首先利用if 标签来进行判断,where 后面跟恒等式或者利用where标签。

 

 

 

 

 

 

 

 单条件的动态查询:

    利用choose(when,otherwise)来选择

  

//单条件动态查询
    @Test
    public  void test_SelectByConditionSingle() throws IOException {

        int id = 1;
        int status =1;
        String companyName = "华为";
        String brandName = "华为";


        //处理参数
        companyName = "%"+companyName+"%";
        brandName = "%"+brandName+"%";



        //封装对象——方法2
        Brand brand = new Brand();
       // brand.setStatus(status);
      //  brand.setBrandName(brandName);
        // brand.setCompanyName(companyName);


        //1获取sqlSessionFactory
        String resource = "mybatis-config.xml";                                 //配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);      //传入流
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);    //返回对象

        //2 获取sqlSession 对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3 获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //4 执行方法
// 方法一:       List<Brand> brands = brandMapper.selectByCondition(status,companyName,brandName);
//方法二           List<Brand> brands = brandMapper.selectByCondition(brand);
//方法三

        List<Brand> brands = brandMapper.selectByConditionSingle(brand);
        System.out.println(brands);
        //5 释放资源
        sqlSession.close();
    }

SQL语句编写两种方法

<!--单条件查询-->
 <!--   <select id="selectByConditionSingle" resultMap="barandResultMap">
    select *
from tb_brand
where
    <choose>&lt;!&ndash;相当于switch&ndash;&gt;
        <when test="status != null">  &lt;!&ndash;相当于case&ndash;&gt;
             status = #{status}
        </when>
        <when test="companyName != null and companyName !=''">  &lt;!&ndash;相当于case&ndash;&gt;
            company_name like #{companyName}
        </when>
        <when test="brandName != null and brandName != ''">  &lt;!&ndash;相当于case&ndash;&gt;
             brand_name like #{brandName}
        </when>
        <otherwise>
            1=1
        </otherwise>
    </choose>
    </select>-->





    <select id="selectByConditionSingle" resultMap="barandResultMap">
        select *
        from tb_brand
        <where>
        <choose><!--相当于switch-->
            <when test="status != null">  <!--相当于case-->
                status = #{status}
            </when>
            <when test="companyName != null and companyName !=''">  <!--相当于case-->
                company_name like #{companyName}
            </when>
            <when test="brandName != null and brandName != ''">  <!--相当于case-->
                brand_name like #{brandName}
            </when>

        </choose>
        </where>
    </select>

 

标签:status,companyName,--,brand,查询,brandName,MyBatis,ndash
From: https://www.cnblogs.com/zhaolei0419/p/16658631.html

相关文章

  • APIRouter of FASTAPI
    ROUTEROFAPPhttps://fastapi.tiangolo.com/tutorial/first-steps/如果应用比较简单,可以把所有的路径定义在app上。如果应用非常庞大,有很多的逻辑,按照业务分为产生数十......
  • npm publish更新包版本
    版本管理npm的发包需要遵循语义化版本,一个版本号包含三个部分: MAJOR.MINOR.PATCH ,MAJOR表示主版本号,当你做了不兼容的API修改;MINOR表示次版本号,当你做了向下兼容的......
  • Session认证机制与JWT认证机制
    一、什么是身份认证?身份认证(Authentication)又称“身份验证”、“鉴权”,是指通过一定的手段,完成对用户身份的确认。日常生活中的身份认证随处可见,例如:高铁的验票乘车,手......
  • 【设计模式】Java设计模式 - 建造者模式
    【设计模式】Java设计模式-建造者模式......
  • C#实现HTTP访问类HttpHelper
    在项目开发过程中,我们经常会访问第三方接口,如我们需要接入的第三方接口是WebAPI,这时候我们就需要使用HttpHelper调用远程接口了。示例中的HttpHelper类使用Log4Net记录了......
  • 通过Quartz 进行定时任务
    小记一下通过Quartz 进行轮询数据库从而进行自动打印的需求。一:首先通过NuGet引用Quartz,Quartz依赖Common.Logging和Common.Logging.Log4Net1211,所以同时需要引用这两个D......
  • 网络层协议与应用
    1 网络层的功能1、定义了基于IP协议的逻辑地址2、连接不同的媒介类型3、选择数据通过网络的最佳路径2 IP数据包IP数据报文由首部(称为报头)和数据两部分组成。首部的......
  • 博客园的打赏功能
    简单几步为自己的博客园主页添加打赏功能:1准备工作:准备支付宝和微信的二维码,后缀名需修改.bmp格式。如zfb.bmp,wx.bmp。   2上传图片:进入自己的博客园,然后进入......
  • 使用RestHighLevelClient的3种分页实现
    目录from+size分页1.1from+size的命令行实现1.2from+size的RestHighLevelClient实现scroll分页2.1scroll分页的命令行实现2.2scroll的RestHighLevelClient实......
  • Hive-day2
    Hive的基本操作 Hive库操作1.创建数据库1)创建一个数据库,数据库在**HDFS上的默认存储路径是/hive/warehouse/\*.db**。createdatabasetestdb; 2)避免要创建的数据......