mybatis与mybatis-plus同时使用
配置文件
mybatis-plus:
configuration:
#这个配置会将执行的sql打印出来,在开发或测试的时候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
#逻辑未删除值,(逻辑删除下有效)
logic-delete-value: 1
#逻辑未删除值,(逻辑删除下有效)需要注入逻辑策略LogicSqlInjector,以@Bean方式注 入
logic-not-delete-value: 0
#配置扫描xml
mapper-locations: classpath*:mapper/**/*Dao.xml # *.xml的具体路径
依赖
<!-- mybatis-plus-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.14</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
方法一:根据主键id去查询单个结果 selectById
/*** 方法一: 根据主键id去查询单个结果* T selectById(Serializable id); ---参数为主键类型*/User user1 = userMapper.selectById(1);/*** 返回值结果* {"id": 1,"name": "df","age": 222}*/
方法二:查询多条数据库中的记录 selectList
/*** 方法二: 查询多条数据库中的记录* List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);* ---参数为Wrapper可以为空说明没有条件的查询*/List<User> users1 = userMapper.selectList(null);/*** 运行结果集* [{"id": 1,"name": "df","age": 222},{"id": 2,"name": "wang","age": 22}]*/
方法三:查询多条数据库中的记录---条件查询 selectList(wrapper)
/*** 方法三:查询多条数据库中的记录---条件查询* List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);*///首先构造QueryWrapper来进行条件的添加QueryWrapper wrapper = new QueryWrapper();wrapper.eq("id",1);//相当于where id=1List<User> list = userMapper.selectList(wrapper);/*** 返回值结果* {"id": 1,"name": "df","age": 222}*/
福利赠送:条件构造器QueryWrapper常用方法
/***附加条件构造器QueryWrapper常用方法 ---这几个肯定够用了*/wrapper.eq("数据库字段名", "条件值"); //相当于where条件wrapper.between("数据库字段名", "区间一", "区间二");//相当于范围内使用的betweenwrapper.like("数据库字段名", "模糊查询的字符"); //模糊查询likewrapper.groupBy("数据库字段名"); //相当于group by分组wrapper.in("数据库字段名", "包括的值,分割"); //相当于inwrapper.orderByAsc("数据库字段名"); //升序wrapper.orderByDesc("数据库字段名");//降序wrapper.ge("数据库字段名", "要比较的值"); //大于等于wrapper.le("数据库字段名", "要比较的值"); //小于等于
方法四:根据主键的id集合进行多条数据的查询 selectBatchIds
/*** 方法四: 根据主键的id集合进行多条数据的查询* List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);* --条件为集合*/List list1 = Arrays.asList(1,2);List<User> list2 = userMapper.selectBatchIds(list1);/*** 运行结果集* [{"id": 1,"name": "df","age": 222},{"id": 2,"name": "wang","age": 22}]*/
方法五:分页查询 selectPage
/*** 方法五: 分页查询* IPage<T> selectPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);* ---参数为分页的数据+条件构造器*/IPage<User> page = new Page<>(1,2);//参数一:当前页,参数二:每页记录数//这里想加分页条件的可以如方法三自己构造条件构造器IPage<User> userIPage = userMapper.selectPage(page, null);/*** 运行结果集* {"records":[{"id": 1,"name": "df","age": 222},{"id": 2,"name": "wang","age": 22}],* "total": 0,"size": 2,"current": 1,"searchCount": true,"pages": 0 }*/
本文介绍如何结合使用MyBatis与MyBatis-Plus,包括配置方法、依赖引入及多种查询操作示例,如单个结果查询、多条记录查询、分页查询等。
1万+

被折叠的 条评论
为什么被折叠?



