Springboot集成mybatis plus时的分页使用

1.添加架包

com.baomidou.mybatis-plus

2.配置文件并添加分页插件

# Tomcat
server:
  tomcat:
    uri-encoding: UTF-8
    max-threads: 1000
    min-spare-threads: 30
  port: 8080
  servlet:
    context-path: /

spring:
  profiles:
    active: dev
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
  servlet:
    multipart:
      max-file-size: 100MB
      max-request-size: 100MB
      enabled: true
  redis:
    database: 0
    host: 116.62.117.145
    port: 6379
    password: ziyun123      # 密码(默认为空)
    timeout: 6000ms  # 连接超时时长(毫秒)
    jedis:
      pool:
        max-active: 1000  # 连接池最大连接数(使用负值表示没有限制)
        max-wait: -1ms      # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-idle: 10      # 连接池中的最大空闲连接
        min-idle: 5       # 连接池中的最小空闲连接
  freemarker:
    suffix: .html
    request-context-attribute: request


renren:
  redis:
    open: true  # 是否开启redis缓存  true开启   false关闭
  shiro:
    redis: true # true表示shiro session存到redis里,需要开启redis,才会生效【分布式场景】
  schedule:
    open: true #打开 关闭定时任务


j2cache:
  configLocation: /j2cache-dev.properties
  openSpringCache: false

#mybatis
mybatis-plus:
  mapper-locations: classpath*:mapper/**/*.xml
  #实体扫描,多个package用逗号或者分号分隔
  typeAliasesPackage: io.renren.modules.*.entity;
  global-config:
    #主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
    id-type: 0
    #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
    field-strategy: 2
    #驼峰下划线转换
    db-column-underline: true
    #刷新mapper 调试神器
    refresh-mapper: true
    #数据库大写下划线转换
    #capital-mode: true
    # Sequence序列接口实现类配置
    #key-generator: com.baomidou.mybatisplus.incrementer.OracleKeyGenerator
    #逻辑删除配置
    logic-delete-value: -1
    logic-not-delete-value: 0
    #自定义填充策略接口实现
    #meta-object-handler: com.baomidou.springboot.xxx
    #自定义SQL注入器
    sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false
    call-setters-on-nulls: true

# 日志
logging:
#  level: debug
#  level.io.renren: debug
  path: logs/
  file: admin.log
  level:
      com.ziyun.erp.modules.*.dao: debug

单表分页

<select id="selectUserList" parameterType="map" resultType="com.test.mybatisplus.pojo.User">
        SELECT
        <include refid="Base_Column_List" />
        FROM user
        WHERE name LIKE CONCAT("%",#{name},"%")
    </select>

Java代码

 public static void testPageListUser() {
        Page<User> page = new Page<User>(1,10);

        Map<String, Object> params=new HashMap<String, Object>();
        condition.put("name", "测试");
        //String name="测试";
        List<User> lstUser = userDao.selectUserList(page,params);
      
    }

在人人框架中使用

单表不用写sql

package com.ziyun.erp.modules.member.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.erp.modules.member.dao.ConfigMemberLevelDao;
import com.ziyun.erp.modules.member.entity.ConfigMemberLevelEntity;
import com.ziyun.erp.modules.member.service.ConfigMemberLevelService;
import org.springframework.transaction.annotation.Transactional;


@Service("configMemberLevelService")
public class ConfigMemberLevelServiceImpl extends ServiceImpl<ConfigMemberLevelDao, ConfigMemberLevelEntity> implements ConfigMemberLevelService {

    @Autowired
    private ConfigMemberLevelDao configMemberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<ConfigMemberLevelEntity> page = this.selectPage(
                new Query<ConfigMemberLevelEntity>(params).getPage(),
                new EntityWrapper<ConfigMemberLevelEntity>()
        );

        return new PageUtils(page);
    }

多表查询分页需自己写sql

ServiceImpl

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;   
import com.java.common.utils.PageUtils; 

    @Override
    public PageUtils queryPageItemComments(Integer pageNo, Integer size, Integer commentLevel, String itemId) {
        Page<ItemCommentVO> page = new Page<>(pageNo, size);
        List<ItemCommentVO> list = this.baseMapper.queryItemComments(page, commentLevel, itemId);
        page.setRecords(list);
        return new PageUtils(page);
    }

Dao层方法的参数传入Pagination page即可,现在是IPage

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.java.pojo.Items;
import com.java.pojo.vo.ItemCommentVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface ItemsDao extends BaseMapper<Items> {

    List<ItemCommentVO> queryItemComments(IPage<ItemCommentVO> page, @Param("commentLevel") Integer commentLevel, @Param("itemId") String itemId);
	
}

Xml

    <select id="queryItemComments" resultType="com.java.pojo.vo.ItemCommentVO">
        SELECT
            ic.`comment_level` AS comment_level,
            ic.`content` AS content,
            ic.`sepc_name` AS sepcName,
            ic.`created_time` AS createdTime,
            u.`face` AS userFace,
            u.`nickname` AS nickname
            FROM
            `items_comments` ic
            LEFT JOIN
            `users` u
            ON
            ic.`user_id` = u.`id`
            WHERE
            ic.`item_id` = #{itemId}
        <if test="commentLevel != null">
            AND ic.`comment_level` = #{commentLevel}
        </if>
    </select>

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Spring Boot集成MyBatis Plus,你可以按照以下步骤进行操作: 1. 首先,在你的Spring Boot项目的pom.xml文件中添加MyBatis Plus的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本号</version> </dependency> ``` 2. 在application.properties或application.yml文件中配置数据库连接信息,例如: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=username spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 3. 创建一个Mapper接口,并使用MyBatis Plus的注解来定义SQL操作。例如: ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper extends BaseMapper<User> { // 自定义的SQL操作 } ``` 4. 创建实体类,并添加对应的注解。例如: ```java import com.baomidou.mybatisplus.annotation.TableName; @TableName("user") public class User { // 字段和对应的getter/setter方法 } ``` 5. 在启动类上添加`@MapperScan`注解,指定Mapper接口所在的包路径。例如: ```java import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 完成上述步骤后,你就成功地将MyBatis Plus集成到Spring Boot项目中了。你可以在Service层调用Mapper接口的方法来进行数据库操作。MyBatis Plus提供了许多便捷的方法,如分页查询、条件查询等,可以大大简化开发过程。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值