Mybatis中的Criteria條件查詢方式
Mybatis Criteria條件查詢
Criterion
Criterion是最基本,最底層的Where條件,用于字段級(jí)的篩選。
Criteria
Criteria包含一個(gè)Cretiron的集合,每一個(gè)Criteria對(duì)象內(nèi)包含的Cretiron之間是由AND連接的,是邏輯與的關(guān)系。
其它
Example類的distinct字段用于指定DISTINCT查詢。
orderByClause字段用于指定ORDER BY條件,這個(gè)條件沒有構(gòu)造方法,直接通過傳遞字符串值指定。
代碼示例
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的關(guān)系 */
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);
//方式一和方式二是等價(jià)的
// spring獲取mapper代理對(duì)象
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用法總結(jié)

用一對(duì)多內(nèi)斂查詢的時(shí)候,有的老鐵提出left join in 但是我和同事商討結(jié)果是用代碼寫處各種list然后stream存到數(shù)據(jù)庫中,這樣一來把計(jì)算壓力從數(shù)據(jù)庫存入服務(wù)器,當(dāng)并發(fā)量高了,這樣做的好處就體現(xiàn)在性能方面了。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
微信公眾號(hào)開發(fā)之設(shè)置自定義菜單實(shí)例代碼【java版】
這篇文章主要介紹了微信公眾號(hào)開發(fā)之設(shè)置自定義菜單實(shí)例代碼,本實(shí)例是為了實(shí)現(xiàn)在管理后臺(tái)實(shí)現(xiàn)微信菜單的添加刪除管理。需要的朋友可以參考下2018-06-06
Java?Zookeeper分布式分片算法超詳細(xì)講解流程
ZooKeeper是一個(gè)分布式的,開放源碼的分布式應(yīng)用程序協(xié)調(diào)服務(wù),是Google的Chubby一個(gè)開源的實(shí)現(xiàn),是Hadoop和Hbase的重要組件。它是一個(gè)為分布式應(yīng)用提供一致性的軟件,提供的功能包括:配置維護(hù)、域名服務(wù)、分布式同步、組服務(wù)等2023-03-03
關(guān)于ScheduledThreadPoolExecutor不執(zhí)行的原因分析
這篇文章主要介紹了關(guān)于ScheduledThreadPoolExecutor不執(zhí)行的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
淺談Java中ArrayList的擴(kuò)容機(jī)制
本文主要介紹了淺談Java中ArrayList的擴(kuò)容機(jī)制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Sentinel熱門詞匯限流的實(shí)現(xiàn)詳解
這篇文章主要介紹了使用Sentinel對(duì)熱門詞匯進(jìn)行限流的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

