天天看點

SpringBoot 連接配接mysql踩到的坑

首先對于用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=11111

spring.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 Page

This 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 cause

java.lang.IllegalStateException: No DataSource set
  at 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 Page

This 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 cause

java.lang.NullPointerException: null
  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]
  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 {

  @Autowired
  private 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 {
    
    @Autowired
    private JdbcTemplate jdbcTemplate;      

@Component這樣的一個注解,這樣就可以了