首页 > 其他分享 >Spring与MyBatis集成中遇到的问题

Spring与MyBatis集成中遇到的问题

时间:2023-10-06 12:33:21浏览次数:35  
标签:集成 java Spring support factory springframework beans MyBatis org

1、依赖版本问题

描述

在进行Spring框架于MyBatis框架集成时需要使用xml文件装配sqlSessionFactory为bean,从而自动获取sqlSession。遇到了sqlSessionFactory装配失败的问题

报错信息

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-config.xml]: Invocation of init method failed; nested exception is java.lang.BootstrapMethodError: java.lang.NoSuchMethodError: org.apache.ibatis.session.Configuration.setVfsImpl(Ljava/lang/Class;)V
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1511)
	... 28 more
Caused by: java.lang.BootstrapMethodError: java.lang.NoSuchMethodError: org.apache.ibatis.session.Configuration.setVfsImpl(Ljava/lang/Class;)V
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:528)
	at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:490)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792)
	... 38 more

解决

反复检验之后发现是Maven依赖版本问题,MyBatis 3.2.8版本适配有问题。

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
            <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>
启动IoC容器
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/Study/Repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/Study/Repository/org/slf4j/slf4j-log4j12/1.7.5/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

2、原实现类与MyBatis接口冲突问题

描述

本次集成是在原本三层架构的Spring+JDBC项目基础上进行重构,各个DAO层实现类在ioc容器中进行bean装配,而MyBatis的映射是基于DAO层接口的,在Service层进行依赖注入时会发生冲突,所以要将实现类移除出ioc容器

报错信息

Exception in thread "main" java.lang.ExceptionInInitializerError
	at com.dtinone.sgproj.test.TestMain.main(TestMain.java:11)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImp': Unsatisfied dependency expressed through field 'udao'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.dtinone.sgproj.dao.UserDAOInter' available: expected single matching bean but found 2: userDAOImp,userDAOInter
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
	at com.dtinone.util.IocUtil.<init>(IocUtil.java:13)
	at com.dtinone.util.IocUtil.<clinit>(IocUtil.java:7)
	... 1 more

解决

去除@Repository注解,也即不装配实现类为bean,则只有接口和映射文件作为DAO层实现

package com.dtinone.sgproj.dao.imp;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import com.dtinone.sgproj.bean.UserBean;
import com.dtinone.util.DBUtil;
import com.dtinone.sgproj.dao.UserDAOInter;
import org.springframework.stereotype.Repository;
 
public class UserDAOImp implements UserDAOInter {
 
	@Override
	public UserBean getUserByNamePwd(UserBean user) {
		// TODO Auto-generated method stub
		UserBean theUser = null;
 
		String sql = "select * from t_user where f_username = ? and f_password = ?";
		Connection con = DBUtil.getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setString(1, user.getF_username());
			ps.setString(2, user.getF_password());
 
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				theUser = new UserBean();
				theUser.setPk_userid(rs.getInt("pk_userid"));
				theUser.setF_username(rs.getString("f_username"));
				theUser.setF_password(rs.getString("f_password"));
			}
 
 
 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			DBUtil.closeConnection(con);
		}
 
 
		return theUser;
	}
 
}

标签:集成,java,Spring,support,factory,springframework,beans,MyBatis,org
From: https://www.cnblogs.com/tobieance/p/17744427.html

相关文章

  • Spring Boot 热部署
    相信小伙伴们在日常的开发中,调试代码时,免不了经常修改代码,这个时候,为了验证效果,必须要重启SpringBoot应用。频繁地重启应用,导致开发效率降低,加班随之而来。有没有什么办法,能让SpringBoot项目热部署呢,从而不用每次都手点。答案是肯定的。第一步:添加spring-boot-devtools......
  • spring注解开发---beans注入
    万能xml开头:<!--导入p,c命名空间context注解--><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"......
  • Springboot整合Redis
    一、Redis简介简单来说Redis就是一个使用C语言开发的数据库,不过与传统数据库不同的是Redis的数据是存在内存中的,也就是它是内存数据库,所以读写速度非常快,因此Redis被广泛应用于缓存方向。另外,Redis除了做缓存之外,Redis也经常用来做分布式锁,甚至是消息队列。Redis......
  • Spring中Bean的六大行为模式
    一、singleton:单例模式(默认)singleton是Bean在Spring中默认的行为,这个默认的行为模式就是说一个类只有一个Bean对象,所有的用户都使用的同一个对象,如果有一个用户对这个对象进行了修改,那么所有的结果都会发生改变。如:这样一个User类:packagecom.demo.spring.beans;publi......
  • Spring Mvc框架环境搭建
    1.创建动态WEB项目本次在IDEA2021.2.1版本中创建SpringMV 出现webapp文件夹则说明文件创建好啦!2.配置TomCat配置完之后,让我们来运行一下! 配置成功了!!!配置Spring前端控制器<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="<http://maven.apache.org/......
  • 认识Spring MVC
    认识SpringMV   工作流程图解析 前端控制器前端控制器是SpringMVC的核心,负责接受Http请求并协调各个组件请求工作。<!DOCTYPEweb-appPUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication2.3//EN""<http://java.sun.com/dtd/web-app_2_3.dtd>"><web-......
  • 基于Java+SpringBoot+Vue的微信小程序实现民族节日及民间故事推广系统
    一、前言......
  • 分布式事务 —— SpringCloud Alibaba Seata
    Seata简介传统的单体应用中,业务操作使用同一条连接操作不同的数据表,一旦出现异常就可以整体回滚。随着公司的快速发展、业务需求的变化,单体应用被拆分成微服务应用,原来的单体应用被拆分成多个独立的微服务,分别使用独立的数据源,业务操作需要调用三个服务来完成。此时每个服务内部......
  • Spring初见
    Spring初见Spring简介Spring是分层的JavaEE轻量级开源框架,以控制反转(IOC)和面向前面编程(AOP)为内核,提供表现层:SpringMVC持久层SpringJDBC的技术。Spring的使命就是简化开发!!Spring特点方便解耦,简化开发:IOC容器可将对象之间的依赖关系交给Spring控制AOP编程的支持:可使用面向......
  • 【SpringBoot】多环境配置和启动
    环境分类,可以分为本地环境、测试环境、生产环境等,通过对不同环境配置内容,来实现对不同环境做不同的事情。SpringBoot项目,通过application-xxx.yml添加不同的后缀来区分配置文件,启动时候通过后缀启动即可。application.yml:公共配置application-xxx.yml:不同环境的差异配......