Mybatis中的Criteria条件查询方式
作者:wunianisme 发布时间:2022-02-21 06:49:46
标签:Mybatis,Criteria,条件查询
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用法总结
用一对多内敛查询的时候,有的老铁提出left join in 但是我和同事商讨结果是用代码写处各种list然后stream存到数据库中,这样一来把计算压力从数据库存入服务器,当并发量高了,这样做的好处就体现在性能方面了。
来源:https://blog.csdn.net/qq_29645505/article/details/90086644


猜你喜欢
- 1.构建springboot项目2.打包应用3.编写dockerfile4.构建镜像5.发布运行![root@localhost demo]
- 目录前言实现思路实测前言需求 导出Excel:本身以为是一个简单得导出,但是每行得记录文件中有一列为图片url,需要下载所有记录行对应得图片
- Java 实现网络爬虫框架最近在做一个搜索相关的项目,需要爬取网络上的一些链接存储到索引库中,虽然有很多开源的强大的爬虫框架,但本着学习的态
- 引言RecyclerView是在Android5.0版本作为support-v7的一个组件出现,高效替代了最初的ListView等列表组件。
- 本文实例为大家分享了Androidstudio调用摄像头拍照并保存照片的具体代码,供大家参考,具体内容如下首先在manifest.xmlns
- 定义在类里面的类就叫做内部类。内部类的特点:在内部类中可以直接访问外部类的成员,包括私有的成员在外部类中不能直接访问内部类的成员,必须通过创
- 由于项目这种类型的图片按钮比较多,所以重写了ImageButton类。package me.henji.widget;import andr
- 前言XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场
- Flutter有个悠久的传统,便是运行flutter create并删除所有内容,然后在所有标准样板文件(boilerplate)当中尽力复
- 一、dfs(深度优先搜索)1.图的dfs/** * 深度优先搜索 * * @param node * @param set */publi
- 看代码吧~package com.mtpc.admin.controller.exportSql;import ch.qos.logback
- 前言:我们知道,在单体项目中,我们将用户信息存在 session 中,那么在该 session 过期之前,我们都可以从 session 中获
- 问题遇到问题:在前后端分离跨域访问的项目中shiro进行权限拦截失效 (即使有正确权限的访问也会被拦截) 时造成302重定向错误等问题报错:
- 前言接下来是 Spring Boot 统⼀功能处理模块了,也是 AOP 的实战环节,要实现的课程⽬标有以下 3 个:统⼀⽤户登录权限验证统⼀
- 前言InterruptedException异常可能没你想的那么简单!当我们在调用Java对象的wait()方法或者线程的sleep()方法
- 1.获取屏幕宽高方法1:int screenWidth = getWindowManager().getDefaultDisplay().g
- 前言之前在SpringBoot项目中简单使用定时任务,不过由于要借助cron表达式且都提前定义好放在配置文件里,不能在项目运行中动态修改任务
- 启用开发者模式①填写服务器配置启用开发模式需要先成为开发者,而且编辑模式和开发模式只能选择一个(进入微信公众平台=>开发=>基本
- 1.对包名的判断,异常则说明不存在:try {PackageManager pm = getPackageManager();pm.getP
- C# 中 PadLeft ,PadRight的用法简单来说就是给字符串实现补位。如:String.PadLeft(5,'0'