【Mybatis-Plus】Spring整合 驼峰命名设置失效问题

查询时发现这个问题:

DEBUG [main] - Creating a new SqlSession
DEBUG [main] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4ef74c30] was not registered for synchronization because synchronization is not active
DEBUG [main] - Fetching JDBC Connection from DataSource
DEBUG [main] - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/oa?serverTimezone=GMT]
DEBUG [main] - JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@319bc845] will not be managed by Spring
DEBUG [main] - ==>  Preparing: SELECT user_id,user_name,user_password,user_status,user_is_del FROM sys_user WHERE user_id=? DEBUG [main] - ==> Parameters: 3(Integer) DEBUG [main] - <== Total: 1 DEBUG [main] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4ef74c30] null

我们的User实体类完全符合表字段名称,一模一样

【Mybatis-Plus】Spring整合 驼峰命名设置失效问题_第1张图片

到这里我就多半猜到是驼峰自动匹配了,数据库表字段是ab_cdef_ghij这样的命名规范的

Mybatis-Plus直接给Java转换成abCdefGhij这样了

找不到关于MybatisPlus在Spring中是如何取消驼峰配置的,可以做的是改变实体类属性为驼峰符合的命名:

【Mybatis-Plus】Spring整合 驼峰命名设置失效问题_第2张图片

再次测试的结果:

DEBUG [main] - Creating a new SqlSession
DEBUG [main] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d61eb55] was not registered for synchronization because synchronization is not active
DEBUG [main] - Fetching JDBC Connection from DataSource
DEBUG [main] - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/oa?serverTimezone=GMT]
DEBUG [main] - JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@58ffcbd7] will not be managed by Spring
DEBUG [main] - ==>  Preparing: SELECT user_id,user_name,user_password,user_status,user_is_del FROM sys_user WHERE user_id=? 
DEBUG [main] - ==> Parameters: 3(Integer)
DEBUG [main] - <==      Total: 1
DEBUG [main] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d61eb55]
User(userId=3, userName=user02, userPassword=123456, userStatus=1, userIsDel=0)

 

终于在百度各种博客帖子查不到我想要的答案之后,直奔官方文档的解释查看:

https://mp.baomidou.com/config/#configuration

【Mybatis-Plus】Spring整合 驼峰命名设置失效问题_第3张图片

也就是说我需要使用原生Mybatis的XML文件来配置

上面的注解@TableField也不需要

 

然后再往上翻,基本配置中找到了:

【Mybatis-Plus】Spring整合 驼峰命名设置失效问题_第4张图片

可是我就算这样配置了也照样没用啊???

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
"
>
    

    
    <context:property-placeholder location="classpath:db.properties" system-properties-mode="FALLBACK" />

    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    bean>

    
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">

        <property name="dataSource" ref="dataSource" />
        <property name="globalConfig" ref="globalConfig" />
        <property name="configuration" ref="configuration" />

        
        <property name="mapperLocations">
            <array>
                <value>classpath:mybatis/mapper/*Mapper.xmlvalue>
            array>
        property>

        
        <property name="plugins">
            <array>
                <bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"/>
            array>
        property>
    bean>

    <bean id="configuration" class="com.baomidou.mybatisplus.core.MybatisConfiguration">
       <property name="mapUnderscoreToCamelCase" value="false" />
    bean>

    
    <bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
        
        <property name="dbConfig" ref="dbConfig" />
    bean>

    <bean id="dbConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig">
        <property name="idType" value="AUTO" />
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="basePackage" value="cn.echo42.mapper" />
        
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    bean>
beans>

测试结果:

DEBUG [main] - Creating a new SqlSession
DEBUG [main] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d61eb55] was not registered for synchronization because synchronization is not active
DEBUG [main] - Fetching JDBC Connection from DataSource
DEBUG [main] - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/oa?serverTimezone=GMT]
DEBUG [main] - JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@58ffcbd7] will not be managed by Spring
DEBUG [main] - ==>  Preparing: SELECT user_id,user_name,user_password,user_status,user_is_del FROM sys_user WHERE user_id=? 
DEBUG [main] - ==> Parameters: 3(Integer)
DEBUG [main] - <==      Total: 1
DEBUG [main] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d61eb55]
null

 

暂时使用驼峰规则来使用了。。。

 

你可能感兴趣的:(【Mybatis-Plus】Spring整合 驼峰命名设置失效问题)