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这样的一个注解,这样就可以了

上面是第2+1坑了,

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

希望对你有所帮助

猜你喜欢

转载自blog.csdn.net/datouniao1/article/details/81267951