Mybatis中的Criteria條件查詢方式
Mybatis Criteria條件查詢
Criterion
Criterion是最基本,最底層的Where條件,用于字段級的篩選。
Criteria
Criteria包含一個Cretiron的集合,每一個Criteria對象內(nèi)包含的Cretiron之間是由AND連接的,是邏輯與的關(guān)系。
其它
Example類的distinct字段用于指定DISTINCT查詢。
orderByClause字段用于指定ORDER BY條件,這個條件沒有構(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); //方式一和方式二是等價的 // 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用法總結(jié)
用一對多內(nèi)斂查詢的時候,有的老鐵提出left join in 但是我和同事商討結(jié)果是用代碼寫處各種list然后stream存到數(shù)據(jù)庫中,這樣一來把計算壓力從數(shù)據(jù)庫存入服務(wù)器,當(dāng)并發(fā)量高了,這樣做的好處就體現(xiàn)在性能方面了。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 在啟動時進(jìn)行配置文件加解密的方法詳解
這篇文章主要介紹了Spring Boot 在啟動時進(jìn)行配置文件加解密的方法,本文通過實(shí)例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06Java源碼解析CopyOnWriteArrayList的講解
今天小編就為大家分享一篇關(guān)于Java源碼解析CopyOnWriteArrayList的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01Java?詳細(xì)講解線程的狀態(tài)及部分常用方法
在Java程序中,一個線程對象只能調(diào)用一次start()方法啟動新線程,并在新線程中執(zhí)行run()方法。一旦run()方法執(zhí)行完畢,線程就結(jié)束了,本篇來講解Java線程的狀態(tài)以及部分常用方法2022-04-04java開發(fā)_圖片截取工具實(shí)現(xiàn)原理
本文將詳細(xì)介紹java開發(fā)_圖片截取工具實(shí)現(xiàn)原理,需要了解的朋友可以參考下2012-11-11Java避免UTF-8的csv文件打開中文出現(xiàn)亂碼的方法
這篇文章主要介紹了Java避免UTF-8的csv文件打開中文出現(xiàn)亂碼的方法,結(jié)合實(shí)例形式分析了java操作csv文件時使用utf-16le編碼與utf8編碼相關(guān)操作技巧,需要的朋友可以參考下2019-07-07