您的位置:首页技术文章
文章详情页

springboot整合mybatis-plus实现多表分页查询的示例代码

浏览:29日期:2023-03-21 16:58:19

1.新建一个springboot工程

2.需要导入mybatis和mybatis-plus的依赖文件

<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.1</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency>

3.application.yml配置文件

server: port: 8080spring: datasource: url: jdbc:mysql://localhost:3306/mybatisplus?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC username: root password: 数据库密码mybatis: mapper-locations: classpath*:mapper/*.xmlmybatis-plus: mapper-locations: classpath:/mapper/*Mapper.xmllogging: level: com.tuanzi.*: debug

4.首先我们需要写一个类来配置分页插件

省略import@EnableTransactionManagement@Configuration@MapperScan('com.tuanzi.*.mapper*')public class MybatisPlusConfig { /** * 分页插件 */ @Bean public PaginationInterceptor paginationInterceptor(){ return new PaginationInterceptor(); }}

5.controller类

@RestController@RequestMapping('/user')public class UserController { @Autowired UserService userService; /** * 多表关联,分页查询(1对1) * @param page * @return */ @RequestMapping('/findAll') public Result<IPage<User>> findAll(@RequestBody Page<User> page){ return userService.pages(page); } /** * 多表关联,分页查询(1对多) * @param page * @return */ @RequestMapping('/selectAll') public Result<IPage<User>> selectAll(@RequestBody Page<User> page){ return userService.pageList(page); }}

6.service类

public interface UserService extends IService<User> { Result<IPage<User>> pages(Page<User> page); Result<IPage<User>> pageList(Page<User> page);}

7.service实现类

@Servicepublic class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Autowired UserMapper userMapper; @Override public Result<IPage<User>> pages(Page<User> page) { IPage<User> userIPage = userMapper.Pages(page); return Result.getSuccess('分页查询成功',userIPage); } @Override public Result<IPage<User>> pageList(Page<User> page) { IPage<User> userIPage = userMapper.pageList(page); return Result.getSuccess('分页查询成功',userIPage); }}

8.mapper接口

注意!!: 如果入参是有多个,需要加注解指定参数名才能在xml中取值

@Mapper@Repositorypublic interface UserMapper extends BaseMapper<User> { IPage<User> Pages(@Param('page') Page<User> page); IPage<User> pageList(@Param('page') Page<User> page);}

9.xml文件

一对一关联

<!-- 一对一 通用查询映射结果 --> <resultMap type='com.tuanzi.user.entity.User'> <result column='id' property='id' /> <result column='name' property='name' /> <result column='age' property='age' /> <result column='email' property='email' /> <!--assocication 一对一关联查询可以指定联合的JavaBean对象property='work'指定哪个属性是联合的对象javaType:指定这个属性对象的类型 --> <association property='work' javaType='com.tuanzi.user.entity.Work'> <result column='id' property='id' /> <result column='position' property='position' /> <result column='user_id' property='userId' /> </association> </resultMap>

一对多关联

<!-- 一对多 通用查询映射结果 --> <resultMap type='com.tuanzi.user.entity.User'> <result column='id' property='id' /> <result column='name' property='name' /> <result column='age' property='age' /> <result column='email' property='email' /> <!--collection定义关联结合类型的属性的封装规则property='workList'指定哪个属性是联合的对象ofType:指定集合里面元素的类型--> <collection property='workList' ofType='com.tuanzi.user.entity.Work'> <result column='id' property='id' /> <result column='position' property='position' /> <result column='user_id' property='userId' /> </collection> </resultMap>

SQL语句:

<select resultMap='BaseResultMap1'> select a.id id,a.name name,a.age age,a.email email,b.position position,b.user_id user_id from user a LEFT JOIN work b on a.id=b.user_id </select> <select resultMap='BaseResultMap2'> select a.id id,a.name name,a.age age,a.email email,b.position position,b.user_id user_id from user a LEFT JOIN work b on a.id=b.user_id </select>

10.这样就基本完成了!我这里省略了实体类

我们运行一下,用postman测试一下结果这里我们需要传2个参数,当然我们也可以不用传,因为mybatis-plus有默认值来看下mybatis-plus的page源码

springboot整合mybatis-plus实现多表分页查询的示例代码

效果图:

springboot整合mybatis-plus实现多表分页查询的示例代码

springboot整合mybatis-plus实现多表分页查询的示例代码

最后附赠源码地址:demo

到此这篇关于springboot整合mybatis-plus实现多表分页查询的示例代码的文章就介绍到这了,更多相关springboot整合mybatis-plus多表分页查询内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!

标签: Spring
相关文章: