版权声明:此博客为个人博客,不涉及商业用途,仅提供学习参考,内容均来自个人原创以及互联网转载和摘录。 --------------------- 本文来自 路西法Lucifer 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/qq_37495786/article/details/83243869
项目结构:
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测试请求:如图: