Mybatis 您所在的位置:网站首页 example对象 Mybatis

Mybatis

2023-12-14 03:35| 来源: 网络整理| 查看: 265

Mybatis-generator生成Example的使用心得 1.Generator插件生成Mapper和Example文件介绍1-1.Mapper文件1-2.Example文件 2.使用中遇到的问题2-1.or()和createcriteria的区别?2-2.selectByExample() 和 selectByExampleWithBLOGs()的区别?2-3.Mybatis逆向工程如何多表查询?

上篇介绍了SpringBoot整合generator插件的过程以及一些坑。 所以本篇文章主要介绍生成出来的实体类、Example、mapper如何进行使用,以及小麻袋在使用过程中遇到的一些问题。 (上篇整合generator的博客在这哦:https://blog.csdn.net/qq_43318965/article/details/106635346)

1.Generator插件生成Mapper和Example文件介绍 1-1.Mapper文件

mapper层接口中主要提供一些关于主键和通过Example类作为参数的方法,如下:

方法功能说明int countByExample(UserExample example) thorws SQLException按条件计数int deleteByPrimaryKey(Integer id) thorws SQLException按主键删除int deleteByExample(UserExample example) thorws SQLException按条件查询String/Integer insert(User record) thorws SQLException插入数据(返回值为ID)T selectByPrimaryKey(Integer id) thorws SQLException按主键查询ListselectByExample(UserExample example) thorws SQLException按条件查询ListselectByExampleWithBLOGs(UserExample example) thorws SQLException按条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。int updateByPrimaryKey(User record) thorws SQLException按主键更新int updateByPrimaryKeySelective(User record) thorws SQLException按主键更新值不为null的字段int updateByExample(User record, UserExample example) thorws SQLException按条件更新int updateByExampleSelective(User record, UserExample example) thorws SQLException按条件更新值不为null的字段 1-2.Example文件

mybatis-generator (逆向工程)会生成实例以及实例对应的Example,其主要作用就是能够自己组装sql语句中where后面的条件。 xxxExample example = new xxxExample(); Criteria criteria = new Example().createCriteria();

方法功能说明example.setOrderByClause(“字段名 ASC”);添加升序排列条件,DESC为降序example.setDistinct(false)去除重复,boolean型,true为选择不重复的记录。criteria.andXxxIsNull添加字段xxx为null的条件criteria.andXxxIsNotNull添加字段xxx不为null的条件criteria.andXxxEqualTo(value)添加xxx字段等于value条件criteria.andXxxNotEqualTo(value)添加xxx字段不等于value条件criteria.andXxxGreaterThan(value)添加xxx字段大于value条件criteria.andXxxGreaterThanOrEqualTo(value)添加xxx字段大于等于value条件criteria.andXxxLessThan(value)添加xxx字段小于value条件criteria.andXxxLessThanOrEqualTo(value)添加xxx字段小于等于value条件criteria.andXxxIn(List)添加xxx字段值在List条件criteria.andXxxNotIn(List)添加xxx字段值不在List条件criteria.andXxxLike(“%”+value+”%”)添加xxx字段值为value的模糊查询条件criteria.andXxxNotLike(“%”+value+”%”)添加xxx字段值不为value的模糊查询条件criteria.andXxxBetween(value1,value2)添加xxx字段值在value1和value2之间条件criteria.andXxxNotBetween(value1,value2)添加xxx字段值不在value1和value2之间条 2.使用中遇到的问题 2-1.or()和createcriteria的区别?

createcriteria,当没有规则时,则加入到现有规则,但有规则时,不再加入到现有规则,只是返回创建的规则,如源码所示:

public Criteria createCriteria() { Criteria criteria = createCriteriaInternal(); if (oredCriteria.size() == 0) { oredCriteria.add(criteria); } return criteria; }

or,创建的规则,加入到规则集中,并且是or的关系,如源码所示:

public Criteria or() { Criteria criteria = createCriteriaInternal(); oredCriteria.add(criteria); return criteria; } 2-2.selectByExample() 和 selectByExampleWithBLOGs()的区别?

selectByExampleWithBLOGs这个方法是我在开发过程中遇到问题后,才发现有这样一个方法。 当时是因为我表中有个字段类型为text长字段,使用selectByExample查询时,基本字段都能查找到,除了这个text类型的字段一直为空。

1.两个方法的返回的resultMap不同 selectByExample 方法返回:BaseResultMap selectByExampleWithBLOBs 方法返回:ResultMapWithBLOBs ResultMapWithBLOBs 定义时,继承了BaseResultMap,并且拥有自己特殊的字段,该字段通常是longvarchar类型

2.使用场景不同 若检索大字段时,则需要使用selectByExampleWithBLOBs ,一般情况则使用selectByExample 即可。

2-3.Mybatis逆向工程如何多表查询?

mybatisGenerator生成的Example都是用于单表操作的,如果需要进行多表查询,就和mybatis进行多表查询一样,在xml中自己写sql。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有