Spring Boot 整合Mybatis-Plus联表分页查询

1.什么是Mybatis-plus?

Mybatis-plus是一个Mybatis的增强工具,在mybatis的基础上只做增强不做改变,提高效率。

引入mybatis-plus依赖:有了mybatis-plus依赖可以不加mybatis依赖

 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.7</version>
        </dependency>

连接数据库

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/company?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root

添加映射文件路径

mybatis-plus.mapper-locations=classpath:mapper/*.xml

添加日志:方便查询错误

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

添加分页查询

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor mybatisPage = new MybatisPlusInterceptor();
        mybatisPage.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return mybatisPage;
    }
}

实体类正常

@Data
@ApiModel(value = "员工类")
public class Worker  {
    @ApiModelProperty("员工编号")
    @TableId(type = IdType.AUTO)//id自增
    private Integer id;
    @ApiModelProperty("员工姓名")
    private String name;
    @ApiModelProperty("员工性别")
    private String sex;
    @ApiModelProperty("员工工资编号")
    private Integer salary;
    @ApiModelProperty("员工是否入党")
    private String party;
    @ApiModelProperty("员工入职时间")
  @JsonFormat(pattern ="yyyy-MM-dd",timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date inTime;
    @ApiModelProperty("员工地址")
    private String origin;
    @ApiModelProperty("员工部门编号")
    @TableField(value = "dep")
    private Integer depid;
    @TableField(exist = false)
    private Dep dep;

}

dao

public interface WorkerMapper extends BaseMapper<Worker> {
    int addWorker(Worker worker);
    int update(Worker worker);
    int delete(int id);
    Worker selectById(int id);
    List<Worker> selectAll();
//    分页查询
    IPage<Worker> selectByPage(IPage<Worker> page, @Param("p")Wrapper<Worker> queryWrapper);
}

mapper.xml

    <select id="selectByPage" resultMap="myMap">
        select*from worker w join dep d on d.id=w.dep ${p.customSqlegment}
    </select>

测试

@SpringBootTest
class SpringbootApplicationTests {
@Autowired
private WorkerMapper workerMapper;
    @Test
    void selectPage() {
        Page<Worker> page = new Page<>(1, 2);
        IPage<Worker> page1 = workerMapper.selectByPage(page, null);
        System.out.println("总页数"+page1.getPages());
        System.out.println("总条数"+page1.getTotal());
        System.out.println("当前页的记录"+page1.getRecords());
    }

}
  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spring boot整合mybatis-plus和pagehelper分页插件是一种常见的开发方式,可以实现数据库的分页查询功能。下面是一个简单示例项目的源码,以供参考: 首先,需要在pom.xml文件中添加相关依赖: ```xml <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- MyBatis Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <!-- PageHelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> </dependencies> ``` 在application.properties(或application.yml)文件中配置数据库连接信息: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis-plus.configuration.map-underscore-to-camel-case=true ``` 创建一个简单的实体类User: ```java public class User { private Long id; private String username; private String password; // 省略getter和setter方法 } ``` 创建一个Mapper接口UserMapper: ```java @Mapper public interface UserMapper extends BaseMapper<User> { // 省略其他方法 List<User> getUsersByPage(Page<User> page, @Param("username") String username); } ``` 创建一个Service接口UserService以及其实现类UserServiceImpl: ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Autowired private UserMapper userMapper; @Override public IPage<User> getUsersByPage(Page<User> page, String username) { return userMapper.getUsersByPage(page, username); } } ``` 在Controller中注入UserService,并进行分页查询操作: ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users") public IPage<User> getUsers(@RequestParam(value = "page", defaultValue = "1") Integer pageNum, @RequestParam(value = "size", defaultValue = "10") Integer pageSize, @RequestParam(value = "username", required = false) String username) { Page<User> page = new Page<>(pageNum, pageSize); return userService.getUsersByPage(page, username); } } ``` 至此,就完成了spring boot整合mybatis-plus和pagehelper分页插件的配置和使用。 请注意,这是一个简单示例项目,实际使用中可能需要根据需求进行适当调整和修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值