首先对于用SpringBoot连接mysql我先说明一下pom文件中需要引入那些jar:

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>6.0.6</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId><version>2.0.3.RELEASE</version></dependency>

当然版本你可以按照自己的需要进行选择,,然后我们在来看配置文件application.properties:

spring.datasource.name =shopping
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/shopping?useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=11111spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

如果在启动的过程中提示这样的错误

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

那么你在配置application.properties里面:spring-datasource.driver-class-name=com.mysql.cj.jdbc.Driver

上面算是第一个坑

第二个坑可能是和我的的项目有关系:

前台提示这个错误:

Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.Sun Jul 29 09:41:17 CST 2018
There was an unexpected error (type=Internal Server Error, status=500).
No DataSource set

到Ecplise的控制台看:

2018-07-29 09:41:17.778 ERROR 4724 --- [nio-7890-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: No DataSource set] with root causejava.lang.IllegalStateException: No DataSource setat org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]at org.springframework.jdbc.support.JdbcAccessor.obtainDataSource(JdbcAccessor.java:77) ~[spring-jdbc-5.0.7.RELEASE.jar:5.0.7.RELEASE]at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:371) ~[spring-jdbc-5.0.7.RELEASE.jar:5.0.7.RELEASE]at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:446) ~[spring-jdbc-5.0.7.RELEASE.jar:5.0.7.RELEASE]at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456) ~[spring-jdbc-5.0.7.RELEASE.jar:5.0.7.RELEASE]at org.springframework.jdbc.core.JdbcTemplate.queryForList(JdbcTemplate.java:484) ~[spring-jdbc-5.0.7.RELEASE.jar:5.0.7.RELEASE]at com.wdg.main.util.CommonDao.findList(CommonDao.java:126) ~[classes/:na]at com.wdg.main.controller.HelloWorldController.home(HelloWorldController.java:26) ~[classes/:na]at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]

看样子是数据源没有配置,可是我上面在application.properties里面配置了啊,大家也看到了,那是怎么回事,重点在这一个语句:

private JdbcTemplate jdbcTemplate=new JdbcTemplate();

我在service里面用到的jdbc模板居然是new出来的,这样就遇到数据源没有设置

那么怎么修改:

@Autowired
    private JdbcTemplate jdbcTemplate;

上面算是遇到的第二个坑吧,现在想想自己真的挺傻的,居然用new的方式

那么我们来看看第三个坑,也是上面的我们改成为的注入之后,我们看看 前台报什么错误:

Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.Sun Jul 29 09:46:05 CST 2018
There was an unexpected error (type=Internal Server Error, status=500).
No message available

这个是前台给出的页面的提示,我们看看后台报什么错误:

018-07-29 09:49:32.509 ERROR 9792 --- [nio-7890-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root causejava.lang.NullPointerException: nullat com.wdg.main.util.CommonDao.findList(CommonDao.java:126) ~[classes/:na]at com.wdg.main.controller.HelloWorldController.home(HelloWorldController.java:26) ~[classes/:na]at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60]at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60]at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60]at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]

空指针的,就是我前面在自动注入的地方,看到吗,如果采用注入的方式会报空指针,这可如何是好,如果用new的方式提示没有设置数据源,如果采用注入的方式提示空指针,真的的是没法玩了。

别着急我们继续,我们上面说我们这个是输入service层,那么我们这个地方也应该采用注入的方式来引用,在Controller里面我们引用service,service也要进行注入才行

看看那我们在Controller里面怎么引用的service:

@RestController
public class HelloWorldController {@RequestMapping("/home")String home() {String sql = "select * from userinfo";List<UserInfo> list_u = new CommonDao().findList(sql, UserInfo.class);for (UserInfo user : list_u) {System.out.println(user.toString());}return "I love huanhuan";}}

看到了么CommonDao,用的是new,这个当然是不行的,怎么修改,我们要改正自动注入的方式:

修改为:

@RestController
public class HelloWorldController {@Autowiredprivate CommonDao dao;@RequestMapping("/home")String home() {String sql = "select * from userinfo";List<UserInfo> list_u = dao.findList(sql, UserInfo.class);for (UserInfo user : list_u) {System.out.println(user.toString());}return "I love huanhuan";}}

那么既然使用service使用注入用没有什么要求,当然是用的:

我们需要在service的类项目上面添加:

@Component
public class CommonDao {@Autowiredprivate JdbcTemplate jdbcTemplate;

@Component这样的一个注解,这样就可以了

上面是第2+1坑了,

然后再送你一个,“”静态方法在慎用“”

希望对你有所帮助

SpringBoot 连接mysql踩到的坑相关推荐

  1. SpringBoot连接mysql数据,写入数据

    (1)先准备好mysql数据,作为springboot的数据存储服务器. 安装和启动mysql服务器的介绍:https://zhangphil.blog.csdn.net/article/detail ...

  2. Springboot连接mysql数据库报错

    Springboot连接mysql数据库报错java.sql.SQLException: Access denied for user ''@'localhost' (using password: ...

  3. AndroidStudio通过JDBC连接MySQL数据库六大巨坑

    文章目录 注意 基础 Androidstudio通过JDBC连接数据库巨坑介绍(这里呢,我使用我所做项目的修改密码界面来做介绍) 1.网络权限问题(打不开apk) 2.jar包问题(找不到driver ...

  4. boot连接不上mysql数据库_【springboot连接 MYSQL数据库出问题_springboot】 | IT修真院·坑乎...

    Spring: datebase: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/spring-c ...

  5. 手把手教你在本地的vm虚拟机中搭建一个linux并建立连接(踩完所有坑)

    文章目录 前言 正文 虚拟机下载 linux虚拟机安装及版本安装. linux虚拟机安装 网络环境搭建 查看自己主机网段 配置linux环境下的ip地址 解决每次关闭防火墙问题 设置linux的网络适 ...

  6. SpringBoot连接MySql数据库报错:HikariPool-1 - Exception during pool initialization

    问题如下: 解决办法(可参考,每个人遇到的不一样,以下是我的解决办法): 1.检查你的密码账号是否正确,username可以通过新建一个连接来查询,密码是自己mysql设置的,忘记的可以改密码,网上自 ...

  7. 关于Win10安装keil并且电脑连接jlink踩过的坑

    1. 问题以及解决方案 安装Keil 插入jlink外设完成烧写程序在开发板实现对应功能. 周围大多数同学对照老师的教材都很顺利地进行下去了,而倒霉如我,几乎操作手册的每一步都精准踩坑.

  8. SpringBoot连接Mysql数据库遇到Unable to load authentication plugin ‘caching_sha2_password解决方案

    遇到问题 o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: S1009 o.h.engine.jdbc.spi.SqlE ...

  9. mysql 踩过的坑_MySQL数据库踩过的一些坑

    在使用MySQL期间的一些问题 1.下载地址:https://cdn.mysql.com//Downloads/MySQL-5.5/mysql-5.5.61-winx64.msi 2.配置文件: 打开 ...

最新文章

  1. 数据蒋堂 | 内置的数据无法实现高性能
  2. 书写神器——markdown
  3. android 无法显示SD卡目录,Android studio无法在SD卡上创建新目录?
  4. 国家电网和南方电网还傻傻分不清?
  5. html的悬浮框 贼简单 记录一下
  6. AndroidX 方法数限制 Error:Cannot fit requested classes in a single dex file 64K问题
  7. CES 2018 七大看点前瞻:模块化电视、枪型游戏设备……
  8. MySQL数据库常见存储引擎(一)
  9. SQL标准语句——思维导图
  10. 2022高教社杯数学建模思路 - 案例:核方法(机器学习)
  11. python+pytesseract本地pdf识别转文字,图片识别转文字,避坑大量识别转文字时的内存泄露问题解决
  12. Python如何进行语法检查
  13. 【RNAseq】差异分析
  14. java version 1.8下载_JDK1.8下载、安装和环境配置教程
  15. Node.js:时间处理库:moment.js用法
  16. 耐压测试仪结构组成部分
  17. 微信签到 表单 mysql_java做的一个简易的微信签到系统
  18. 3D坐标系、矩阵变换、视景体与裁剪
  19. 浅析SSL/TLS的会话流程和源码实现
  20. [Python]numpy数据分析练习[21~30]

热门文章

  1. struts2学习 - action -3 动态方法调用 DMI
  2. 网页挂马防护市场探索
  3. LINUX上MYSQL优化三板斧
  4. 某程序员揭秘“开水团”大厂真实福利:工位拥挤,没有食堂!公司防员工跟防贼一样,特别是纸巾和插排都粘到桌子上!...
  5. 我用分布式事务干掉了一摞简历
  6. 跟刘强东、雷军等大佬聊天后,我总结了:如何结交牛人,跟大咖做朋友!
  7. 一步一图,带你重头梳理微服务架构!
  8. 基于Spring Boot+Cloud构建微云架构
  9. 全球Top5互联网巨头崛起秘诀,真相竟然是?
  10. 敏捷开发流程的8个步骤