欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot单元测试类的使用.Loading class `com.mysql.jdbc.Driver'. This is deprecated.

程序员文章站 2024-03-14 16:57:28
...

以前都是直接连接数据库的,现在我们要搞到单元测试里玩一玩 就是junit这玩意

先说说 我遇到的坑

1.Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.

 把properties的spring.datasource.driver-class-name=com.mysql.jdbc.Driver

改为 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

 

2.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.
这个需要设置一下mysql的时区

在mysql client中输入 set global time_zone='+8:00';即可成功

 

首先,我们先写一下单元测试的代码 一些注释都在里面了

package com.user.pack;

import javax.annotation.Resource;


import com.user.pack.domain.AyUser;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Newfile1ApplicationTests {

	@Test
	public void contextLoads() {
	}
	@Resource
	private JdbcTemplate jdbcTemplate;//实例化JdbcTemplate 其实就是建个JdbcTemplate的对象啦由@resource自动实例化 省去复杂的过程

	@Test
	public void mySqlTest()
	{
		String sql="select userid,user,password from springboot";//标准查询语句 加上下面的rowmapper的传入参数就可以进行查询操作
		List<AyUser> userList=(List<AyUser>) jdbcTemplate.query(sql,new RowMapper<AyUser>(){
			@Override
			public AyUser mapRow(ResultSet rs,int rowNum) throws SQLException {
				AyUser user = new AyUser();
				user.setUserid(rs.getString("userid"));		
				user.setUser(rs.getString("user"));
				user.setPassword(rs.getString("password"));
				return user;//返回一个bean
			}
		});
		System.out.println("query accomplish:");
		for(AyUser user:userList)
		{
			System.out.println("[userid]:"+user.getUserid()+"[name:]"+user.getUser());
		}
	}
}

然后设置一下 properties

spring.datasource.url=jdbc:mysql://127.0.0.1:3309/bilibili
spring.datasource.username=root
spring.datasource.password=123457
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

这是相关依赖

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
</dependencies>

spring boot单元测试类的使用.Loading class `com.mysql.jdbc.Driver'. This is deprecated.