SpringBoot+MyBatis 簡單分頁功能
阿新 • • 發佈:2019-02-03
專案結構:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mybatis</groupId> <artifactId>springboot-maybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-maybatis</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <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-web</artifactId> <version>2.0.5.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.22</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
實體類:
用了lombok,不需要再寫get、set、tostring方法了,直接用註解即可,程式碼也更加清晰。
package com.mybatis.springbootmaybatis.pojo; import lombok.Getter; import lombok.Setter; import lombok.ToString; /** * @author: Lucifer * @create: 2018-10-21 16:52 * @description: **/ @Getter @Setter @ToString public class teacher { private Integer id; private String name; }
UserService:
package com.mybatis.springbootmaybatis.service;
import com.github.pagehelper.PageInfo;
public interface UserService {
PageInfo selectByPage(Integer pageNum, Integer pageSize);
}
UserServiceImp:
package com.mybatis.springbootmaybatis.service; import com.github.pagehelper.PageInfo; import com.mybatis.springbootmaybatis.mapper.UserMapper; import com.mybatis.springbootmaybatis.pojo.teacher; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @author: Lucifer * @create: 2018-10-21 17:50 * @description: **/ @Service public class UserServiceimp implements UserService { @Autowired UserMapper userMapper; @Override public PageInfo selectByPage(Integer pageNum, Integer pageSize) { List<teacher> userList=userMapper.selectByPage(pageNum-1,pageSize); // 用PageInfo對結果進行包裝 PageInfo pageInfo = new PageInfo(userList); return pageInfo; } }
UserController:
package com.mybatis.springbootmaybatis.controller;
import com.github.pagehelper.PageInfo;
import com.mybatis.springbootmaybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: Lucifer
* @create: 2018-10-21 16:52
* @description:
**/
@RequestMapping("/user")
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping("/findByPage/{pageNum}/{pageSize}")
public PageInfo findByPageList(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
System.out.println("pageNum:"+pageNum);
System.out.println("pageSize:"+pageSize);
PageInfo pageInfo=userService.selectByPage(pageNum,pageSize);
return pageInfo;
}
}
啟動類:
@MapperScan註解加在啟動類上,就不需要在每個mapper介面中加上@mapper註解了。
package com.mybatis.springbootmaybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.mybatis.springbootmaybatis.mapper")
public class SpringbootMaybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMaybatisApplication.class, args);
}
}
application.yml:
springboot的全域性配置檔案:配置資料庫連線資訊,本demo可以不用開啟駝峰命名規則,另外指定了mapper檔案的位置
#資料庫連線配置#
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test
driver-class-name: com.mysql.jdbc.Driver
#開啟駝峰命名、指定mapper檔案位置#
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:mybatis/*.xml
#控制檯列印sql語句,指定自己的mapper介面路徑#
logging:
level:
com.mybatis.springbootmaybatis.mapper: debug
mapper.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.mybatis.springbootmaybatis.mapper.UserMapper">
<select id="selectByPage" resultType="com.mybatis.springbootmaybatis.pojo.teacher">
select *
from teacher limit
#{pageNum},
#{pageSize}
</select>
</mapper>
這裡用的是postman測試請求:如圖: