我正在学习春天和 mybatis 。
我遇到了一个问题。我主要从officail tutorial学习。但我无法得到我的意思。
春天无法在 ServiceImpl 中自动装配 UserMapper 。
还有一个例外
java.lang.NullPointerException
main.java.cn.qingtianr.service.impl.UserServiceImpl.findByUserName(UserServiceImpl.java:23)
main.java.cn.qingtianr.action.UserAction.execute(UserAction.java:22)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:870)
ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1293)
ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1369)
ognl.ASTMethod.getValueBody(ASTMethod.java:90)
ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
ognl.SimpleNode.getValue(SimpleNode.java:258)
ognl.Ognl.getValue(Ognl.java:494)
还有其他来源。
的applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="archivesi" class="main.java.cn.qingtianr.service.impl.ArchiveServiceImpl"/>
<bean id="articlesi" class="main.java.cn.qingtianr.service.impl.ArticleServiceImpl"/>
<bean id="usersi" class="main.java.cn.qingtianr.service.impl.UserServiceImpl"/>
<!--<aop:aspectj-autoproxy proxy-target-class="true" />-->
<!-- 自动扫描 -->
<!--<context:component-scan base-package="main.java.cn.qingtianr" />-->
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:main/resources/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="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="userdao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="main.java.cn.qingtianr.dao.UserDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
UserServiceImpl.java
package main.java.cn.qingtianr.service.impl;
import main.java.cn.qingtianr.dao.UserDao;
import main.java.cn.qingtianr.dbc.MybatisSqlSessionFactory;
import main.java.cn.qingtianr.model.User;
import main.java.cn.qingtianr.service.UserService;
/**
* Created by jack on 16-3-29.
*/
public class UserServiceImpl implements UserService{
private UserDao userdao;
public User findByUserName(String username) throws Exception
{
User user = null;
try
{
if(userdao == null){
System.out.println("11111111111");
}
user = this.userdao.findByUserName(username);
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
MybatisSqlSessionFactory.closeSession();
}
return user;
}
public UserDao getUserdao() {
System.out.println("It is in getUserdao");
return userdao;
}
public void setUserdao(UserDao userdao) {
System.out.println("It is in setUserdao");
this.userdao = userdao;
}
}
UserDao.java
package main.java.cn.qingtianr.dao;
import main.java.cn.qingtianr.model.User;
/**
* Created by jack on 16-3-29.
*/
public interface UserDao {
public User findByUserName(String username) throws Exception;
}
UserDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="main.java.cn.qingtianr.dao.UserDao">
<select id="findByUserName" parameterType="int" resultType="main.java.cn.qingtianr.model.User">
SELECT * FROM user WHERE username = #{username}
</select>
</mapper>
如果spring autowire mybatis Mapper,则会在 UserServiceImpl.java 中打印It is in setUserdao
或getUserdao
。
但11111111
为空时只有userdao
。
那么有人可以帮助我吗? THX。
答案 0 :(得分:0)
<bean id="usersi" class="main.java.cn.qingtianr.service.impl.UserServiceImpl">
<property name="userdao" ref = "userdao"/>
</bean >
答案 1 :(得分:-1)
您没有在usersi bean定义中设置userDao bean。请点击此处了解更多信息:http://www.mybatis.org/spring/mappers.html