在springboot项目中配置数据库下划线命名映射为java的驼峰命名时出错

问题

在使用spirngboot集成mybaits的时候,想要开启命名映射,如图配置

# mybatis配置
mybatis:
  type-aliases-package: com.zhong.springcloud.pojo
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
#  命名映射
  configuration:
    map-underscore-to-camel-case: true

项目中配置了mybatis-config.xml
在springboot项目中配置数据库下划线命名映射为java的驼峰命名时出错_第1张图片
mybatis-config.xml文件中的配置


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <settings>
        
        <setting name="cacheEnabled" value="true"/>
    settings>
configuration>

错误信息

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method ‘sqlSessionFactory’ threw exception; nested exception is java.lang.IllegalStateException: Property ‘configuration’ and ‘configLocation’ can not specified with together
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.24.jar:5.3.24]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.24.jar:5.3.24]
… 78 common frames omitted
Caused by: java.lang.IllegalStateException: Property ‘configuration’ and ‘configLocation’ can not specified with together
at org.springframework.util.Assert.state(Assert.java:76) ~[spring-core-5.3.24.jar:5.3.24]
at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:488) ~[mybatis-spring-2.0.7.jar:2.0.7]
at org.mybatis.spring.SqlSessionFactoryBean.getObject(SqlSessionFactoryBean.java:633) ~[mybatis-spring-2.0.7.jar:2.0.7]
at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration.sqlSessionFactory(MybatisAutoConfiguration.java:188) ~[mybatis-spring-boot-autoconfigure-2.2.2.jar:2.2.2]
at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration E n h a n c e r B y S p r i n g C G L I B EnhancerBySpringCGLIB EnhancerBySpringCGLIB36ad62da.CGLIB$sqlSessionFactory 1 ( < g e n e r a t e d > )   [ m y b a t i s − s p r i n g − b o o t − a u t o c o n f i g u r e − 2.2.2. j a r : 2.2.2 ] a t o r g . m y b a t i s . s p r i n g . b o o t . a u t o c o n f i g u r e . M y b a t i s A u t o C o n f i g u r a t i o n 1() ~[mybatis-spring-boot-autoconfigure-2.2.2.jar:2.2.2] at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration 1(<generated>) [mybatisspringbootautoconfigure2.2.2.jar:2.2.2]atorg.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration E n h a n c e r B y S p r i n g C G L I B EnhancerBySpringCGLIB EnhancerBySpringCGLIB 36 a d 62 d a 36ad62da 36ad62da F a s t C l a s s B y S p r i n g C G L I B FastClassBySpringCGLIB FastClassBySpringCGLIB 54783 d 2 d . i n v o k e ( < g e n e r a t e d > )   [ m y b a t i s − s p r i n g − b o o t − a u t o c o n f i g u r e − 2.2.2. j a r : 2.2.2 ] a t o r g . s p r i n g f r a m e w o r k . c g l i b . p r o x y . M e t h o d P r o x y . i n v o k e S u p e r ( M e t h o d P r o x y . j a v a : 244 )   [ s p r i n g − c o r e − 5.3.24. j a r : 5.3.24 ] a t o r g . s p r i n g f r a m e w o r k . c o n t e x t . a n n o t a t i o n . C o n f i g u r a t i o n C l a s s E n h a n c e r 54783d2d.invoke() ~[mybatis-spring-boot-autoconfigure-2.2.2.jar:2.2.2] at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.3.24.jar:5.3.24] at org.springframework.context.annotation.ConfigurationClassEnhancer 54783d2d.invoke(<generated>) [mybatisspringbootautoconfigure2.2.2.jar:2.2.2]atorg.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) [springcore5.3.24.jar:5.3.24]atorg.springframework.context.annotation.ConfigurationClassEnhancerBeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.3.24.jar:5.3.24]
at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration E n h a n c e r B y S p r i n g C G L I B EnhancerBySpringCGLIB EnhancerBySpringCGLIB36ad62da.sqlSessionFactory() ~[mybatis-spring-boot-autoconfigure-2.2.2.jar:2.2.2]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_271]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_271]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_271]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_271]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.24.jar:5.3.24]
… 79 common frames omitted

关闭命名映射之后或者关闭配置路径之后项目正常。

原因分析

根据错误信息分析,出现该错误的原因是同时使用了configLocation和configuration属性。这两个属性是SqlSessionFactoryBean类的属性,分别用于指定MyBatis配置文件的位置和MyBatis配置对象。

在配置中,你在application.xml中指定了configLocation属性,在mybatis-config.xml中定义了MyBatis的配置对象,在applicaiton中又指定了configuration属性,因此出现了冲突。

解决方法

  1. 解决该问题的方法是,需要在应用程序中删除configLocation属性,只使用applicaiton.yml中的configuration属性来指定MyBatis的配置对象。或者不使用applicaiton.yml中的configuration属性,就可以配置config-locations并且在其中配置属性
  2. 如果需要同时启用这两个属性,则需要自定义一个MybatisConfig类,该类将扩展org.mybatis.spring.boot.autoconfigure.MybatisProperties类。

你可能感兴趣的:(解决问题,java,spring,boot,数据库,springcloud)