mybatis支持驼峰自动转换sql吗_SpringBoot中mybatis配置自动转换驼峰标识没有生效
一、前言需要知道的是,在Java开发中,实体一般采取陀骆峰的形式命名,而数据库表设计,会采取下划线的方式。数据库大小写是否敏感的问题与系统有关。通常认为Linux环境下,大小写是敏感的,Window环境下大小写是不敏感的。而开发过程中一般在window环境下进行,而生产环境一般使用Linux环境,因而数据库设计采用下划线方式,避免大小写敏感的问题。当然,window环境下大小写敏感也是可以通过设置
一、前言
需要知道的是,在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 classMybatisConfig {
@AutowiredprivateDataSource dataSource;
@Bean
@Primarypublic SqlSessionFactory sqlSessionFactory(org.apache.ibatis.session.Configuration configuration) throwsException {
SqlSessionFactoryBean sessionFactory= newSqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeAliasesPackage("com.security.admin.model"); //扫描Model
PathMatchingResourcePatternResolver resolver= newPathMatchingResourcePatternResolver();
sessionFactory.setMapperLocations(resolver.getResources("classpath*:**/sqlmap/*.xml")); //扫描映射文件
configuration.setMapUnderscoreToCamelCase(true);
sessionFactory.setConfiguration(configuration);returnsessionFactory.getObject();
}
@Bean
@ConfigurationProperties(prefix= "mybatis.configuration")publicorg.apache.ibatis.session.Configuration configuration(){return neworg.apache.ibatis.session.Configuration();
}
}
Reference:
剑握在手, SpringBoot中mybatis配置自动转换驼峰标识没有生效, https://www.cnblogs.com/flying607/p/8473075.html
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)