目录
1、POM文件
<!-- jdbc驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
2、YAML文件
spring:
#数据源配置
datasource:
druid:
url: jdbc:mysql://localhost:3306/t_shinb?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=Hongkong
username: root
password: shinb
driver-class-name: com.mysql.cj.jdbc.Driver
#连接池配置(通常来说,只需要修改initialSize、minIdle、maxActive
initial-size: 1
max-active: 20
min-idle: 1
# 配置获取连接等待超时的时间
max-wait: 60000
#打开PSCache,并且指定每个连接上PSCache的大小
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
validation-query: SELECT 'x'
test-on-borrow: false
test-on-return: false
test-while-idle: true
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
#配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 300000
filters: stat
filter:
wall:
config:
multi-statement-allow: true
# WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter
#是否启用StatFilter默认值true
web-stat-filter.enabled: false
web-stat-filter.url-pattern: /*
web-stat-filter.exclusions: "*.js , *.gif ,*.jpg ,*.png ,*.css ,*.ico , /druid/*"
web-stat-filter.session-stat-max-count: 1000
web-stat-filter.profile-enable: true
# StatViewServlet配置
#展示Druid的统计信息,StatViewServlet的用途包括:1.提供监控信息展示的html页面2.提供监控信息的JSON API
#是否启用StatViewServlet默认值true
stat-view-servlet.enabled: false
#根据配置中的url-pattern来访问内置监控页面,如果是上面的配置,内置监控页面的首页是/druid/index.html例如:
stat-view-servlet.url-pattern: /druid/*
#允许清空统计数据
stat-view-servlet.reset-enable: true
stat-view-servlet.login-username: admin
stat-view-servlet.login-password: admin
#StatViewSerlvet展示出来的监控信息比较敏感,是系统运行的内部情况,如果你需要做访问控制,可以配置allow和deny这两个参数
#deny优先于allow,如果在deny列表中,就算在allow列表中,也会被拒绝。如果allow没有配置或者为空,则允许所有访问
#配置的格式
#<IP>
#或者<IP>/<SUB_NET_MASK_size>其中128.0.0.1/24
#24表示,前面24位是子网掩码,比对的时候,前面24位相同就匹配,不支持IPV6。
#stat-view-servlet.allow=
#stat-view-servlet.deny=128.0.0.1/24,128.0.0.1
# Spring监控配置,说明请参考Druid Github Wiki,配置_Druid和Spring关联监控配置
#aop-patterns= # Spring监控AOP切入点,如x.y.z.service.*,配置多个英文逗号分隔
################### mysql end ##########################
#mybatis配置
mybatis:
mapper-locations: classpath:/mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
MyBatis-Spring-Boot-Starter配置参考
3、代码实现
User
package com.shinb.demo.model;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
public class User {
private Long id;
private String name;
private Integer age;
private Boolean married;
@JsonFormat(pattern="yyyy-MM-dd HH-mm-ss")
private Date birth;
//TODO Getters and Setters
}
UserMapper.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.shinb.demo.dao.IUserDao">
<resultMap id="BaseResultMap" type="com.shinb.demo.model.User">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="married" jdbcType="BIT" property="married" />
<result column="birth" jdbcType="TIMESTAMP" property="birth" />
</resultMap>
<select id="selectAll" resultMap="BaseResultMap">
select id,name,age,married,birth
from t_user
</select>
</mapper>
Dao
package com.shinb.demo.dao;
import com.shinb.demo.model.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface IUserDao {
List<User> selectAll();
}
Service
package com.shinb.demo.service;
import com.shinb.demo.model.User;
import java.util.List;
public interface IUserService {
List<User> selectAll();
}
package com.shinb.demo.service.impl;
import com.shinb.demo.dao.IUserDao;
import com.shinb.demo.model.User;
import com.shinb.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao iUserDao;
@Override
public List<User> selectAll() {
return iUserDao.selectAll();
}
}
Controller
package com.shinb.demo.controller;
import com.shinb.demo.model.User;
import com.shinb.demo.service.IUserService;
import com.shinb.demo.yaml.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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;
@RestController
@RequestMapping("/person")
public class MvcController {
@Autowired
private Person person;
@Autowired
private IUserService iUserService;
//Spring Boot(三) MVC
@GetMapping("/info")
public Person getPerson(@RequestParam String name){
person.setName(name);
return person;
}
//Spring Boot(四) Mybatis
@GetMapping("/list")
public List<User> getUser(){
return iUserService.selectAll();
}
}
Application
package com.shinb.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication 标注是一个SpringBoot应用
*/
@SpringBootApplication
@MapperScan("com.shinb.demo.dao") //Mybatis映射器扫描路径
public class SpringbootDemoApplication {
public static void main(String[] args) {
//启动Spring
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
代码结构
4、测试
curl http://localhost:8082/shinb/person/list
[
{
"id":1,
"name":"shinb",
"age":18,
"married":true,
"birth":"2021-12-15 16-00-00"
},
{
"id":2,
"name":"zhangsna",
"age":20,
"married":false,
"birth":"2021-12-02 16-00-00"
},
{
"id":3,
"name":"lisi",
"age":25,
"married":true,
"birth":"2021-12-03 07-47-32"
}
]