zoukankan      html  css  js  c++  java
  • SpringBoot中mybatis配置自动转换驼峰标识没有生效

    一、前言

      需要知道的是,在Java开发中,实体一般采取陀骆峰的形式命名,而数据库表设计,会采取下划线的方式。数据库大小写是否敏感的问题与系统有关。通常认为Linux环境下,大小写是敏感的,Window环境下大小写是不敏感的。而开发过程中一般在window环境下进行,而生产环境一般使用Linux环境,因而数据库设计采用下划线方式,避免大小写敏感的问题。当然,window环境下大小写敏感也是可以通过设置的,目前即使mysql设置大小写不敏感,但是实际情况下还是会区分大小写的。大小写敏感的问题尚且存在争议。但无论如何,通过下划线的方式是可以避免大小敏感的问题,在规范的表设计,都需要采取下划线的方式。

    二、问题

      mybatis在没有配置类的情况下,只需要在SpringBoot的配置文件添加 map-underscore-to-camel-case=true 便可解决自动转换的问题。当有配置类的情况下,会优先使用配置类,导致SpringBoot的配置文件无法加载到配置类中,这个时候,需要手动将配置文件信息加载到配置类中。

    三、application.yml配置文件

    server:
      port: 8001
    spring:
      datasource:
        name: druidDataSource
        type: com.alibaba.druid.pool.DruidDataSource
        druid:
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://localhost:3306/db_security?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true
          username: root
          password: 123456
          filters: config,stat,wall,log4j
          max-active: 100
          initial-size: 10
          max-wait: 60000
          min-idle: 1
          time-between-eviction-runs-millis: 60000
          min-eviction-runs-millis: 30000
          validation-query: selct 'x'
          test-while-idle: true
          test-on-borrow: false
          test-on-return: false
          pool-prepared-statements: true
          max-open-prepared-statements: 60
          max-pool-prepared-statement-pre-connection-size: 20
    pagehelper:
      helperDialect: mysql
      reasonable: true
      supportMethodArguments: true
    params: count=countSql
    mybatis:
      mapper-locations: classpath:sqlmap/*.xml
      type-aliases-package: com.security.admin.model
      configuration:
        mapUnderscoreToCamelCase: true
        map-underscore-to-camel-case: true

    四、MybatisConfig

    @Configuration
    @MapperScan("com.security.admin.dao")    // 扫描DAO
    public class MybatisConfig {
      @Autowired
      private DataSource dataSource;
    
      @Bean
      @Primary
      public SqlSessionFactory sqlSessionFactory(org.apache.ibatis.session.Configuration configuration) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setTypeAliasesPackage("com.security.admin.model");    // 扫描Model
        
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sessionFactory.setMapperLocations(resolver.getResources("classpath*:**/sqlmap/*.xml"));    // 扫描映射文件
        configuration.setMapUnderscoreToCamelCase(true);
        sessionFactory.setConfiguration(configuration);
        return sessionFactory.getObject();
      }
    
      @Bean
      @ConfigurationProperties(prefix = "mybatis.configuration")
      public org.apache.ibatis.session.Configuration configuration(){
        return new org.apache.ibatis.session.Configuration();
      }
    }

    Reference:

     剑握在手, SpringBoot中mybatis配置自动转换驼峰标识没有生效, https://www.cnblogs.com/flying607/p/8473075.html

  • 相关阅读:
    HTML 与 HTML 页面之间动态传值的问题
    maven 导入本地项目(JQuery中的绝杀 $("表单").serialize() 可以自动提交表格数据)+邮件发送+通用的Servlet写法
    linux服务器nginx的卸载
    http协议
    所谓的批量删除
    查看本机ssh公钥,生成公钥
    centos7 redis5编译安装
    linux没有ll等命令的解决办法
    Linux 安装python3.7.0
    CentOS7 安装mysql
  • 原文地址:https://www.cnblogs.com/ryelqy/p/14119062.html
Copyright © 2011-2022 走看看