1.打开IntelliJ IDEA  File->new->project->Spring Initializr,选择jdk后下一步

idea+springboot+mybatis+pagehelper分页+log4j

 

2.更改Group+Artifact,选择java Version,我这里选择默认

idea+springboot+mybatis+pagehelper分页+log4j

 

3.这里只选择web

idea+springboot+mybatis+pagehelper分页+log4j

 

4. 更改项目名和项目路径

idea+springboot+mybatis+pagehelper分页+log4j

 

5.完成后的项目结构

idea+springboot+mybatis+pagehelper分页+log4j

6.添加maven依赖包,将下面的依赖复制到pom.xml文件中

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.1</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

 

7.把application.properties 改为application.yml,后在该文件中添加配置属性

idea+springboot+mybatis+pagehelper分页+log4j

修改启动的端口号,配置数据库连接,注意冒号后面一定要有空格

idea+springboot+mybatis+pagehelper分页+log4j

 

8.测试能否跑起来,在com.example.demo下建立controller 包,在包下新建一个DemoController 类

idea+springboot+mybatis+pagehelper分页+log4j

 

9.在DemoController 里添加一个test方法

idea+springboot+mybatis+pagehelper分页+log4j

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public String test(String name){
        return name;
    }
}

 

10.使用postman 进行测试

idea+springboot+mybatis+pagehelper分页+log4j

 

11.整合mybatis

 

1.添加maven依赖

idea+springboot+mybatis+pagehelper分页+log4j

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.1</version>
</dependency>

2.在数据库新建user表


3.在com.example.demo 下新建entity 包,在该包下建立User类

idea+springboot+mybatis+pagehelper分页+log4j

package com.example.demo.entity;

public class User {
    private Integer id;
    private String name;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

 

5.在resources资源包下建立mapping文件夹,在该文件夹下新建UserMapping.xml文件

idea+springboot+mybatis+pagehelper分页+log4j

 

6.在com.example.demo下新建mapper 包,在该包下新建UserMapper 接口

idea+springboot+mybatis+pagehelper分页+log4j

 

7.填写UserMapping.xml文件

<?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.example.demo.mapper.UserMapper">
    <resultMap id="BaseResultMap" type="com.example.demo.entity.User">
        <result column="id" jdbcType="INTEGER" property="id" javaType="Integer"></result>
        <result column="name" jdbcType="VARCHAR" property="name" javaType="String"></result>
        <result column="age" jdbcType="INTEGER" property="age" javaType="Integer"></result>
        
    </resultMap>
    <select id="getUserList" resultType="com.example.demo.entity.User">
        select * from user
    </select>
</mapper>

8.填写UserMapper.java

idea+springboot+mybatis+pagehelper分页+log4j

package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {
    List<User> getUserList();//这里的方法名和xml定义的要一致
}

 

9.在com.example.demo 下新建service包,在该包下新建UserService类idea+springboot+mybatis+pagehelper分页+log4j

idea+springboot+mybatis+pagehelper分页+log4j

package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;//注入UserMapper,这里的警告不用管

    public List<User> getUserList(){
        return userMapper.getUserList();
    }
}

 

10.在DemoController 添加以下代码

idea+springboot+mybatis+pagehelper分页+log4j

 

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DemoController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public String test(String name){
        return name;
    }

    @RequestMapping(value = "/getUserList",method = RequestMethod.POST)
    public List<User> getUserList(){
        return userService.getUserList();
    }
}

 

11.在postman进行测试

idea+springboot+mybatis+pagehelper分页+log4j

 

12.以上springboot 整合mybatis完毕

 

13.使用pagehelper 进行分页

1.添加maven 依赖

idea+springboot+mybatis+pagehelper分页+log4j

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.10</version>
</dependency>

2.在application.yml内配置pagehelper

idea+springboot+mybatis+pagehelper分页+log4j

pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql
  page-size-zero: true

3.在DemoController.java 中添加下面代码

idea+springboot+mybatis+pagehelper分页+log4j

 

@RequestMapping(value = "/getUserListPage",method = RequestMethod.POST)
public PageInfo<User> getUserListPage(Integer pageNum,Integer pageSize){
    PageHelper.startPage(pageNum,pageSize);
    List<User> users=userService.getUserList();
    PageInfo<User> pageInfo=new PageInfo<>(users);
    return pageInfo;
}

 

4.在postman中测试

idea+springboot+mybatis+pagehelper分页+log4j

 

5.至此pagehelper 整合完成

 

整合log4j

 

1.添加maven依赖

idea+springboot+mybatis+pagehelper分页+log4j

 

2.在resources文件下新建log4j.properties

idea+springboot+mybatis+pagehelper分页+log4j

idea+springboot+mybatis+pagehelper分页+log4j

idea+springboot+mybatis+pagehelper分页+log4j

 

3.在DemoController 中添加以下代码

idea+springboot+mybatis+pagehelper分页+log4j

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DemoController {
    protected Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public String test(String name){
        log.info("进入了test方法。。。");
        return name;
    }

 

4.在postman上测试结果如下

idea+springboot+mybatis+pagehelper分页+log4j

idea+springboot+mybatis+pagehelper分页+log4j

 

idea+springboot+mybatis+pagehelper分页+log4j

idea+springboot+mybatis+pagehelper分页+log4j

 

 

相关文章: