【Java专题_02】springboot+mybatis+pagehelper分页插件+druid数据源详细教程

2024-03-04 17:11:22 浏览数 (1)

前言

涉及的技术栈:springboot、mybatis、分页插件pagehelper、Druid数据源 gitee源码地址:https://gitee.com/shawsongyue/sevenhee_modules/tree/master/springboot_mybatis

1.基础maven依赖

代码语言:javascript复制
        <!--MyBatis分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.10</version>
        </dependency>

        <!--druid数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

        <!--Mysql数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>

        <!--mybatis依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        
        <!--单元测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!--集成springmvc框架并实现自动配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

2.目录结构与配置文件

代码语言:javascript复制
#服务配置
server:
  port: 8080

#spring配置
spring:
  #数据源
  datasource:
    url: jdbc:mysql://localhost:3306/sevenhee_modules?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: gnnykj.2020
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      initial-size: 5
      min-idle: 5
      max-active: 20
      max-wait: 60000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 30000
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: true
      test-on-return: false
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      filter:
        stat:
          merge-sql: true
          slow-sql-millis: 5000
      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
        session-stat-enable: true
        session-stat-max-count: 100
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        reset-enable: true
        login-username: admin
        login-password: admin
        allow: 127.0.0.1

#mybatis配置
mybatis:
  #xml文件配置路径(两种选择)
  mapper-locations:
    #资源目录
#    - classpath:mapper/*.xml
    #src文件目录下
    - classpath*:com/**/mapper/impl/*.xml
  #实体类别名配置
  type-aliases-package: com.xsy.sevenhee.modules.entity

#分页插件配置
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

3.创建数据库表

4.创建实体层(SysUser.class)

代码语言:javascript复制
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

/**
 * 系统用户
 *
 * @author xiaosongyue
 * @date 2021/04/14 15:22:11
 */
@Data
@NoArgsConstructor
public class SysUser {
    private Long id;
    private String username;
    private String password;
    private Date createTime;
    private Date updateTime;
    private int deleted;
    private int version;
}

4.创建mapper层(SysUserMapper.class)

代码语言:javascript复制
import com.xsy.sevenhee.modules.entity.SysUser;

import java.util.List;

/**
 * 系统用户映射器
 *
 * @author xiaosongyue
 * @date 2021/04/14 15:17:17
 */
public interface SysUserMapper {

    /**
     * 查询全部系统用户
     *
     * @return {@link List<SysUser>}
     */
    List<SysUser> selectAllSysUser();

    /**
     * 插入系统用户
     *
     * @param sysUser 系统用户
     * @return int
     */
    int insertSysUser(SysUser sysUser);

    /**
     * 删除系统用户
     *
     * @param id id
     * @return int
     */
    int deleteById(Long id);

    /**
     * 更新系统用户
     *
     * @param sysUser 系统用户
     * @return int
     */
    int updateSysUserById(SysUser sysUser);
}

4.创建xml映射文件(SysUserMapper.xml)

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xsy.sevenhee.modules.mapper.SysUserMapper">

    <select id="selectAllSysUser" resultType="SysUser">
        select * from sys_user
    </select>

    <insert id="insertSysUser" parameterType="SysUser">
        insert into sys_user values (#{id},#{username},#{password},#{createTime},#{updateTime},#{deleted},#{version})
    </insert>

    <delete id="deleteById" parameterType="Long">
        delete from sys_user where id=#{id}
    </delete>

    <update id="updateSysUserById" parameterType="SysUser">
        update sys_user set username=#{username},password=#{password},create_time=#{createTime},update_time=#{updateTime},deleted=#{deleted},version=#{version} where id=#{id}
    </update>

</mapper>

6.创建service层(SysUserService.class)

代码语言:javascript复制
import com.github.pagehelper.PageInfo;
import com.xsy.sevenhee.modules.entity.SysUser;

import java.util.List;

/**
 * 系统用户服务
 *
 * @author xiaosongyue
 * @date 2021/04/14 15:53:23
 */
public interface SysUserService {

    /**
     * 获取全部用户
     *
     * @return {@link List<SysUser>}
     */
    List<SysUser> getAllSysUser();

    /**
     * 添加系统用户
     *
     * @param sysUser 系统用户
     * @return int
     */
    int addSysUser(SysUser sysUser);

    /**
     * 根据id删除用户
     *
     * @param id id
     * @return int
     */
    int deleteById(Long id);

    /**
     * 根据用户id修改用户信息
     *
     * @param sysUser 系统用户
     * @return int
     */
    int modifySysUserById(SysUser sysUser);

    /**
     * 获取全部用户,分页
     *
     * @param pageNum  页码
     * @param pageSize 页面大小
     * @return {@link PageInfo<SysUser>}
     */
    PageInfo<SysUser> getAllSysUserByPage(Integer pageNum, Integer pageSize);
}

6.创建service层实现类(SysUserServiceImpl.class)

代码语言:javascript复制
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xsy.sevenhee.modules.entity.SysUser;
import com.xsy.sevenhee.modules.mapper.SysUserMapper;
import com.xsy.sevenhee.modules.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统用户服务实现类
 *
 * @author xiaosongyue
 * @date 2021/04/14 15:55:13
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> getAllSysUser() {
        return sysUserMapper.selectAllSysUser();
    }

    @Override
    public PageInfo<SysUser> getAllSysUserByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(sysUserMapper.selectAllSysUser());
    }

    @Override
    public int addSysUser(SysUser sysUser) {
        return sysUserMapper.insertSysUser(sysUser);
    }

    @Override
    public int deleteById(Long id) {
        return sysUserMapper.deleteById(id);
    }

    @Override
    public int modifySysUserById(SysUser sysUser) {
        return sysUserMapper.updateSysUserById(sysUser);
    }
}

7.创建controller层(SysUserController.class)

代码语言:javascript复制
import com.github.pagehelper.PageInfo;
import com.xsy.sevenhee.modules.entity.SysUser;
import com.xsy.sevenhee.modules.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 系统用户控制器
 *
 * @author xiaosongyue
 * @date 2021/04/14 16:24:54
 */
@RestController
@RequestMapping("/sys_user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 插入
     */
    @GetMapping("/insert")
    public void insert(){
        SysUser sysUser = new SysUser();
        sysUser.setId(22L);
        sysUser.setCreateTime(new Date());
        sysUser.setPassword("1234567");
        sysUser.setUpdateTime(new Date());
        sysUser.setDeleted(0);
        sysUser.setVersion(1);
        sysUser.setUsername("xsxsxsxs");
        System.out.println(sysUserService.addSysUser(sysUser));
    }

    /**
     * 删除通过id
     */
    @GetMapping("/deleteById")
    public void deleteById(){
        System.out.println(sysUserService.deleteById(22L));
    }

    /**
     * 更新
     */
    @GetMapping("/update")
    public void update(){
        SysUser sysUser = new SysUser();
        sysUser.setUsername("opopop");
        sysUser.setId(22L);
        sysUser.setCreateTime(new Date());
        sysUser.setPassword("1234dfasdsds567");
        sysUser.setUpdateTime(new Date());
        sysUser.setDeleted(0);
        sysUser.setVersion(1);
        System.out.println(sysUserService.modifySysUserById(sysUser));
    }

    /**
     * 获取所用用户
     */
    @GetMapping("/getAllSysUser")
    public void getAllSysUser(){
        List<SysUser> sysUsers = sysUserService.getAllSysUser();
        System.out.println("测试:" sysUsers);
    }

    /**
     * 获取所有用户并分页
     */
    @GetMapping("/getAllSysUserByPage")
    public void getAllSysUserByPage(){
        PageInfo<SysUser> sysUsers = sysUserService.getAllSysUserByPage(1,3);
        System.out.println("分页测试:" sysUsers);
    }
}

8.创建主启动类(MybatisApplication.class)

代码语言:javascript复制
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * mybatis应用程序
 *
 * @author xiaosongyue
 * @date 2021/04/14 15:44:28
 */
@SpringBootApplication
public class MybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class,args);
    }
}

9.访问controller层接口测试

访问路径:http://localhost:8080/sys_user/getAllSysUser

10.访问druid数据源监控

访问路径:http://localhost:8080/druid/login.html 账号密码都是:admin

11.gitee源码地址:

https://gitee.com/shawsongyue/sevenhee_modules/tree/master/springboot_mybatis

0 人点赞