idea+springboot+mybatis+pagehelper分页+log4j

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

idea+springboot+mybatis+pagehelper分页+log4j_第1张图片

 

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

idea+springboot+mybatis+pagehelper分页+log4j_第2张图片

 

3.这里只选择web

idea+springboot+mybatis+pagehelper分页+log4j_第3张图片

 

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

idea+springboot+mybatis+pagehelper分页+log4j_第4张图片

 

5.完成后的项目结构

idea+springboot+mybatis+pagehelper分页+log4j_第5张图片

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


    
        org.springframework.boot
        spring-boot-starter-jdbc
    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.0.1
    

    
        mysql
        mysql-connector-java
        runtime
    

    
        org.springframework.boot
        spring-boot-starter-test
        test
    

 

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

idea+springboot+mybatis+pagehelper分页+log4j_第6张图片

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

idea+springboot+mybatis+pagehelper分页+log4j_第7张图片

 

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

idea+springboot+mybatis+pagehelper分页+log4j_第8张图片

 

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

idea+springboot+mybatis+pagehelper分页+log4j_第9张图片

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_第10张图片

 

11.整合mybatis

 

1.添加maven依赖

idea+springboot+mybatis+pagehelper分页+log4j_第11张图片


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.0.1

2.在数据库新建user表

 
  

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

idea+springboot+mybatis+pagehelper分页+log4j_第12张图片

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_第13张图片

 

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

idea+springboot+mybatis+pagehelper分页+log4j_第14张图片

 

7.填写UserMapping.xml文件




    
        
        
        
        
    
    

8.填写UserMapper.java

idea+springboot+mybatis+pagehelper分页+log4j_第15张图片

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 getUserList();//这里的方法名和xml定义的要一致
}

 

9.在com.example.demo 下新建service包,在该包下新建UserService类idea+springboot+mybatis+pagehelper分页+log4j_第16张图片

idea+springboot+mybatis+pagehelper分页+log4j_第17张图片

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 getUserList(){
        return userMapper.getUserList();
    }
}

 

10.在DemoController 添加以下代码

idea+springboot+mybatis+pagehelper分页+log4j_第18张图片

 

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 getUserList(){
        return userService.getUserList();
    }
}

 

11.在postman进行测试

idea+springboot+mybatis+pagehelper分页+log4j_第19张图片

 

12.以上springboot 整合mybatis完毕

 

13.使用pagehelper 进行分页

1.添加maven 依赖

idea+springboot+mybatis+pagehelper分页+log4j_第20张图片


    com.github.pagehelper
    pagehelper-spring-boot-starter
    1.2.10

2.在application.yml内配置pagehelper

idea+springboot+mybatis+pagehelper分页+log4j_第21张图片

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_第22张图片

 

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

 

4.在postman中测试

idea+springboot+mybatis+pagehelper分页+log4j_第23张图片

 

5.至此pagehelper 整合完成

 

整合log4j

 

1.添加maven依赖

idea+springboot+mybatis+pagehelper分页+log4j_第24张图片

 

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

idea+springboot+mybatis+pagehelper分页+log4j_第25张图片

idea+springboot+mybatis+pagehelper分页+log4j_第26张图片

 

3.在DemoController 中添加以下代码

idea+springboot+mybatis+pagehelper分页+log4j_第27张图片

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_第28张图片

 

 

 

你可能感兴趣的:(idea+springboot+mybatis+pagehelper分页+log4j)