SpringBoot配置Druid数据源,持久层分别 mybatis,jdbc
Druid与mybatis整合:
application.yaml 配置参数文件
spring:datasource:#driver-class-name: com.mysql.jdbc.Driverdriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/activiti10unit?characterEncoding=utf-8&serverTimezone=GMTusername: rootpassword: root#告知springboot 使用的链接池类型是druidtype: com.alibaba.druid.pool.DruidDataSourceinitialSize: 20maxActive: 30minIdle: 10userSSL: false
提示:
mysql的connecto 驱动 jar 从5.1.33-5.1.37 的TimeUtil类存在bug;
在连接的url后加一个参数 serverTimezone=GMT ,这里的时区可以根据自己数据库的设定来设置,
mysql新的安全性设置要求SSL连接,此处可以加一个参数userSSL=false,或者自己设置SSL也可以
另外 6.0.2版本的driverClassName不再是原来的路径,改成com.mysql.cj.jdbc.Driver了;
否则会报以下错误:
WARNING: Unexpected exception resolving reference
java.sql.SQLException: The server timezone value '◇□' is unrecognized or represents more than one timezone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc timezone value if you want to utilize timezone support.
com.example.mybatis2018.config.DruidConfig 配置参数类,有些参数必须自己配置加载进容器:
package com.example.mybatis2018.config;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class DruidConfig {@ConfigurationProperties(prefix = "spring.datasource")@Beanpublic DruidDataSource druidDataSource(){return new DruidDataSource();}
}
使用mybatis持久层,写mapper接口
com.example.mybatis2018.mapper.UserMapper
主键版:
package com.example.mybatis2018.mapper;import com.example.mybatis2018.pojo.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;/*** 使用@Mapper主键 来标注这是一个Mapper接口*/
@Mapper
public interface UserMapper {@Select("select * from user where id = #{id}")User selectUserbyId(Long id);/*** 自增主键 select last_insert_id()* 非 自增主键 select uuid() before:true* @param user* @return*///自增主键@SelectKey(keyProperty = "id",keyColumn = "id",statement = "select last_insert_id()" ,before=false,resultType = Long.class)@Insert("insert into user (USER_NAEM,USER_PASSWORD) values(#{USER_NAEM},#{USER_PASSWORD})")int insertUser(User user);@Delete("delete from user where id = #{id}")int deleteUserById(Long id);@Update("update user set USER_NAEM=#{USER_NAEM},USER_PASSWORD = #{USER_PASSWORD} where id = #{id}")int updateUser(User user);
}
---------------------------------------------------------------------------------------------------------------
Druid与jdbc整合:
因为starter-jdbc中自带HikariCp连接池,需要剔除此链接池,后再加入Druid连接池依赖,后续配置参照上面与mybatis整合的配置一致;
剔除原有连接池依赖
加入Druid连接池依赖
后面配置参照与mybatis整合的配置
SpringBoot配置Druid数据源,持久层分别 mybatis,jdbc相关推荐
- springboot配置Druid数据源
springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...
- springboot2.5.5配置druid数据源1.2.8与jdbc
[README] 本文记录了 springboot配置 druid数据源的步骤: [1]新建springboot项目并配置druid 步骤1,新建springbt项目 步骤2,选择spring web ...
- Springboot配置Druid数据源详解
配置数据源 1.添加Druid依赖 <!-- druid数据源--> <!-- https://mvnrepository.com/artifact/com.alibaba/drui ...
- springboot 配置DRUID数据源
druid 是阿里开源的数据库连接池. 开发时整合 druid 数据源过程. 1.修改pom.xml <dependency><groupId>mysql</grou ...
- SpringBoot(配置druid数据源、配置MyBatis、事务控制、druid 监控)
SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少 数据层操作,所有的开发都一定秉持着 MVC 设计模式的原则,MVC 里面业务层不可少 ...
- mybatis generator 中文注释_[SpringBoot2.X] 23- 整合持久层技术 -MyBatis - 配置
整合MyBatis 1搭建项目环境 1.1.1创建项目--略 11.2修改POM文件,添加相关依赖 <dependency><groupId>org.springframewo ...
- 关于Springboot 配置多数据源 Druid 德鲁伊
关于Springboot 配置多数据源 Druid 德鲁伊 yml config yml server:port: 8080servlet:encoding:charset: UTF-8 spring ...
- springboot中druid数据源配置无效的问题和jar包找不到问题
springboot中druid数据源配置无效的问题 阿里云的仓库 链接: 阿里云仓库. 自己在springboot项目中,引入druid的依赖,希望引入druid数据源. 但是idea中,虽然在这个 ...
- SpringBoot项目拥抱Mybatis-Plus持久层框架实践
本文目录 前言 自从 Mybatis-Plus推出以来,越来越多的公司在自己的项目中选择Mybatis-Plus框架替换了持久层框架Mybatis.因为Mybatis-Plus用起来既有Mybatis ...
最新文章
- maven与eclipse连接的配置
- UVALive - 7511 Multiplication Table(暴力+模拟)
- 2021牛客暑期多校训练营3 C Minimum grid 网络流 + 二分图匹配
- 工作203:实现预览效果
- node+express创建第一个node项目
- Quay (1) - 用Quay Operator配置Quay环境
- 使用Web API和React创建用户注册和登录
- java怎样将日期本土化_Java中的日期操作
- linux开关机日志文件,Linux开关机命令(shutdown,reboot,halt,init)
- 闪迪内存卡软件测试,闪迪存储卡怎么样
- 大数据思维的十大核心原理
- 驱动程序解析及其对硬件和系统发挥效能的重大作用和注意事宜
- word文档页码不连续怎么弄
- 【Python】利用滑动窗口计算全基因组每个窗口上CNV的拷贝数和Vst
- ABB智能定位器TZID-C说明书(中文版)
- 龙族幻想导入数据id_龙族幻想:一键捏脸数据ID 快速捏脸数据ID汇总分享
- rx590 黑苹果 无货_应粉丝要求花9000配了一台高端黑苹果电脑,大家看看值不值吧!...
- 《中国文房四宝之宣纸》:是你未曾见过的东方之美
- 开源中国iOS客户端学习——(十二)用户登陆
- 由旧版本环信对接之后没有声音,探索AVAudioSession的Category和Option