测试的时候全部抛异常NullPointerException了
来源:3-9 持久层实现——SelectionDAO
wacky1
2020-08-07 18:08:42
spring.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/selection_course?useUnicode=true&characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <context:component-scan base-package="com.imooc.sc"/> </beans>
SelectionDao
package com.imooc.sc.dao; import com.imooc.sc.entity.Selection; import java.util.List; import java.util.Map; public interface SelectionDao { void insert(List<Selection> seles); void delete(int sid,int cid); List<Map<String,Object>> selectByStudent(int sid); List<Map<String,Object>> selectByCourse(int cid); }
SelectionDaoImpl
package com.imooc.sc.dao.impl; import com.imooc.sc.dao.StudentDao; import com.imooc.sc.entity.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.Map; @Repository public class StudentDaoImpl implements StudentDao { @Autowired private JdbcTemplate jdbcTemplate; public void insert(Student stu) { String sql = "insert into student(name,sex,born) values(?,?,?)"; jdbcTemplate.update(sql,stu.getName(),stu.getSex(),stu.getBorn()); } public void update(Student stu) { String sql = "update student set name = ?,sex = ?,born = ? where id = ?"; jdbcTemplate.update(sql,stu.getName(),stu.getSex(),stu.getBorn(),stu.getId()); } public void delete(int id) { String sql = "delete from student where id = ?"; jdbcTemplate.update(sql,id); } public Student select(int id) { String sql = "select * from student where id = ?"; Student student = jdbcTemplate.queryForObject(sql,new StudentRowMapper(),id); return student; } public List<Student> selectAll() { String sql = "select * from student"; List<Student> list = jdbcTemplate.query(sql,new StudentRowMapper()); return list; } private class StudentRowMapper implements RowMapper<Student>{ public Student mapRow(ResultSet resultSet, int i) throws SQLException { Student stu = new Student(); stu.setId(resultSet.getInt("id")); stu.setName(resultSet.getString("name")); stu.setSex(resultSet.getString("sex")); stu.setBorn(resultSet.getDate("born")); return stu; } } }
Test类代码
@org.junit.Test public void test(){ SelectionDao selectionDao = new SelectionDaoImpl(); List<Map<String,Object>> list = selectionDao.selectByStudent(1); System.out.println(list); }
报错代码
3回答
同学你好,这里的selectionDao对象应该使用@Autowired注入,而不是new一个SelectionDaoImpl对象,如:
如果是new对象,spring中的JdbcTemplate对象无法注入到类中,方法中使用jdbcTemplate会报空指针异常。同学修改一下再试试。
祝:学习愉快~
wacky1
提问者
2020-08-07
测试类代码
import com.imooc.sc.dao.SelectionDao; import com.imooc.sc.dao.StudentDao; import com.imooc.sc.dao.impl.SelectionDaoImpl; import com.imooc.sc.dao.impl.StudentDaoImpl; import com.imooc.sc.entity.Student; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring.xml") public class Test { private JdbcTemplate jdbcTemplate; { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate"); } //创建表 @org.junit.Test public void textExecute(){ jdbcTemplate.execute("create table user1(id int,name varchar(20))"); } //增删改 @org.junit.Test public void testUpdate(){ String sql = "insert into student(name,sex) values (?,?)"; jdbcTemplate.update(sql,new Object[]{"张飞","男"}); } @org.junit.Test public void testUpdate2(){ String sql = "update student set sex=? where id=?"; jdbcTemplate.update(sql,new Object[]{"女",1}); } //批量增删改 @org.junit.Test public void testBatchUpdate(){ String[] sqls = { "insert into student(name,sex) values('关羽','男')", "insert into student(name,sex) values('刘备','男')", "update student set sex='男' where id = 1" }; jdbcTemplate.batchUpdate(sqls); } @org.junit.Test public void testBatchUpdate2(){ String sql = "insert into selection(student,course) values(?,?)"; List<Object[]> list = new ArrayList<Object[]>(); list.add(new Object[]{1,1003}); list.add(new Object[]{1,1001}); jdbcTemplate.batchUpdate(sql,list); } //查询简单数据项 @org.junit.Test public void testQuerySimple1(){ String sql = "select count(*) from student"; int count = jdbcTemplate.queryForObject(sql,Integer.class); System.out.println(count); } //获取多个 @org.junit.Test public void testQuerySimple2(){ String sql = "select name from student where sex=?"; List<String> names = jdbcTemplate.queryForList(sql,String.class,"男"); System.out.println(names); } //查询复杂对象 @org.junit.Test public void testQueryMap1(){ String sql = "select * from student where id = ?"; Map stu = jdbcTemplate.queryForMap(sql,1); System.out.println(stu); } //查询多个 @org.junit.Test public void testQueryMap2(){ String sql = "select * from student"; List<Map<String,Object>> list = jdbcTemplate.queryForList(sql); System.out.println(list); } //查询复杂对象(封装为实体对象) @org.junit.Test public void testQueryEntity1(){ String sql = "select * from student where id = ?"; Student student = jdbcTemplate.queryForObject(sql, new StudentRowMap(),1); System.out.println(student); } //查询多个 @org.junit.Test public void TestQueryEntity2(){ String sql = "select * from student"; List<Student> list = jdbcTemplate.query(sql,new StudentRowMap()); System.out.println(list); } private class StudentRowMap implements RowMapper<Student>{ public Student mapRow(ResultSet resultSet, int i) throws SQLException { Student stu = new Student(); stu.setId(resultSet.getInt("id")); stu.setName(resultSet.getString("name")); stu.setSex(resultSet.getString("sex")); stu.setBorn(resultSet.getDate("born")); return stu; } } @org.junit.Test public void test(){ SelectionDao selectionDao = new SelectionDaoImpl(); List<Map<String,Object>> list = selectionDao.selectByStudent(1); System.out.println(list); } }
好帮手慕阿满
2020-08-07
同学你好,报错提示是空指针异常,出现在SelectionDaoImpl类的selectByStudent()方法的第41行,如:
第41行的代码如下:
也就是说jdbcTemplate对象为null。
在执行该方法时,没有加载spring.xml配置文件,所以jdbcTemplate为null,报空指针异常。
建议同学在测试类上,加上如下注解,加载配置文件再试试。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
祝:学习愉快~
相似问题