0

spring配置

    <context:annotation-config/>
    <context:component-scan base-package="cn.summerwaves.*">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--引入配置文件-->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties"/>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
        <!--连接池配置-->
        <property name="initialSize" value="1"/>
        <property name="minIdle" value="1"/>
        <property name="maxActive" value="100"/>
        <property name="maxIdle" value="20"/>
        <property name="maxWait" value="1000"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:cn/summerwaves/mapper/*xml"></property>
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.summerwaves.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

Service接口

public interface IUserService {
    public void insertUser(User user) throws IOException;
}

Service实现

@Service("userService")
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    
    @Override
    public void insertUser(User user) throws IOException {
        userMapper.insertUserByAnnotation(user);
    }

}

测试类

@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class IUserServiceImplTest {

    @Resource
    private IUserService userService;

    @Test
    public void insertUser() throws Exception {

      
        User user = new User();
        user.setUsername("张三");
        user.setPassword("123456");
        user.setSex(1);
        userService.insertUser(user);
    }
}

抛出java.lang.NullPointerException空指针异常

但是改为

@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class IUserServiceImplTest {

    @Test
    public void insertUser() throws Exception {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mybatis.xml");
        UserServiceImpl userService = (UserServiceImpl) ctx.getBean("userService");
        User user = new User();
        user.setUsername("张三");
        user.setPassword("123456");
        user.setSex(1);
        userService.insertUser(user);
    }
}

可以正常连接数据库

并且

编辑器显示已注入,点左边那个小豆可以进入Service实现类

已经卡在这好几天了,搜索引擎前几页都翻过了,还是解决不了....

2个回答

0

已采纳


IUserServiceImplTest加上注解
@RunWith(SpringJUnit4ClassRunner.class)

0

简单瞟了一眼,没发现开启注解的语句,我记得用注解的话配置里面不是要开启注解的吗?
PS:不晓得是不是这两句,你再搜搜看好了

撰写答案