系列十四、spring5整合MyBatis分页

一、业务代码

1.1、pom.xml


    
    
      mysql
      mysql-connector-java
      8.0.26
    
    
    
      org.springframework
      spring-jdbc
      5.3.27
    
    
      org.mybatis
      mybatis
      3.5.11
    
    
      org.mybatis
      mybatis-spring
      2.1.0
    
    
      com.github.pagehelper
      pagehelper
      5.3.2
    



    
      org.springframework
      spring-beans
      5.2.5.RELEASE
    
    
      org.springframework
      spring-context
      5.2.5.RELEASE
    
    
      org.springframework
      spring-core
      5.2.5.RELEASE
    
    
      org.springframework
      spring-expression
      5.2.5.RELEASE
    


    
      commons-logging
      commons-logging
      1.1.1
    
    
      com.alibaba
      druid
      1.2.16
    
    
      junit
      junit
      4.13.2
    
    
      com.alibaba
      fastjson
      1.2.76
    

    
    
      org.projectlombok
      lombok
      1.18.22
    
    
      org.slf4j
      slf4j-api
      1.7.32
    
    
      ch.qos.logback
      logback-classic
      1.2.10
    

1.2、UserDO

package org.star.entity.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Author:
 * @Date: 2023/8/28 16:51
 * @Description:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserDO implements Serializable {
    /**
     * 编号
     */
    private Long id;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 手机号码
     */
    private String mobile;

}

1.3、UserMapper

package org.star.mapper;

import org.star.entity.model.UserDO;

import java.util.List;

/**
 * @Author:
 * @Date: 2023/8/28 16:53
 * @Description:
 */
public interface UserMapper {

    /**
     * 查询所有用户信息
     * @return
     */
    List listAllUser();

}

1.4、UserMapper.xml





    

1.5、UserService

package org.star.service;

import com.github.pagehelper.PageInfo;
import org.star.entity.model.UserDO;

import java.util.List;

/**
 * @Author: 
 * @Date: 2023/8/28 17:41
 * @Description:
 */
public interface UserService {

    /**
     * 查询所有用户信息
     *
     * @return
     */
    List listAllUser();

    /**
     * 分页查询用户信息
     * @param pageNum   当前页码
     * @param pageSize  每页显示的记录数
     * @return
     */
    PageInfo pageUser(int pageNum, int pageSize);

}

1.6、UserServiceImpl

package org.star.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.star.entity.model.UserDO;
import org.star.mapper.UserMapper;
import org.star.service.UserService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 
 * @Date: 2023/8/28 17:41
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public List listAllUser() {
        return userMapper.listAllUser();
    }

    @Override
    public PageInfo pageUser(int pageNum, int pageSize) {
        // 设置当前页码以及每页显示的记录数
        PageHelper.startPage(pageNum,pageSize);
        // 执行查询
        List userDOS = userMapper.listAllUser();
        // 设置pageInfo对象
        PageInfo pageInfo = new PageInfo<>(userDOS);

        return pageInfo;
    }
}

二、配置信息

2.1、jdbc.properties

db.driverClass=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/20230828_spring5?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT
db.username=root
db.password=123456

2.2、applicationContext.xml




    
    

    
    
        
        
        
        
    


    
    
        
        
        
        
            
                
            
        
        
            
                
                    
                        
                        
                            helperDialect=mysql
                            reasonable=true
                        
                    
                
            
        
    

    
    
        
        
    

三、测试

3.1、AppTest

package org.star;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.star.entity.model.UserDO;
import org.star.mapper.UserMapper;
import org.star.service.UserService;
import org.star.service.impl.UserServiceImpl;

import java.util.List;

/**
 * Unit test for simple App.
 */
@Slf4j
public class AppTest {

    private ClassPathXmlApplicationContext context = null;

    @Before
    public void initApplicationContext() {
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void listAllUserTest() {
        userMapperTest();
        // userServiceTest();
    }

    @Test
    public void pageUserTest() {
        UserService userService = context.getBean(UserServiceImpl.class);
        PageInfo pageInfo = userService.pageUser(1,10);

        log.info("pageInfo:{}", JSON.toJSONString(pageInfo));
    }

    private void userServiceTest() {
        UserService userService = context.getBean(UserServiceImpl.class);
        List userDOS = userService.listAllUser();
        System.out.println("userService userDOS = " + userDOS);
    }

    private void userMapperTest() {
        UserMapper userMapper = context.getBean(UserMapper.class);
        List userDOS = userMapper.listAllUser();
        System.out.println("userMapper userDOS = " + userDOS);
    }


}

你可能感兴趣的:(Spring5系列,mybatis,数据库,spring)