mybatis generator:example的举例说明

迷南。 2023-09-26 21:37 69阅读 0赞

generator生成对象和xml的时候,会生成一个example对象,看一下example在查询、修改当中的使用,本文在这里进行记载,方便以后使用:

一、常用的方法举例:

所需依赖:

  1. <!-- 映射关系:自动生成实体 -->
  2. <dependency>
  3. <groupId>org.mybatis.generator</groupId>
  4. <artifactId>mybatis-generator-core</artifactId>
  5. <version>1.3.7</version>
  6. </dependency>

*搭建过程略,后期会专门出一个搭建过程的文章。*

1、简单查询

  1. DemoExample example = new DemoExample();
  2. example.createCriteria().andIdEqualTo(1L);
  3. List<Demo> demos = demoMapper.selectByExample(example);
  4. System.out.println(demos);

2、模糊查询

  1. DemoExample example = new DemoExample();
  2. //注意条件
  3. example.createCriteria().andUserNameLike("%" + "l" + "%");
  4. List<Demo> demos = demoMapper.selectByExample(example);
  5. System.out.println(demos);

3、分页

  1. DemoExample example = new DemoExample();
  2. RowBounds rowBounds = new RowBounds(0, 10);
  3. List<Demo> demos = demoMapper.selectByExampleWithRowbounds(example,rowBounds);
  4. System.out.println(demos)

4、复杂的其它查询:

  1. DemoExample example = new DemoExample();
  2. //年龄在0~40之间
  3. DemoExample.Criteria criteria = example.createCriteria();
  4. criteria.andUserNameLike("%" + "l" + "%").andAgeBetween(0L,40L);
  5. //createAt不在【0~10】
  6. DemoExample.Criteria criteria2 = example.createCriteria();
  7. criteria2.andCreateAtNotBetween(0L,10L);
  8. example.or(criteria2);
  9. List<Demo> demos = demoMapper.selectByExample(example);
  10. System.out.println(demos);

二、方法列表

1、mapper文件:

1b4324f006b640309ff558b5f95f8de4.png

2、example文件:

cec2939656224b2998e733864a4e2a06.png

发表评论

表情:
评论列表 (有 0 条评论,69人围观)

还没有评论,来说两句吧...

相关阅读

    相关 冒泡排序举例

    冒泡排序 思路:冒泡排序主要是比较数组中两个相邻元素间的大小,如果前一个数比后一个数大,就交换顺序 每一次比较都会产生一个最大或者最小 每下一次循环都可

    相关 举例同步和异步

    举例说明同步和异步 > 如果系统中存在临界资源(资源数量少于竞争资源的线程数量的资源),例如正在写的数据以后可能被另一个线程读到,或者正在读的数据可能已经被另一个线程写过

    相关 举例什么是事务

    1.什么是事务: 事务是程序中一系列严密的操作,所有操作执行必须成功完成,否则在每个操作所做的更改将会被撤销,这也是事务的原子性(要么成功,要么失败)。 2.事务特性:

    相关 保理业务举例

    一个案例讲清楚什么叫保理: 1、保理:A是甲国卖汽车的,突然接了个大单子,来自一个乙国的汽车店B要进1000万的车。但B有个要求,说要3个月才能卖完,所以想等3个月后再结货款

    相关 String.join方法使用举例

    以前循环字符串数组,需要在元f素后面加上符号来区分开来时,我们通常会遇到首位得元素还需要做特殊处理,但是Jdk1.8后,String.join方法很好的解决了这个问题。