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相关推荐

  1. springboot配置Druid数据源

    springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...

  2. springboot2.5.5配置druid数据源1.2.8与jdbc

    [README] 本文记录了 springboot配置 druid数据源的步骤: [1]新建springboot项目并配置druid 步骤1,新建springbt项目 步骤2,选择spring web ...

  3. Springboot配置Druid数据源详解

    配置数据源 1.添加Druid依赖 <!-- druid数据源--> <!-- https://mvnrepository.com/artifact/com.alibaba/drui ...

  4. springboot 配置DRUID数据源

    druid 是阿里开源的数据库连接池. 开发时整合   druid 数据源过程. 1.修改pom.xml <dependency><groupId>mysql</grou ...

  5. SpringBoot(配置druid数据源、配置MyBatis、事务控制、druid 监控)

    SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少 数据层操作,所有的开发都一定秉持着 MVC 设计模式的原则,MVC 里面业务层不可少 ...

  6. mybatis generator 中文注释_[SpringBoot2.X] 23- 整合持久层技术 -MyBatis - 配置

    整合MyBatis 1搭建项目环境 1.1.1创建项目--略 11.2修改POM文件,添加相关依赖 <dependency><groupId>org.springframewo ...

  7. 关于Springboot 配置多数据源 Druid 德鲁伊

    关于Springboot 配置多数据源 Druid 德鲁伊 yml config yml server:port: 8080servlet:encoding:charset: UTF-8 spring ...

  8. springboot中druid数据源配置无效的问题和jar包找不到问题

    springboot中druid数据源配置无效的问题 阿里云的仓库 链接: 阿里云仓库. 自己在springboot项目中,引入druid的依赖,希望引入druid数据源. 但是idea中,虽然在这个 ...

  9. SpringBoot项目拥抱Mybatis-Plus持久层框架实践

    本文目录 前言 自从 Mybatis-Plus推出以来,越来越多的公司在自己的项目中选择Mybatis-Plus框架替换了持久层框架Mybatis.因为Mybatis-Plus用起来既有Mybatis ...

最新文章

  1. maven与eclipse连接的配置
  2. UVALive - 7511 Multiplication Table(暴力+模拟)
  3. 2021牛客暑期多校训练营3 C Minimum grid 网络流 + 二分图匹配
  4. 工作203:实现预览效果
  5. node+express创建第一个node项目
  6. Quay (1) - 用Quay Operator配置Quay环境
  7. 使用Web API和React创建用户注册和登录
  8. java怎样将日期本土化_Java中的日期操作
  9. linux开关机日志文件,Linux开关机命令(shutdown,reboot,halt,init)
  10. 闪迪内存卡软件测试,闪迪存储卡怎么样
  11. 大数据思维的十大核心原理
  12. 驱动程序解析及其对硬件和系统发挥效能的重大作用和注意事宜
  13. word文档页码不连续怎么弄
  14. 【Python】利用滑动窗口计算全基因组每个窗口上CNV的拷贝数和Vst
  15. ABB智能定位器TZID-C说明书(中文版)
  16. 龙族幻想导入数据id_龙族幻想:一键捏脸数据ID 快速捏脸数据ID汇总分享
  17. rx590 黑苹果 无货_应粉丝要求花9000配了一台高端黑苹果电脑,大家看看值不值吧!...
  18. 《中国文房四宝之宣纸》:是你未曾见过的东方之美
  19. 开源中国iOS客户端学习——(十二)用户登陆
  20. 由旧版本环信对接之后没有声音,探索AVAudioSession的Category和Option

热门文章

  1. ASP.NET 2.0构建动态导航的Web应用程序(TreeView和Menu )[转载]
  2. 关于数据库的一些基本操作语句
  3. 个人Blog小程序开发完毕
  4. 潭州教育-Python学习笔记@基本数据类型:元祖字典
  5. jQuery ZeroClipboard中Flash定位不准确的解决方案
  6. 利用IP地址查询接口来查询IP归属地
  7. JY游戏开发,案例之 《下到一百层》,欢迎大家品赏。
  8. MCTS顺利通过,下一步MCPD
  9. win 7系统连接WIFI
  10. 服务器来电后自动开机|xfs文进系统备份恢复linux下误删除的文件