今天修改了一些东西后,发现web项目启动不了了。日志中出现504组如下的日志,都是mybatis打印的,扫描jar,解析dao,mapping文件等信息。费了半天劲,也没有找到问题。因为这些全是debug级别的,我就没有在意。后来发现每组的最后一条信息"Ignoring bean creation exception...",有错误信息,才发现问题的根源是mybatis的mapping.xml文件中,语句的id重复了。
可问题是
1.为什么会打印这么多重复的日志呢?
2.为什么只有debug级别的日志呢?
问题1:
通过对日志 和源码的学习,终于把第一问题解答了。
1). dao类是需要注入一个SqlSessionFactory的。mybatis与spring整合,对应的是SqlSessionFactoryBean类。
2). SqlSessionFactoryBean在创建的时候,会加载所有的mapping文件。
3). 当加载失败的时候,不会抛出异常,只是打印了一个debug级别的日志。刚才提到的"Ignoring bean creation exception..."信息。
所以程序会继续进行。
4). 然后,当每次需要注入“依赖 [此SqlSessionFactoryBean类] 的Dao类”的时候,就会打印一组 3)中的错误信息。
so,当有很多类依赖这些Dao的时候,就有了这504组错误信息。。
问题2:
查看了所有的日志,发现只有这一行信息能够清楚的说明问题的原因。这一行信息是由spring记录的。( org.springframework.beans.factory.support.AbstractBeanFactory#getTypeForFactoryBean )
估计他并不想直接吞掉这个异常,因为把这个Exception放入了suppressedExceptions属性里,可能会在必要的时候统一抛出。
可是,在这种情况下,他并没有抛出正确的异常。不知算不算mybatis-spring的bug。总之,这种编程问题,在测试环境下,就可以发现,不会影响生产环境,所以问题也不大。不过这种处理方式,错信息混杂在debug级别的信息中,确实不太好定位问题。
附两组日志:
DEBUG DefaultListableBeanFactory - Creating shared instance of singleton bean 'tradeShareDao' DEBUG DefaultListableBeanFactory - Creating instance of bean 'tradeShareDao' DEBUG DefaultListableBeanFactory - Eagerly caching bean 'tradeShareDao' to allow for resolving potential circular references DEBUG DefaultListableBeanFactory - Creating shared instance of singleton bean 'settlementSqlSessionFactory' DEBUG DefaultListableBeanFactory - Creating instance of bean 'settlementSqlSessionFactory' DEBUG DefaultListableBeanFactory - Eagerly caching bean 'settlementSqlSessionFactory' to allow for resolving potential circular references DEBUG DefaultListableBeanFactory - Returning cached instance of singleton bean 'settlementDataSource' DEBUG PathMatchingResourcePatternResolver - Looking for matching resources in jar file [file:/xxxx/webapps/management/WEB-INF/lib/xxx-settlement-orm-1.0-SNAPSHOT.jar] DEBUG PathMatchingResourcePatternResolver - Resolved location pattern [classpath:settlement_mapper/*Mapper.xml] to resources [class path resource [settlement_mapper/PaymentTypeMapper.xml], class path resource [settlement_mapper/TradeShareMapper.xml]] DEBUG DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'settlementSqlSessionFactory' DEBUG ResolverUtil - Find JAR URL: jar:file:/xxxx/webapps/management/WEB-INF/lib/xxx-settlement-orm-1.0-SNAPSHOT.jar!/com/test/settlement/orm/ DEBUG ResolverUtil - Inner URL: file:/xxxx/webapps/management/WEB-INF/lib/xxx-settlement-orm-1.0-SNAPSHOT.jar!/com/test/settlement/orm/ DEBUG ResolverUtil - Extracted JAR URL: file:/xxxx/webapps/management/WEB-INF/lib/xxx-settlement-orm-1.0-SNAPSHOT.jar DEBUG ResolverUtil - Found JAR: file:/xxxx/webapps/management/WEB-INF/lib/xxx-settlement-orm-1.0-SNAPSHOT.jar DEBUG ResolverUtil - Listing jar:file:/xxxx/webapps/management/WEB-INF/lib/xxx-settlement-orm-1.0-SNAPSHOT.jar!/com/test/settlement/orm/ DEBUG ResolverUtil - Found resource: /com/test/settlement/orm/dao/TradeShareDao.class DEBUG ResolverUtil - Found resource: /com/test/settlement/orm/dao/PaymentTypeDao.class DEBUG ResolverUtil - Checking to see if class com.test.settlement.orm.dao.TradeShareDao matches criteria [is assignable to Object] DEBUG ResolverUtil - Checking to see if class com.test.settlement.orm.dao.PaymentTypeDao matches criteria [is assignable to Object] DEBUG DefaultListableBeanFactory - Ignoring bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tradeShareDao' defined in URL [jar:file:/xxxx/webapps/management/WEB-INF/lib/xxx-settlement-orm-1.0-SNAPSHOT.jar!/com/test/settlement/orm/dao/TradeShareDao.class]: Cannot resolve reference to bean 'settlementSqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'settlementSqlSessionFactory' defined in class path resource [applicationContext-db-settlement.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'class path resource [settlement_mapper/manual-SettleItemMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: XML fragments parsed from previous mappers already contains value for com.test.settlement.orm.dao.SettleItemDao.SettleItem_columns DEBUG DefaultListableBeanFactory - Creating shared instance of singleton bean 'paymentTypeDao' DEBUG DefaultListableBeanFactory - Creating instance of bean 'paymentTypeDao' DEBUG DefaultListableBeanFactory - Eagerly caching bean 'paymentTypeDao' to allow for resolving potential circular references DEBUG DefaultListableBeanFactory - Creating shared instance of singleton bean 'settlementSqlSessionFactory' DEBUG DefaultListableBeanFactory - Creating instance of bean 'settlementSqlSessionFactory' DEBUG DefaultListableBeanFactory - Eagerly caching bean 'settlementSqlSessionFactory' to allow for resolving potential circular references DEBUG DefaultListableBeanFactory - Returning cached instance of singleton bean 'settlementDataSource' DEBUG PathMatchingResourcePatternResolver - Looking for matching resources in jar file [file:/xxxx/webapps/management/WEB-INF/lib/xxx-settlement-orm-1.0-SNAPSHOT.jar] DEBUG PathMatchingResourcePatternResolver - Resolved location pattern [classpath:settlement_mapper/*Mapper.xml] to resources [class path resource [settlement_mapper/PaymentTypeMapper.xml], class path resource [settlement_mapper/TradeShareMapper.xml]] DEBUG DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'settlementSqlSessionFactory' DEBUG ResolverUtil - Find JAR URL: jar:file:/xxxx/webapps/management/WEB-INF/lib/xxx-settlement-orm-1.0-SNAPSHOT.jar!/com/test/settlement/orm/ DEBUG ResolverUtil - Inner URL: file:/xxxx/webapps/management/WEB-INF/lib/xxx-settlement-orm-1.0-SNAPSHOT.jar!/com/test/settlement/orm/ DEBUG ResolverUtil - Extracted JAR URL: file:/xxxx/webapps/management/WEB-INF/lib/xxx-settlement-orm-1.0-SNAPSHOT.jar DEBUG ResolverUtil - Found JAR: file:/xxxx/webapps/management/WEB-INF/lib/xxx-settlement-orm-1.0-SNAPSHOT.jar DEBUG ResolverUtil - Listing jar:file:/xxxx/webapps/management/WEB-INF/lib/xxx-settlement-orm-1.0-SNAPSHOT.jar!/com/test/settlement/orm/ DEBUG ResolverUtil - Found resource: /com/test/settlement/orm/dao/TradeShareDao.class DEBUG ResolverUtil - Found resource: /com/test/settlement/orm/dao/PaymentTypeDao.class DEBUG ResolverUtil - Checking to see if class com.test.settlement.orm.dao.TradeShareDao matches criteria [is assignable to Object] DEBUG ResolverUtil - Checking to see if class com.test.settlement.orm.dao.PaymentTypeDao matches criteria [is assignable to Object] DEBUG DefaultListableBeanFactory - Ignoring bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'paymentTypeDao' defined in URL [jar:file:/xxxx/webapps/management/WEB-INF/lib/xxx-settlement-orm-1.0-SNAPSHOT.jar!/com/test/settlement/orm/dao/PaymentTypeDao.class]: Cannot resolve reference to bean 'settlementSqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'settlementSqlSessionFactory' defined in class path resource [applicationContext-db-settlement.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'class path resource [settlement_mapper/manual-SettleItemMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: XML fragments parsed from previous mappers already contains value for com.test.settlement.orm.dao.SettleItemDao.SettleItem_columns
相关推荐
3. **整合到Spring Boot**:在Spring Boot的配置文件`application.yml`或`application.properties`中配置MyBatis的相关属性,如数据源、Mapper扫描路径等。同时,在主启动类上添加`@MapperScan`注解,指定Mapper接口...
4. **DAO实现(DAO Implementation)**:MBG生成的注解式Mapper接口可以被MyBatis自动扫描并注入到Spring或其它IoC容器中,无需额外的DAO实现类。 使用MyBatis Generator时,开发者只需运行MBG,它会根据配置文件中...
mybatis.type-aliases-package=com.neo.model spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true spring.datasource.username=root...
Mybatis Plus是一款强大的扩展Mybatis的工具,它简化了...通过Mybatis Plus的代码生成器,开发者可以极大地提高开发效率,专注于业务逻辑,而不是重复的代码编写工作。结合Spring MVC,可以构建出高效、稳定的Web应用。
这款工具能够自动生成MyBatis框架所需的实体类、Mapper接口、XML映射文件以及对应的DAO层实现类,让开发者可以将更多的精力集中在业务逻辑上,而非重复性的基础代码编写。 在MyBatis项目中,实体类是用来封装数据的...
在applicationContext.xml中配置数据源、SqlSessionFactory、TransactionManager以及MyBatis的扫描路径。例如: ```xml <!-- 配置数据源属性 --> <bean id="sqlSessionFactory" class="org.mybatis.spring....
将生成的文件导入到项目中,配置MyBatis的SqlSessionFactory,并在Spring或其它框架中配置Mapper扫描,就可以在代码中直接使用这些自动生成的组件进行数据库操作了。 总结来说,MyBatis Generator是一个强大的自动...
通过MyBatis逆向工程,开发者可以专注于业务逻辑的实现,而不是重复的基础代码编写。同时,随着数据库表结构的修改,只需更新配置文件,再次运行逆向工程,就能快速同步代码,保持数据访问层与数据库的一致性。这...
- 通过`<context:component-scan base-package=""/>`标签指定扫描包路径。 9. **其他常用注解**: - **`@Test`**:用于单元测试。 - **`@Before`**:用于方法前。 - **`@After`**:用于方法后。 #### 六、Bean...
为了使 MyBatis-Plus 能够识别并正确处理达梦数据库,你可能还需要自定义一个实体扫描器(EntityScanner)和一个数据库 ID 生成器(IDGenerator),因为 MyBatis-Plus 默认的 ID 生成策略可能不适用于达梦数据库。...
可以通过`mybatis.type-aliases-package`配置项来指定Bean所在的包,这样MyBatis就能自动识别这些包内的类,避免因类名冲突导致的问题。例如: ``` mybatis.type-aliases-package=com.test.demo.model ``` 4. *...
- 将生成的文件引入到你的项目中,并在 Mybatis 的配置文件中添加 Mapper 的扫描路径。 - 最后,在代码中通过 SqlSession 和 Mapper 接口即可进行数据库操作。 Mybatis Generator for Cong 提供了一个方便的解决...
MyBatisPlus 在 MyBatis 的基础上进行了增强,减少了大量重复的代码,使得开发者可以更加专注于业务逻辑。 1. **集成 MyBatisPlus into Spring Boot** 集成 MyBatisPlus 到 Spring Boot 项目中,首先需要在 `pom....
MyBatis的配置主要包括数据源、SqlSessionFactory和Mapper扫描器。这些配置通常会放在src/main/resources目录下的配置文件中,如applicationContext.xml、servlet-context.xml和mybatis-config.xml。 在开发过程中...
- 对于数据访问对象(DAO),通常不直接使用`@Repository`注解,因为多数情况下DAO依赖于Spring对持久层框架(如MyBatis)的支持。但在不需要框架支持的情况下,可以使用`@Repository`注解来标识DAO类。 3. **@...