gpt4 book ai didi

Mybatis中的Criteria条件查询方式

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 37 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Mybatis中的Criteria条件查询方式由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

Mybatis Criteria条件查询

Criterion

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

Criteria

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

其它

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

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

代码示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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条件查询方式的文章就讲到这里了,如果你想了解更多关于Mybatis中的Criteria条件查询方式的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com