SpringBoot的Web开发入门案例9—数据访问

  1. 创建一个springboot项目(打包方式为jar包):

  2. 勾选Spring Web选项,勾选JDBC APIMySQL Driver

  3. pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>springboot_db</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot_db</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
  1. application.properties
server.port=8088
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  1. 写测试方法 SpringbootDbApplicationTests:
package com.example.demo;import java.sql.SQLException;import javax.sql.DataSource;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class SpringbootDbApplicationTests {@Autowiredprivate DataSource ds;@Testpublic void jdbcTest() throws SQLException {System.out.println(ds.getConnection());System.out.println(ds.getClass().getName());}
}
  1. 运行 jdbcTest() ,报错:
java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone.
You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. 
  1. 修改application.properties文件:
server.port=8088
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  1. 再次运行jdbcTest(), 成功,控制台打印:
HikariProxyConnection@797224183 wrapping com.mysql.cj.jdbc.ConnectionImpl@291373d3
com.zaxxer.hikari.HikariDataSource

所以SpringBoot的默认数据源是:com.zaxxer.hikari.HikariDataSource

  1. 修改测试方法 SpringbootDbApplicationTests:
package com.example.demo;import java.sql.SQLException;
import java.util.List;
import java.util.Map;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;@SpringBootTest
class SpringbootDbApplicationTests {@Autowiredprivate JdbcTemplate jdbcTemplate;@Testpublic void jdbcTest() throws SQLException {String sql = "select * from user";List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);System.out.println(list);}
}
  1. test数据库中的user表:
  2. 运行jdbcTest测试方法,控制台打印:
[{id=1, first_name=BLU, last_name=BLU}, {id=2, first_name=BLU1, last_name=spring}, {id=3, first_name=BLU2, last_name=mybatis}]

切换数据源druid

  1. 加入druid依赖:
<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.20</version>
</dependency>
  1. 修改application.properties文件(切换数据源为:com.alibaba.druid.pool.DruidDataSource):
server.port=8088
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

SpringBoot的Web开发入门案例9—数据访问相关推荐

  1. SpringBoot的Web开发入门案例1

    SpringBoot的Web开发入门案例1-登录和页面数据遍历读取 新建maven项目:logintest pom.xml文件: <project xmlns="http://mave ...

  2. SpringBoot的Web开发入门案例3—异常处理

    SpringBoot的Web开发入门案例3-异常处理 SpringBoot 默认404界面(由org.springframework.boot.autoconfigure.web.ErrorMvcAu ...

  3. SpringBoot的Web开发入门案例2—国际化

    SpringBoot的Web开发入门案例2-国际化 改造logintest项目:SpringBoot的Web开发入门案例1 地址:https://blog.csdn.net/BLU_111/artic ...

  4. SpringBoot的Web开发入门案例7—WebMvcConfigurer配置类

    SpringBoot的Web开发入门案例7-WebMvcConfigurer配置类 WebMvcConfigurer接口的几个常用方法: addViewControllers:配置请求路径和页面的映射 ...

  5. SpringBoot的Web开发入门案例6—替换默认容器Tomcat

    SpringBoot的Web开发入门案例6-替换默认容器Tomcat为Jetty Spring Boot默认是使用Tomcat作为内嵌的Servlet容器的,如需修改为Jetty,只要修改pom文件即 ...

  6. SpringBoot的Web开发入门案例5—注册Servlets, Filter, Listener

    SpringBoot的Web开发入门案例5-注册Servlets, Filter, Listener 注册Servlet 创建MyServlet类 package com.blu.conf;impor ...

  7. SpringBoot的Web开发入门案例8—支持jsp

    新建springboot工程:springboot_jsp,打包方式为war 导入web模块: 生成的项目结构: 包含启动类SpringbootJspApplication: package com. ...

  8. 002 第一季SpringBoot2核心技术-核心功能:配置文件、Web开发(原生组件)、数据访问、单元测试、指标监控、原理解析:@Value、命令行参数、手动获取bean、自定义starter

    三.核心技术之- ->核心功能 1. 配置文件 1.1 文件类型 1.1.1 properties 同以前的properties用法 优先级高于yml的方式. 1.1.2 yaml 1) 简介 ...

  9. AOP配置开发入门案例

    该AOP开发入门案例采用XML文件方式配置开发(非注解方式)共包含一个xml文件和4个Java类,创建好web工程后引入相应jar包(文末会给出),建好包(若自定义包名注意更改类中的包名),将xml文 ...

最新文章

  1. [TT]-Trustonic acronyms
  2. php 进行http请求,PHP模拟http请求的方法详解
  3. 安装Cornerstone3.1注意点
  4. 数据科学与python——Pandas统计分析基础(数据堆叠+数据清洗)
  5. python文件例题_文件操作练习题
  6. Bootstrap响应式工具类
  7. [leetcode]Length of Last Word
  8. 微信公众账号开发入门准备
  9. masscan端口扫描
  10. qcloud_cos 的安装问题
  11. 高校邮箱账号盗用监控及钓鱼邮件检测-上海交通大学
  12. 【无标题】500- Filtered request failed.
  13. 如何修改SnipeIT的部分设置
  14. house-prices
  15. 初学者笔记(三):利用python列表做一个最简单的垃圾分类
  16. 酷家乐怎样把两个方案合并_两个地产科技的理工男联姻:他们都想做一家云端的Autodesk...
  17. 通俗易懂解释汉明码(附MATLAB实现代码)
  18. Ubuntu20.04 安装python3.7
  19. 卷积神经网络——vgg16网络及其python实现
  20. 基于PCL库的通过ICP匹配多幅点云方法

热门文章

  1. pcb入门之PCB封装自制
  2. profiler-RenderDoc使用之unity篇
  3. python关于excel格式刷_这些Excel学会了,你做账的效率将大大提高
  4. canvas实现画板功能——选择颜色、线条粗细、橡皮擦、撤销等功能
  5. 2 对局域网所需计算机的分析,校园局域网组建剖析.doc
  6. IPv6地址格式浅谈
  7. CORS跨域请求 预检通道未成功 状态码 404
  8. opencv-python 姿势估计
  9. 鸿蒙手机下载安装,鸿蒙系统手机安装包
  10. GPRS、CDMA 1X受关注 中国迈向2.5G