码上敲享录 > mybatis的常见问题 > SpringBoot中mybatis配置mybatis.configuration.map-underscore-to-camel-case=true自动转换驼峰标识没有生效

SpringBoot中mybatis配置mybatis.configuration.map-underscore-to-camel-case=true自动转换驼峰标识没有生效

上一章章节目录下一章 2019-12-14已有9138人阅读 评论(0)

SpringBoot中mybatis配置mybatis.configuration.map-underscore-to-camel-case=true没有生效

#开启驼峰法

mybatis.configuration.map-underscore-to-camel-case=true

使用该配置可以让mybatis自动将SQL中查出来的带下划线的字段,转换为驼峰标志,再去匹配类中的属性。如数据库字段为user_name,转换为驼峰标志,再去匹配类中的属性userName。

查找原因:原来是项目中用了多数据源导致配置没生效,如果配置文件写死连接数据库就能成功开启驼峰法。

解决方法:

增加如下配置


/**

    * 使 application.properties配置生效,如果不主动配置,由于@Order配置顺序不同,将导致配置不能及时生效 多数据源配置驼峰法生效

    * @return 数据源

    */

   @Bean

   @ConfigurationProperties(prefix="mybatis.configuration")

   public org.apache.ibatis.session.Configuration globalConfiguration() {

       return new org.apache.ibatis.session.Configuration();

   }

   

   /**

    * 创建会话工厂。

    *

    * @param dataSource 数据源

    * @return 会话工厂

    */

   @Bean(name="sqlSessionFactory")

   public SqlSessionFactory getSqlSessionFactory(@Qualifier("dataSource") DataSource dataSource,org.apache.ibatis.session.Configuration config) {

       SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

       bean.setDataSource(dataSource);

       bean.setConfiguration(config);

       try {

           return bean.getObject();

       } catch (Exception e) {

           e.printStackTrace();

           return null;

       }

   }

application.properties  mybatis的congfig加载到类中,再注入到SqlSessionFactoryBean中,问题解决。




向大家推荐《Activiti工作流实战教程》:https://xiaozhuanlan.com/activiti
1

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交