Mybatis+MyBatisGenerator+PageHelper集成

初始化一个web项目mybatis_test,pom文件如下所示:

pom.xml文件


	4.0.0

	com.haiyoung
	mybatis_test
	0.0.1-SNAPSHOT
	jar

	mybatis_test
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.2.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

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

		
			mysql
			mysql-connector-java
			6.0.6
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

application.yml配置文件
spring:
  #数据库配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/haiyoung?useSSL=true&serverTimezone=UTC
    username: root
    password: xxxxxx #thinkpad
#    password: xxxxxx #mint
server:
  port: 8080
  servlet:
    context-path: /mybatis_test
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  config-location: classpath:mybatis-config.xml
mybatis-config.xml配置文件




    
        
        
        
    

    
        
        
    

mybatis集成MyBatisGenerator

添加 mybatis-generator-core 依赖


    org.mybatis.generator
    mybatis-generator-core
    1.3.5
添加 mybatis-generator-maven-plugin 插件配置到pom文件中

    org.mybatis.generator
    mybatis-generator-maven-plugin
    1.3.5
    
    
        src/main/resources/mybatis_generator_config.xml
        true
        true
    
mybatis-generator配置文件 mybatis_generator_config.xml




    
    

    
        
            
            
        
        
        
        
        

        
            
        

        
        
            
            
        

        
        
            
        

        
        
            
        

        
        
            
        
运行mybatis-generator:generate自动生成mapper.xml文件及接口
Mybatis+MyBatisGenerator+PageHelper集成_第1张图片
生成的文件如下图所示:
Mybatis+MyBatisGenerator+PageHelper集成_第2张图片
经测试,生成文件自动关联,运行时也没有什么异常
向数据库中添加如下测试数据

Mybatis+MyBatisGenerator+PageHelper集成_第3张图片

测试其中一个根据id获取数据的接口如下所示:

@RequestMapping("/person")
public Person getPerson(HttpServletRequest request,
       @RequestParam(value = "id") Integer id){
    return personService.getPerson(id);
}

public Person getPerson(Integer id){
    return personMapper.selectByPrimaryKey(id);
}

当id=3时,返回结果如下图所示:

http://localhost:8080/mybatis_test/person?id=3

返回结果如下所示:

{
    "id": 3,
    "name": "xxx",
    "age": 18,
    "description": "xxxxx"
}
mybatis-generator配置成功,工作正常

mybatis集成PageHelper

pom文件中添加如下依赖


    com.github.pagehelper
    pagehelper
    5.1.4

mybatis-config.xml文件中添加pageHelper拦截器插件





    
        
        
        
    

    
        
        
    

    
    
        
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
    

新建一个Mapped URL如下所示:

@RequestMapping("/allPersons")
public List getPerson(HttpServletRequest request){
    return personService.getAllPersons();
}

@RequestMapping("/page/allPersons")
public PageInfo> getPersonByPage(HttpServletRequest request,
       @RequestParam(value = "pageNum") Integer pageNum,
       @RequestParam(value = "pageSize") Integer pageSize){
//        PageHelper.startPage(1, 5);
    PageHelper.startPage(pageNum, pageSize);
    //紧跟着的第一个select方法会被分页
    List persons = personService.getAllPersons();
    PageInfo page = new PageInfo(persons);
    return page;
}
我们测试一下:
首先我们获取所有数据:
http://localhost:8080/mybatis_test/allPersons
返回了所有数据:
[
    {
        "id": 1,
        "name": "Test111",
        "age": 27,
        "description": "Test111"
    },
    {
        "id": 2,
        "name": "Test222",
        "age": 25,
        "description": "Test222"
    },
    {
        "id": 3,
        "name": "xxx",
        "age": 18,
        "description": "xxxxx"
    },
    {
        "id": 4,
        "name": "xxx01",
        "age": 19,
        "description": "xxx01"
    },
    {
        "id": 5,
        "name": "xxx02",
        "age": 20,
        "description": "xxx02"
    },
    {
        "id": 6,
        "name": "xxx03",
        "age": 21,
        "description": "xxx03"
    },
    {
        "id": 7,
        "name": "xxx04",
        "age": 22,
        "description": "xxx04"
    },
    {
        "id": 8,
        "name": "xxx05",
        "age": 23,
        "description": "xxx05"
    }
]
我们再测试一下分页:
http://localhost:8080/mybatis_test/page/allPersons?pageNum=1&pageSize=5
返回第一页5行数据:
{
    "total": 8,
    "list": [
        {
            "id": 1,
            "name": "Test111",
            "age": 27,
            "description": "Test111"
        },
        {
            "id": 2,
            "name": "Test222",
            "age": 25,
            "description": "Test222"
        },
        {
            "id": 3,
            "name": "xxx",
            "age": 18,
            "description": "xxxxx"
        },
        {
            "id": 4,
            "name": "xxx01",
            "age": 19,
            "description": "xxx01"
        },
        {
            "id": 5,
            "name": "xxx02",
            "age": 20,
            "description": "xxx02"
        }
    ],
    "pageNum": 1,
    "pageSize": 5,
    "size": 5,
    "startRow": 1,
    "endRow": 5,
    "pages": 2,
    "prePage": 0,
    "nextPage": 2,
    "isFirstPage": true,
    "isLastPage": false,
    "hasPreviousPage": false,
    "hasNextPage": true,
    "navigatePages": 8,
    "navigatepageNums": [
        1,
        2
    ],
    "navigateFirstPage": 1,
    "navigateLastPage": 2,
    "firstPage": 1,
    "lastPage": 2
}
我们修改一下参数,获取第二页数据:
http://localhost:8080/mybatis_test/page/allPersons?pageNum=2&pageSize=5
返回第二页数据如下所示:
{
    "total": 8,
    "list": [
        {
            "id": 6,
            "name": "xxx03",
            "age": 21,
            "description": "xxx03"
        },
        {
            "id": 7,
            "name": "xxx04",
            "age": 22,
            "description": "xxx04"
        },
        {
            "id": 8,
            "name": "xxx05",
            "age": 23,
            "description": "xxx05"
        }
    ],
    "pageNum": 2,
    "pageSize": 5,
    "size": 3,
    "startRow": 6,
    "endRow": 8,
    "pages": 2,
    "prePage": 1,
    "nextPage": 0,
    "isFirstPage": false,
    "isLastPage": true,
    "hasPreviousPage": true,
    "hasNextPage": false,
    "navigatePages": 8,
    "navigatepageNums": [
        1,
        2
    ],
    "navigateFirstPage": 1,
    "navigateLastPage": 2,
    "firstPage": 1,
    "lastPage": 2
}
至此,我们成功集成了Mybatis+MyBatisGenerator+PageHelper,且能正常工作。
源代码地址: https://github.com/Haiyoung/HyProject/tree/master/mybatis_test

你可能感兴趣的:(Spring,springboot,database,mybatis)