• 【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实体类完全符合表字段名称,一模一样

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

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

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

    再次测试的结果:

    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的XML文件来配置

    上面的注解@TableField也不需要

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

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

    <?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
    "
    >
        <!--
            dao层的配置:核心是要产生
            Mapper代理类对象
            1.引入数据库配置信息
            2.数据源配置
            3.SqlSessionFactory
            4.产生Mapper接口的代理类对象
        -->
    
        <!-- 1.引入数据库配置信息 -->
        <context:property-placeholder location="classpath:db.properties" system-properties-mode="FALLBACK" />
    
        <!--2.数据源配置 -->
        <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>
    
        <!-- 3.SqlSessionFactory -->
        <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" />
    
            <!-- 加载xxMapper.xml -->
            <property name="mapperLocations">
                <array>
                    <value>classpath:mybatis/mapper/*Mapper.xml</value>
                </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>
    
        <!-- 4.产生Mapper接口的代理类对象 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 需要生成代理类对象的mapper接口包 -->
            <property name="basePackage" value="cn.echo42.mapper" />
            <!-- sqlSessionFactory 的name 用于为代理类中生成SqlSession -->
            <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

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

  • 相关阅读:
    递归
    HDU_oj_2041 超级楼梯
    树与森林——树与森林的遍历
    HUD_oj_2040 亲和数
    HDU_oj_2039 判定三角形
    HDU_oj_2037 今年暑假不AC
    多边形面积
    HDU_oj_2036 改革春风吹满地(多边形面积)
    【转发】【composer】composer 命令行介绍
    【chm】【windows】win7下chm打开不显示内容
  • 原文地址:https://www.cnblogs.com/mindzone/p/13443967.html
Copyright © 2020-2023  润新知