Spring集成Ibatis:轻量级容器的数据持久化支持

需积分: 0 12 下载量 63 浏览量 更新于2024-08-16 收藏 734KB PPT 举报
Spring提供对Ibatis整合的强大支持,它是一个基于IoC(Inversion of Control)模式的轻量级容器,专注于数据类型对象的生命周期管理和维护,特别适合与主流持久层框架协同工作。通过集成,Spring简化了数据访问层的管理,使得开发人员能够更加专注于业务逻辑。 Spring对Ibatis的支持主要体现在以下几个方面: 1. **数据源管理**: - Spring的DriverManagerDataSource类负责管理数据库连接,通过Spring的依赖注入(DI)机制,使得数据源的创建和管理不再直接依赖于应用程序代码,提高了代码的灵活性和可维护性。 2. **SqlMapClient支持**: - SqlMapClientFactoryBean类是Spring提供的一个工厂Bean,用于创建和管理SqlMapClient实例,这使得SqlMapClient的生命周期与Spring容器同步,便于在Spring应用上下文中进行统一的配置和管理。 3. **DAO支持**: - SqlMapClientDaoSupport是一个基础支持类,它允许开发者继承并使用Spring的模板方法来执行Ibatis操作,如CRUD操作,避免了显式地创建SqlMapClient实例,简化了代码。 4. **SqlMapClientTemplate**: - 这个类封装了Ibatis的持久化操作接口,提供了诸如查询、插入、更新和删除等功能,用户可以直接调用这些API来实现业务逻辑。 **Spring与Ibatis整合步骤**: - 在项目中集成Spring支持,通常是在WebProject中添加Spring配置。 - 添加Ibatis所需的核心包、日志包以及数据库驱动包。 - 在Spring的applicationContext.xml配置文件中,将Ibatis的数据源配置交给Spring管理,避免重复配置。 - 加载SqlMapConfig.xml文件,以进一步配置其他参数。 - 定义和映射pojo类到Ibatis的XML映射文件,实现对象与数据库表的关联。 - 创建SqlMapConfig.xml,该文件在Spring集成后不再需要单独配置数据源。 - 新建DAO类,使用Spring的SqlMapClientTemplate进行操作。 总结来说,Spring对Ibatis的整合使开发流程更为简洁,通过Spring的依赖注入和管理机制,提升了代码的可测试性和解耦性。同时,Spring对Ibatis的整合提供了高度的灵活性,使得项目可以根据需求进行调整,更好地适应复杂的企业级开发环境。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deptServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'deptMapper' defined in file [D:\WorkSpace\work13\djd_server\target\classes\com\jiading\djd\mapper\DeptMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 40; 元素内容必须由格式正确的字符数据或标记组成。 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:893) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at com.jiading.djd.DjdApplication.main(DjdApplication.java:14) [classes/:na]报错了

2023-07-25 上传

java.lang.IllegalStateException: Failed to execute CommandLineRunner at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:779) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] at com.unkown.data.hw.ipran.straight.collect.UnkownDataHwIpranStraightCollectApplication.main(UnkownDataHwIpranStraightCollectApplication.java:39) [classes/:na] Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.unkown.data.hw.ipran.straight.collect.dao.RTrsHwIpranCircuitDao.queryById at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235) ~[mybatis-3.5.5.jar:3.5.5] at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53) ~[mybatis-3.5.5.jar:3.5.5] at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:115) ~[mybatis-3.5.5.jar:3.5.5] at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) ~[na:1.8.0_271] at org.apache.ibatis.binding.MapperProxy.cachedInvoker(MapperProxy.java:102) ~[mybatis-3.5.5.jar:3.5.5] at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85) ~[mybatis-3.5.5.jar:3.5.5] at com.sun.proxy.$Proxy108.queryById(Unknown Source) ~[na:na] at com.unkown.data.hw.ipran.straight.collect.service.impl.RTrsHwIpranCircuitServiceImpl.queryById(RTrsHwIpranCircuitServiceImpl.java:29) ~[classes/:na] at com.unkown.data.hw.ipran.straight.collect.controller.RTrsHwIpranCircuitController.IpranSend(RTrsHwIpranCircuitController.java:54) ~[classes/:na] at com.unkown.data.hw.ipran.straight.collect.MyRunner.run(MyRunner.java:24) ~[classes/:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] ... 5 common frames omitted

2023-05-18 上传