求助。在maven中整合spring-mvc和mybatis时遇到错误日志

大刘_南京 2016-09-12 09:11:23
九月 12, 2016 9:00:00 下午 org.springframework.web.context.support.XmlWebApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.cfdcn.demoweb.dao.UserDao.setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined
...全文
312 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
大刘_南京 2016-09-30
  • 打赏
  • 举报
回复
找到原因了,是xml配置文件里写的不对!
  • 打赏
  • 举报
回复
楼上说的有道理
  • 打赏
  • 举报
回复
datasource在什么文件里配置的,放到主配置文件里。
小灯光环 2016-09-12
  • 打赏
  • 举报
回复
这样配置:
	<!-- 配置mybitasSqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
<constructor-arg index="1" value="BATCH" />
</bean>


	@Resource(name = "sqlSession")
private SqlSession sqlSession;

public SqlSession getSqlSession() {
return sqlSession;
}
大刘_南京 2016-09-12
  • 打赏
  • 举报
回复
UserDao: package com.cfdcn.demoweb.dao; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.support.SqlSessionDaoSupport; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.cfdcn.demoweb.entity.User; @Component public class UserDao extends SqlSessionDaoSupport { @Autowired public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { super.setSqlSessionFactory(sqlSessionFactory); } public User selectById(int id) { return this.getSqlSession().selectOne("com.cfdcn.demoweb.UserMapper.selectById", id); } }

81,091

社区成员

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

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