org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFa
·
项目场景:
SSM基本框架测试,实现helloworld报错如下:
'sqlSessionFactory’开头的东西,后面才是重点。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [application-dao.xml]:
Invocation of init method failed; nested exception is
org.springframework.core.NestedIOException:
Failed to parse config resource: class path resource [mybatis.cfg.xml];
解决方案:
- 配置文件内容是否出错,复制过来的没有内容上的错误。
- 项目结构是否搭建完成即Controller层,Service层,Mapper层,相关注释有没有写上。
项目结构:

LoginController:这里的restcontroller注释
@RestController
public class LoginController {
/**
* 跳转到登录页面
* @return
*/
@RequestMapping("/goLoginPage")
public String goLogin(){
return "login";
}
}
impl.StudentService:这里的service注释
@Service
public class StudentService implements service.StudentService {
@Autowired
private StudentDao studentDao;
@Override
public Student getStudentById(long studentId) {
return studentDao.getStudentById(studentId);
}
}
StudentMapper:这里的mapper注释
@Mapper
public interface DeptMapper extends BaseMapper<Dept> {
int mydeleteById(int id);
}
如果不在这里写mapper和sevice,那就在启动文件那里写mapperscan和componentscan,不写的话@Autowired标签会报错。
@SpringBootApplication
@MapperScan("generator.mapper")
@ComponentScan("generator.service")
public class MybatisplusfinalApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusfinalApplication.class, args);
}
}
另外如果出现Can not find table primary key in Class的错误是没在实体类那里加@TabliId标签,强烈建议用MybatisX插件去搞,最标准。只要补一下Mapper标签就可以了。
Invalid bound statement (not found): mapper.StudentMapper.selectById
- 如果上面没问题,报错原因是因为在mybatis里面这段代码没有删除导致spring和mybatis都扫描了一遍xml,就出错了。删掉Mybatis.cfg.xml里的扫描。
//Mybatis.cfg.xml
<!-- 4.加载Dao层映射文件 --
<mappers>
<mapper resource="mappers/StudentMapper.xml"></mapper>
</mappers>
//application-dao.xml
<!-- 3,配置aop -->
<aop:config>
<!-- 声明一个切入点 -->
<aop:pointcut expression="execution(* service.impl.*.*(..))"
id="pc1" />
<aop:advisor advice-ref="txAdvise" pointcut-ref="pc1" />
</aop:config>
附上完整application-dao.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 1.加载属性集文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.建数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${mysql.driver}"></property>
<property name="url" value="${mysql.url}"></property>
<property name="username" value="${mysql.username}"></property>
<property name="password" value="${mysql.password}"></property>
</bean>
<!-- 3.配置SqlSessionFactory -->
<bean id="sqlSessionFactory" name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入dataSource -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载 mybtais.cfg.xml -->
<property name="configLocation" value="classpath:mybatis.cfg.xml"></property>
</bean>
<!-- dao接口所在包名,Spring会自动查找之中的类 根据dao接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 提供dao接口所在以包 如果有多个包,可以使用,去隔开-->
<property name="basePackage" value="dao"></property>
<!-- 注入sessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 配置声明式事物 -->
<!-- 1.声明事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2.声明事务事务传播特性 【哪些方法加事务。哪此不加】 -->
<tx:advice id="txAdvise" transaction-manager="transactionManager">
<tx:attributes>
<!-- 在切面里面如查有方法名是add开头的就加事物 -->
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="load*" propagation="REQUIRED" />
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 3,配置aop -->
<aop:config>
<!-- 声明一个切入点 -->
<aop:pointcut expression="execution(* service.impl.*.*(..))"
id="pc1" />
<aop:advisor advice-ref="txAdvise" pointcut-ref="pc1" />
</aop:config>
</beans>
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)