springboot之使用hikari操作Mysql

springboot 2.0 默认连接池就是Hikari了,所以引用parents后不用专门加依赖

pom.xml配置


    org.springframework.boot
    spring-boot-starter-data-jpa


    mysql
    mysql-connector-java
    8.0.16

 

application.properties

## 数据库配置
spring.datasource.url=jdbc:mysql://地址:端口/库名?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=UTC
spring.datasource.username=用户名
spring.datasource.password=密码
##  Hikari 连接池配置 ------ 详细配置请访问:https://github.com/brettwooldridge/HikariCP
## 最小空闲连接数量
spring.datasource.hikari.minimum-idle=5
## 空闲连接存活最大时间,默认600000(10分钟)
spring.datasource.hikari.idle-timeout=180000
## 连接池最大连接数,默认是10
spring.datasource.hikari.maximum-pool-size=10
## 此属性控制从池返回的连接的默认自动提交行为,默认值:true
spring.datasource.hikari.auto-commit=true
## 连接池母子
spring.datasource.hikari.pool-name=MyHikariCP
## 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
spring.datasource.hikari.max-lifetime=1800000
## 数据库连接超时时间,默认30秒,即30000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1
spring.jpa.properties.hibernate.hbm2ddl.auto=update
##解决驼峰型字段建表时自动转化为下划线的问题
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true

 

实体类

springboot之使用hikari操作Mysql_第1张图片

 

dao层

springboot之使用hikari操作Mysql_第2张图片

service层

springboot之使用hikari操作Mysql_第3张图片

action层

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xiudou.entity.SysGroup;
import xiudou.service.SysGroupService;

import java.util.Date;

@RestController
@Slf4j
@RequestMapping("/sysGroup")
public class SysGroupAction {

  @Autowired SysGroupService sysGroupService;

  @RequestMapping("/add")
  public String add() {
    SysGroup bean = new SysGroup();
    bean.setName("技术组");
    bean.setCreateTime(new Date());
    System.out.println(sysGroupService.addOrUpdate(bean));
    return "新增用户组";
  }

  @RequestMapping("/find")
  public String find(SysGroup bean) {
    int id = bean.getId();
    bean = sysGroupService.find(id);
    return bean == null ? "查无讯息" : bean.getName();
  }

  @RequestMapping("/update")
  public String update(SysGroup bean) {
    bean.setCreateTime(new Date());
    bean.setName("技术组2");
    sysGroupService.addOrUpdate(bean);
    return "更新用户组";
  }

  @RequestMapping("/delete")
  public String delete(SysGroup bean) {
    int id = bean.getId();
    bean = sysGroupService.find(id);
    sysGroupService.delete(bean);
    return "删除用户组";
  }
}

 

你可能感兴趣的:(springboot之使用hikari操作Mysql)