springmvc整合mybatis时,获取不到sqlsession

刘大神仙 2016-04-17 08:33:41
application.xml文件加载了,但是DAO层方法获取不到sqlsession,下面是代码

web.xml
<display-name>mybatis</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


applicationContext.xml

<!-- 配置jdbc文件的位置 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties"></property>
</bean>

<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" init-method="">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>

<!-- mybatis在spring中的bean配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean>

<bean id="test" class="com.leq.beans.Test">
<property name="title" value="aa"></property>
<property name="body" value="bb"></property>
</bean>


Dao方法

@Repository
public class NewsDaoImpl extends SqlSessionDaoSupport implements NewsDao {


// @Resource
private Test test;

public Test getTest() {
return test;
}
@Resource
public void setTest(Test test) {
this.test = test;
}
// @Resource
private SqlSessionFactory ssf;
// @Resource
// private SqlSessionTemplate sst;
// public SqlSessionTemplate getSst() {
// return sst;
// }
// public void setSst(SqlSessionTemplate sst) {
// this.sst = sst;
// }
// @Resource
private BasicDataSource bds;


public BasicDataSource getBds() {
return bds;
}
@Resource
public void setBds(BasicDataSource bds) {
this.bds = bds;
}
@Resource
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
super.setSqlSessionFactory(sqlSessionFactory);

}
public void getSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
super.getSqlSession();
}
@Override
public Object getNewsInfo(int id) throws Exception {
System.out.println("test ..."+test);
System.out.println("ssf ..."+ssf);
System.out.println("BasicDataSource ..."+bds);
return null;
}

}



下面的是日志,日志上面有加载applicationcontext.xml,但是DAO方法获取不到,是不是我写错了???有高手告诉一下吗


四月 17, 2016 8:25:30 下午 org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'spring'
四月 17, 2016 8:25:30 下午 org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'spring': initialization started
四月 17, 2016 8:25:30 下午 org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'spring-servlet': startup date [Sun Apr 17 20:25:30 CST 2016]; parent: Root WebApplicationContext
四月 17, 2016 8:25:30 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml]
四月 17, 2016 8:25:31 下午 org.springframework.context.annotation.ClassPathBeanDefinitionScanner registerDefaultFilters
INFO: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
四月 17, 2016 8:25:31 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1c62bc5: defining beans [newsController,newsDaoImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter#0,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter#1,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,transactionManager,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#1,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@4f707f0c
四月 17, 2016 8:25:31 下午 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/login.do] onto handler 'newsController'
四月 17, 2016 8:25:31 下午 org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'spring': initialization completed in 273 ms
NewsController ...
test ...null
ssf ...null
BasicDataSource ...null
...全文
301 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
名字写成一样的
chenzxapple 2016-04-17
  • 打赏
  • 举报
回复
你用@Resource要在spring-mvc打开注解加上 <mvc:annotation-driven/> 你试试再加上这个 <bean id="sqlSession" scope="prototype" factory-bean="sqlSessionFactory" factory-method="openSession"> </bean> 这个是自动扫描com.tarena包及其子包,将所有扫描到的Mapper接口都创建Bean对象 你有包的话可以参考下 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.tarena"/> <property name="annotationClass" value="com.tarena.entity.MapperBean"/> </bean>

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧