tkMybatis的Example使用

1、selectOneByExample的使用

Example userExample = new Example(User.class);
userExample.createCriteria().andEqualTo("userName",traceabilitySlice.getCreateBy()).andEqualTo("delFlag",0);
User sendUser = userMapper.selectOneByExample(userExample);

 2、selectByExample的使用

Example example = new Example(User.class);
example.createCriteria().andEqualTo("userName", user.getUserName());
List<User> list = userMapper.selectByExample(example);

 3、排序

Example添加查询条件时,除了使用andEqualTo外,还可设置字段降序和日期范围查询:

Example example = new Example(OfficialAccout.class);
//设置排序,STAT_DATE字段为升序
example.setOrderByClause("STAT_DATE ASC");
example.createCriteria().andBetween("statDate", last, now);
List<OfficialAccout> list = officialAccoutMapper.selectByExample(example);

 当排序单个字段时的使用方法

example.setOrderByClause("user_type ASC"); 

当排序多个字段时的使用方法,中间用逗号隔开

example.setOrderByClause("user_type ASC, level DESC");

或者

example.setOrderByClause("user_type , level DESC");

设置字段升序或降序的另一种方法:

Example example = new Example(IntelDrugStore.class);
example.orderBy("createTime").desc().orderBy("updateTime").desc();

4、andBetween的使用

Example example = new Example(PlantGrowIrrigation.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("plantGrowId", plantGrowId).andBetween("irrigationDate", DateUtil.parse(startDate), DateUtil.parse(endDate));
List<PlantGrowIrrigation> list = plantGrowIrrigationMapper.selectByExample(example);

5、andIn的使用

根据一个集合去数据库查询多条记录。

List<String> menuCodeList = new ArrayList<String>();
        menuCodeList.add("index@index");
        menuCodeList.add("backstage@table");
        menuCodeList.add("backstage@role@index");
        menuCodeList.add("backstage@department@list");
        menuCodeList.add("backstage@user@index");
        menuCodeList.add("admin@404");

        Example example = new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("menuCode", menuCodeList);
        List<Menu> mList =  mMapper.selectByExample(example);

6、and或or的使用

and和or的使用主要是如下三种场景:

1)、where (条件1 and 条件2) or (条件3 and 条件4)

// 条件1 and 条件2
example.createCriteria()
        .andEqualTo("isDeleted",IsDeleted.NOT_DELETED)
        .andEqualTo("name", projectCatalogEntity.getName());
// or (条件3 and 条件4)
example.or(example.createCriteria()
        .andEqualTo("isDeleted",IsDeleted.NOT_DELETED)
        .andEqualTo("code", projectCatalogEntity.getCode()));

即:WHERE ( is_deleted = ? and name = ? ) or ( is_deleted = ? and code = ? )

2)、where (条件1 and 条件2) and (条件3 or 条件4)

// 条件1 and 条件2
example.createCriteria()
        .andEqualTo("isDeleted",IsDeleted.NOT_DELETED))
        .andEqualTo("parentId", projectCatalogEntity.getParentId());
// and (条件3 or 条件4)
example.and(example.createCriteria()
        .andEqualTo("name", projectCatalogEntity.getName())
        .orEqualTo("code", projectCatalogEntity.getCode()));

即:WHERE ( is_deleted = ? and parent_id = ? ) and ( name = ? or code = ? )

注意:条件3既可以用andEqualTo,也可以用orEqualTo。

案例:

Example example = new Example(Prepare.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("supplyId", prepare.getSupplyId());
        Example.Criteria c = example.createCriteria();
        c.orEqualTo("status", "0");
        c.orEqualTo("status", "1");
        example.and(c);
        List<Prepare> list = prepareMapper.selectByExample(example);

即:WHERE ( supply_id = ? )  AND ( status = ? OR status = ? ) 

7、updateByExampleSelective的使用

int updateByExampleSelective(@Param("record") T var1, @Param("example") Object var2);

第一个参数存着要修改的值。第二个参数为条件.

示例:

CrudeDrugs crudeDrugs = new CrudeDrugs();
        crudeDrugs.setMaterialName(medMaterial.getMaterialCommonName());
        Example example = new Example(CrudeDrugs.class);
        example.createCriteria().andEqualTo("materialCode", medMaterial.getMaterialCode());
        crudeDrugsMapper.updateByExampleSelective(crudeDrugs, example);
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure! 这里是一个简单的使用 TkMyBatis 的示例: 首先,你需要在你的项目中引入 TkMyBatis 的依赖,可以在 pom.xml 文件中添加以下内容: ```xml <dependency> <groupId>org.mybatis</groupId> <artifactId>tk-mybatis</artifactId> <version>版本号</version> </dependency> ``` 接下来,你需要创建一个实体类,用于映射数据库表的字段。例如,我们创建一个名为 User 的实体类: ```java public class User { private Long id; private String name; // 省略其他字段的 getter 和 setter 方法 } ``` 然后,你需要创建一个 Mapper 接口,用于定义数据库操作的方法。这个接口需要继承 `Mapper<T>`,其中 T 是你的实体类。例如,我们创建一个名为 UserMapper 的接口: ```java public interface UserMapper extends Mapper<User> { // 省略其他方法 } ``` 接下来,在你的配置文件中配置 MyBatisTkMyBatis。例如,在 application.properties 文件或 application.yml 文件中添加以下内容: ```yaml mybatis: mapper-locations: classpath:mapper/*.xml config-location: classpath:mybatis-config.xml ``` 在这个配置文件中,你需要指定 Mapper 接口和映射文件的位置。 然后,你可以创建一个名为 UserMapper.xml 的映射文件,在这个文件中定义具体的 SQL 查询语句。例如,我们可以编写一个简单的查询所有用户的 SQL: ```xml <mapper namespace="com.example.mapper.UserMapper"> <select id="selectAll" resultType="com.example.entity.User"> SELECT * FROM user </select> </mapper> ``` 最后,在你的代码中使用 UserMapper 接口来进行数据库操作。例如,你可以在你的 Service 类中注入 UserMapper 并调用其方法: ```java @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> getAllUsers() { return userMapper.selectAll(); } } ``` 这就是一个简单的 TkMyBatis 使用示例。当然,TkMyBatis 还提供了许多其他功能,比如条件查询、分页查询等,你可以根据具体需求进行使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值