SpringBoot整合mybatis,mysql,pagehelper
首先看目录结构
添加maven依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.atguigu</groupId>
<artifactId>springboot_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.4.1</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- swagger框架 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.github.pagehelper</groupId>-->
<!-- <artifactId>pagehelper</artifactId>-->
<!-- <version>3.6.3</version>-->
<!-- </dependency>-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置application.yml
spring:
datasource:
druid:
username: root
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/miniprogramwx?useUnicode=true&characterEncoding=utf-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
initial-size: 5
min-idle: 5
max-active: 20
maxWait: 10000
testOnBorrow: true
testOnReturn: false
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
mybatis:
type-aliases-package: com.atguigu.springboot_demo.dao.entity
#mapper-locations: classpath:/mappers/*.xml
#config-location: classpath:mybatis-config.xml
#ssl证书文件名
#server:
# ssl:
# key-store:
# key-store-password:
# key-store-type:
Swagger2Config类
package com.atguigu.springboot_demo.config;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* @Author liuhaoqi
* @Date 2020/3/11 17:44
* @Version 1.0
*/
public class Swagger2Config {
/**
* 创建swagger ui的摘要
*
* @return
*/
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 扫描的class的包路径
.apis(RequestHandlerSelectors.basePackage("com.atguigu.springboot_demo.controller"))
// 只扫描类上有API注解的class
// .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
// 只扫描方法上有ApiOperation注解的方法
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
/**
* swagger ui的标题信息
*
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("标题")
.description("副标题")
.version("1.0")
.build();
}
}
MybatisConfig类 用来解析pagehelper
package com.atguigu.springboot_demo.config;
import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
/**
* @Author liuhaoqi
* @Date 2020/3/12 10:19
* @Version 1.0
*/
@Configuration
public class MybatisConfig {
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper=new PageHelper();
Properties p=new Properties();
// 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用
p.setProperty("offsetAsPageNum","true");
//设置为true时,使用RowBounds分页会进行count查询
p.setProperty("rowBoundsWithCount","true");
p.setProperty("reasonable","true");
pageHelper.setProperties(p);
return pageHelper;
}
}
controller
package com.atguigu.springboot_demo.controller;
import com.atguigu.springboot_demo.service.serviceImpl.AdminServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import javafx.beans.binding.ObjectExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/**
* @Author liuhaoqi
* @Date 2020/3/11 17:46
* @Version 1.0
*/
@Controller
@RequestMapping("/test")
@Api(value = "/test",description = "Operation about test" , tags = "测试管理")
public class TestController {
@Autowired
private AdminServiceImpl adminService;
private Logger logger= LoggerFactory.getLogger(TestController.class);
@ApiOperation(value = "测试方法一",httpMethod = "GET",notes = "每过几秒就纪录一次")
@ApiImplicitParam(name = "data", value = "返回的数据对象", dataType = "Object")
@GetMapping("/test01")
@ResponseBody
public String test() {
return "test success!";
}
@ApiOperation( httpMethod = "GET", notes = "每过几秒就纪录一次", value = "/findAll")
@ApiImplicitParam(name = "data", value = "返回的数据对象", dataType = "Object")
@GetMapping("/findAll")
@ResponseBody
public Object findAll(@RequestParam(value = "page",defaultValue = "1") int page,
@RequestParam(value = "size",defaultValue = "10") int size) {
return adminService.findAll(page,size);
}
}
service
package com.atguigu.springboot_demo.service;
import com.atguigu.springboot_demo.dao.entity.AdminDao;
import java.util.List;
/**
* @Author liuhaoqi
* @Date 2020/3/12 10:22
* @Version 1.0
*/
public interface AdminService {
public Object findAll(int page,int size);
}
serviceImpl
package com.atguigu.springboot_demo.service.serviceImpl;
import com.atguigu.springboot_demo.dao.entity.AdminDao;
import com.atguigu.springboot_demo.dao.mapper.AdminMapper;
import com.atguigu.springboot_demo.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Author liuhaoqi
* @Date 2020/3/12 10:23
* @Version 1.0
*/
@Service
public class AdminServiceImpl implements AdminService {
@Autowired
private AdminMapper adminMapper;
public Object findAll(int page,int size) {
PageHelper.startPage(page,size);
List<AdminDao> adminDaoList=adminMapper.findAll();
PageInfo<AdminDao> pageInfo=new PageInfo<>(adminDaoList);
return pageInfo;
}
}
dao Mapper.java
package com.atguigu.springboot_demo.dao.mapper;
import com.atguigu.springboot_demo.dao.entity.AdminDao;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @Author liuhaoqi
* @Date 2020/3/12 10:25
* @Version 1.0
*/
@Component
@Mapper
public interface AdminMapper {
List<AdminDao> findAll();
}
实体类
package com.atguigu.springboot_demo.dao.entity;
import lombok.Data;
/**
* @Author liuhaoqi
* @Date 2020/3/12 10:44
* @Version 1.0
*/
@Data
public class AdminDao {
private Integer admin_id;
private String account;
private String password;
}
springboot启动类
package com.atguigu.springboot_demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@SpringBootApplication
@MapperScan("com.atguigu.springboot_demo.dao.mapper")
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
mapper.xml映射文件配置
这个文件一定要和mapper.java文件在同一个目录下,比如放在resources资源目录下,那么它的文件文件路径就一定要在java包下的Mapper.java文件路径相同,建立相同的目录结构。否则就会报没有绑定成功的错!!!
<?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.atguigu.springboot_demo.dao.mapper.AdminMapper">
<select id="findAll" resultType="com.atguigu.springboot_demo.dao.entity.AdminDao">
SELECT * FROM `admin`
</select>
</mapper>
数据库:
测试: