第一次spring整合mybatis,用自动注入接口,报错空指针,不知到错哪里啦
项目结构是这样子的
```java
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
<bean id="datasouce" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}">property>
<property name="url" value="${jdbc.url}">property>
<property name="username" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
<bean id="sqlssionfactiry" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasouce">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper">property>
bean>
beans>
然后很简单的就是随便写了一个接口,并在mapper.xml里面写了查询代码
```java
package mapper;
public interface shixian {
public String select();
}
mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.shixian">
<select id="select">
select 'main' from t_lable where id=1
select>
mapper>
然后再测试是这样的
package test;
import mapper.shixian;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class tttt {
@Autowired
private shixian a;
@Test
public void ttttt(){
Object name=a.select();
System.out.println("name");
}
}
报错是这样的
你这单元测试类上 要开启 spring的测试框架,才能注入这个接口类
依赖导入了没