简介
今天的主要任务就是整合mybatis、Druid连接池、PageHelper分页
第二天的内容主要是整合mybatis、Druid连接池、PageHelper分页。
简单来说就是做一些配置。
之前创建项目时,导入了web依赖,现在我们要导入其他的依赖来丰富我们的功能。
注意是在pom文件下哦
<!-- mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- druid 数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!-- pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
注:有时输入正确,但 pom.xml 文件仍然报错,此时考虑依赖并未下载完全,可使用Maven的Reimport功能重新导入即可。重新下载一下就好了。
然后配置 application-dev.properties 文件,在该配置文件内配置以下内容,当然这里也也可以用yml配置,注意空格错行就行了(有需要的话以后会单独出一篇介绍使用方法)。
# 开发环境配置文件
# Druid
# 数据连接池的类型
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 数据连接池的名称
spring.datasource.name=druid_datasource
# 数据库驱动、url、用户名和密码
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.druid.username=root
spring.datasource.druid.password=password
# 配置连接池初始化大小、最大值、最小值
spring.datasource.druid.initial-size=20
spring.datasource.druid.max-active=40
spring.datasource.druid.min-idle=1
# 获取连接等待超时时间
spring.datasource.druid.max-wait=60000
# 间隔多久进行一次检测,检测需要关闭的空闲连接
spring.datasource.druid.time-between-eviction-runs-millis=60000
# 一个连接在池中最小生存的时间
spring.datasource.druid.min-evictable-idle-time-millis=300000
# 检测连接是否有效
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
spring.datasource.druid.test-while-idle= true
spring.datasource.druid.test-on-borrow= false
spring.datasource.druid.test-on-return= false
# Mybatis
# mapper映射xml文件的所在路径
mybatis.mapper-locations=classpath*:mapper/*.xml
# 对应实体类的路径
mybatis.type-aliases-package=com.example.backend_template.entity
# 开启驼峰命名
mybatis.configuration.map-underscore-to-camel-case=true
# Pagehelper
# 指定数据库
pagehelper.helperDialect=mysql
# 是否支持接口参数来传递分页参数
pagehelper.supportMethodsArguments=true
配置 application.properties 文件,在该配置文件内配置以下内容,这段话的意思就是让这个配置文件生效。
spring.profiles.active=dev
其实现在我们就可以测试数据库连接了
首先创建数据库表,添加数据
CREATE DATABASE test;
use test;
CREATE TABLE `tb_user` (
`id` int(11) NOT NULL,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `tb_user` VALUES ('1', 'AA', '112233');
INSERT INTO `tb_user` VALUES ('2', 'BB', '123456');
INSERT INTO `tb_user` VALUES ('3', 'CC', '123456');
INSERT INTO `tb_user` VALUES ('4', 'DD', '123456');
INSERT INTO `tb_user` VALUES ('5', 'EE', '123456');
INSERT INTO `tb_user` VALUES ('6', 'FF', '123456');
然后在com.example.backend_template.entity下新建实体类User。
package com.example.backend_template.entity;
import java.io.Serializable;
/**
* @ClassName User
* @Description
* @Author L
* @Date Create by 2020/6/25
*/
public class User implements Serializable {
private Long id;//编号
private String username;//用户名
private String password;//密码
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
在com.example.backend_template.dao下新建UserDao类,需要使用@Mapper注解,不然SpringBoot无法扫描,我感觉这里也可以使用@Repository(感觉以后也需要更新一篇关于spring boot注解的文章)
package com.example.backend_template.dao;
import com.example.backend_template.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @ClassName UserDao
* @Description
* @Author L
* @Date Create by 2020/6/25
*/
@Mapper//指定这是一个操作数据库的mapper
public interface UserDao {
List<User> findAll();
}
然后在resources.mapper下创建UserDao.xml
namespace中需要与使用@Mapper的接口对应
UserDao.xml文件名称必须与使用@Mapper的接口一致
标签中的id必须与@Mapper的接口中的方法名一致,且参数一致。
<?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.example.backend_template.dao.UserDao">
<select id="findAll" resultType="com.example.backend_template.entity.User">
SELECT * FROM tb_user
</select>
</mapper>
在com.example.backend_template.service下创建UserService接口
package com.example.backend_template.service;
import com.example.backend_template.entity.User;
import java.util.List;
/**
* @ClassName UserService
* @Description
* @Author L
* @Date Create by 2020/6/25
*/
public interface UserService {
List<User> findAll();
List<User> findAll(Integer page,Integer size);
}
在com.example.backend_template.service.impl下创建UserServiceImpl实现类
需要在接口实现类中使用@Service注解,才能被SpringBoot扫描,在Controller中使用@Authwired注入(@Recourse也可以,这里注解还有一点小区别哦,知道的可以写在评论区)
package com.example.backend_template.service.impl;
import com.example.backend_template.dao.UserDao;
import com.example.backend_template.entity.User;
import com.example.backend_template.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @ClassName UserServiceImpl
* @Description
* @Author L
* @Date Create by 2020/6/25
*/
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public List<User> findAll(Integer page, Integer size){
//开启分页查询,只有紧跟的第一个查询有效
//第一个参数为查询第几页,第二个为页面大小
PageHelper.startPage(page,size);
List<User> userList = userDao.findAll();
return userList;
}
}
最后在com.example.backend_template.controller下创建UserController类
package com.example.backend_template.controller;
import com.example.backend_template.entity.User;
import com.example.backend_template.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @ClassName UserController
* @Description
* @Author L
* @Date Create by 2020/7/3
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findAll")
public List<User> findAll(){
return userService.findAll();
}
@RequestMapping("/find")
public List<User> findAll(@RequestParam(defaultValue = "0") Integer page,
@RequestParam(defaultValue = "0") Integer size) {
return userService.findAll(page, size);
}
}
在启动类中添加对@MapperScan的扫描(很重要)
package com.example.backend_template;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.backend_template.dao")//使用MapperScan批量扫描所有的Mapper接口;
public class BackendTemplateApplication {
public static void main(String[] args) {
SpringApplication.run(BackendTemplateApplication.class, args);
}
}
可以看效果了
启动springboot ,不分页情况下,访问 http://localhost:8080/user/findAll ,结果如下
分页情况下,访问http://localhost:8080/user/find?page=2&size=2 ,结果如下
如出现以上两种结果则到表明框架整合成功。测试完后,就可以把从测试开始新建的文件和注解删除了,之后会新加其它的文件,但这里数据库暂时不要删除,之后换数据库的时候再删也不迟。