Mybatis中的Criteria条件查询方式

作者:wunianisme 时间:2022-02-21 06:49:46 

Mybatis Criteria条件查询

Criterion

Criterion是最基本,最底层的Where条件,用于字段级的筛选。

Criteria

Criteria包含一个Cretiron的集合,每一个Criteria对象内包含的Cretiron之间是由AND连接的,是逻辑与的关系。

其它

Example类的distinct字段用于指定DISTINCT查询。

orderByClause字段用于指定ORDER BY条件,这个条件没有构造方法,直接通过传递字符串值指定。

代码示例


import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

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.apache.log4j.pattern.ClassNamePatternConverter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.ssm.mapper.ItemsMapper;
import cn.itcast.ssm.po.ItemsExample;
public class Student {
   public static void main(String[] args) throws IOException {
       /*方式一  */
       ItemsExample itemsExample1 = new ItemsExample();
       itemsExample1.or().andIdEqualTo(5).andNameIsNotNull();
       itemsExample1.or().andPicEqualTo("xxx").andPicIsNull();

List<Integer> fieldValues = new ArrayList<Integer>();
       fieldValues.add(8);
       fieldValues.add(11);
       fieldValues.add(14);
       fieldValues.add(22);
       itemsExample1.or().andIdIn(fieldValues);
       itemsExample1.or().andIdBetween(5, 9);

/*  方式二 criteria1与criteria2是or的关系 */
       ItemsExample itemsExample2 = new ItemsExample();
       ItemsExample.Criteria criteria1 = itemsExample2.createCriteria();
       criteria1.andIdIsNull();
       criteria1.andPriceEqualTo((float) 3);

ItemsExample.Criteria criteria2 = itemsExample2.createCriteria();
       criteria2.andNameIsNull();
       criteria2.andIdGreaterThanOrEqualTo(5);
       itemsExample2.or(criteria2);

//方式一和方式二是等价的
       // spring获取mapper代理对象
       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
       ItemsMapper itemsMapper = (ItemsMapper) applicationContext.getBean("itemsMapper");
       itemsMapper.countByExample(itemsExample2);

// 获取SqlSessionFactory
       String resource = "SqlMapConfig.xml";
       Reader reader = Resources.getResourceAsReader(resource);
       SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
       // 获取SqlSession
       SqlSession sqlSession = sqlMapper.openSession();
   }
}

Mybatis的Criteria用法总结

Mybatis中的Criteria条件查询方式

用一对多内敛查询的时候,有的老铁提出left join in 但是我和同事商讨结果是用代码写处各种list然后stream存到数据库中,这样一来把计算压力从数据库存入服务器,当并发量高了,这样做的好处就体现在性能方面了。

来源:https://blog.csdn.net/qq_29645505/article/details/90086644

标签:Mybatis,Criteria,条件查询
0
投稿

猜你喜欢

  • MyBatis学习教程(四)-如何快速解决字段名与实体类属性名不相同的冲突问题

    2023-11-25 05:43:49
  • C# 无边框窗体边框阴影效果的简单实现

    2021-12-06 23:41:43
  • c#实现爬虫程序

    2023-04-19 18:59:14
  • java迭代器原理及迭代map的四种方式

    2021-08-27 12:04:30
  • Spring Boot 员工管理系统超详细教程(源码分享)

    2022-04-18 13:32:26
  • SpringBoot集成Spring Security用JWT令牌实现登录和鉴权的方法

    2023-07-02 22:48:29
  • 一文搞懂Java创建线程的五种方法

    2023-10-30 18:35:04
  • C#使用Object类实现栈的方法详解

    2021-08-03 17:36:16
  • Springboot项目打war包docker包找不到resource下静态资源的解决方案

    2022-01-01 07:03:55
  • Java中ResultSetMetaData 元数据的具体使用

    2021-06-25 12:38:13
  • Android编程中context及全局变量实例详解

    2023-11-17 05:58:40
  • 完美解决idea创建文件时,文件不分级展示的情况

    2022-01-01 22:10:19
  • 用代码更新你的jar包

    2023-09-19 23:01:04
  • C#中抽象类与接口的区别详解

    2023-08-12 22:33:01
  • spring boot 使用profile来分区配置的操作

    2022-11-27 22:55:15
  • Java设计模式之抽象工厂模式浅析讲解

    2022-08-08 18:26:47
  • Java及数据库对日期进行格式化方式

    2023-08-23 12:27:52
  • java语言图形用户登录界面代码

    2021-09-11 23:19:34
  • 解决springboot 部署到 weblogic 中 jar 包冲突的问题

    2021-06-06 11:32:36
  • 全面解析Hibernate关联操作、查询操作、高级特性、并发处理机制

    2021-06-25 08:48:48
  • asp之家 软件编程 m.aspxhome.com