MyBatisPuls配置详解

1年前 (2022) 程序员胖胖胖虎阿
188 0 0

MyBatisPuls配置详解

在MP中有大量的配置,其中有一部分是Mybatis原生的配置,另一部分是MP的配置,详情:https://mybatis.plus/confifig/

关于配置环境下的使用位置:

Spring Boot环境下进行配在application.properties核心配置文件中

Spring MVC环境下进行配置在spring_MVC.config.xml核心配置文件中:

1、基本配置

Maven 多模块项目的扫描路径需以 classpath*: 开头 (即加载多个 jar 包下的 XML 文件),而classpath:只是访问resources包下的文件

  1. confifigLocation

    MyBatis 配置文件位置,如果您有单独的 MyBatis 配置,请将其路径配置到 confifigLocation 中。 MyBatis

    Confifiguration 的具体内容请参考MyBatis 官方文档

    Spring Boot环境下进行配:

    mybatis-plus.config-location = classpath:mybatis-config.xml
    

    Spring MVC环境下进行配置:

    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml"/> 
    
    </bean>
    

    前提是得有这个mybatis-config.xml核心配置文件

  2. mapperLocations

    MyBatis Mapper 所对应的 XML 文件位置,如果您在 Mapper 中有自定义方法(XML 中有自定义实现),需要进行该配置,告诉 Mapper 所对应的 XML 文件位置。

    Spring Boot环境下配置:

    mybatis-plus.mapper-locations = classpath*:mybatis/*.xml
    

    Spring MVC环境下进行配置:

    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> <property name="mapperLocations" value="classpath*:mybatis/*.xml"/> 
    
    </bean>
    

    通过扫描映射包下的xml文件来获取到里面的mapper语句进行执行

  3. typeAliasesPackage

    MyBaits 别名包扫描路径,通过该属性可以给包中的类注册别名,注册后在 Mapper 对应的 XML 文件中可以直接使用类名,而不用使用全限定的类名(即 XML 中调用的时候不用包含包名)。

    Spring Boot环境下配置:

    mybatis-plus.type-aliases-package = cn.itcast.mp.pojo
    

    Spring MVC环境下进行配置:

    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> <property name="typeAliasesPackage" value="com.baomidou.mybatisplus.samples.quickstart.entity"/> 
    
    </bean>
    

    设置别名后就不要再使用全限定名称了,可以直接使用定义的类名

2、进阶配置

本部分(Confifiguration)的配置大都为 MyBatis 原生支持的配置,这意味着您可以通过 MyBatis XML 配置文件的形式进行配置。

  1. mapUnderscoreToCamelCase【驼峰映射】

    是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射。

    注意:

    此属性在 MyBatis 中原默认值为 false,在 MyBatis-Plus 中,此属性也将用于生成最终的 SQL 的 select body如果您的数据库命名符合规则无需使用 @TableField 注解指定数据库字段名

    Spring Boot环境下配置:

    #关闭自动驼峰映射,该参数不能和mybatis-plus.config-location同时存在 
    mybatis-plus.configuration.map-underscore-to-camel-case=false
    

    因为mapUnderscoreToCamelCase也可以在mybatis-config.xml配置文件中进行配置

  2. cacheEnabled

    全局地开启或关闭配置文件中的所有映射器已经配置的任何缓存,默认为 true。

    Spring Boot环境下配置:

    mybatis-plus.configuration.cache-enabled=false
    

3、DB 策略配置

  1. idType

    类型: com.baomidou.mybatisplus.annotation.IdType

    默认值: ID_WORKER

    全局默认主键类型,设置后,即可省略实体对象中的@TableId(type = IdType.AUTO)配置。

    Spring Boot环境下配置:

    mybatis-plus.global-config.db-config.id-type=auto
    

    mybatis-plus主键生产策列大全如下:

    /*
     * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
     * <p>
     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
     * use this file except in compliance with the License. You may obtain a copy of
     * the License at
     * <p>
     * https://www.apache.org/licenses/LICENSE-2.0
     * <p>
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     * License for the specific language governing permissions and limitations under
     * the License.
     */
    package com.baomidou.mybatisplus.annotation;
    
    import lombok.Getter;
    
    /**
     * 生成ID类型枚举类
     *
     * @author hubin
     * @since 2015-11-10
     */
    @Getter
    public enum IdType {
        /**
         * 数据库ID自增
         */
        AUTO(0),
        /**
         * 该类型为未设置主键类型
         */
        NONE(1),
        /**
         * 用户输入ID
         * <p>该类型可以通过自己注册自动填充插件进行填充</p>
         */
        INPUT(2),
    
        /* 以下3种类型、只有当插入对象ID 为空,才自动填充。 */
        /**
         * 全局唯一ID (idWorker)
         */
        ID_WORKER(3),
        /**
         * 全局唯一ID (UUID)
         */
        UUID(4),
        /**
         * 字符串全局唯一ID (idWorker 的字符串表示)
         */
        ID_WORKER_STR(5);
    
        private final int key;
    
        IdType(int key) {
            this.key = key;
        }
    }
    

    SpringMVC环境下配置:

    <!--这里使用MP提供的sqlSessionFactory,完成了Spring与MP的整合--> 
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource"/> <property name="globalConfig"> 
        <bean class="com.baomidou.mybatisplus.core.config.GlobalConfig"> <property name="dbConfig"> 
            <bean class="com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig"> 
                <property name="idType" value="AUTO"/>
            </bean> 
            </property> 
        </bean> 
        </property> 
    </bean>
    
  2. tablePrefifix

    表名前缀,全局配置后可省略@TableName()配置。

    Spring Boot环境下配置:

    mybatis-plus.global-config.db-config.table-prefix=tb_
    

    SpringMVC配置环境下:

    <!--这里使用MP提供的sqlSessionFactory,完成了Spring与MP的整合--> 
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource"/> <property name="globalConfig"> 
        <bean class="com.baomidou.mybatisplus.core.config.GlobalConfig"> <property name="dbConfig"> 
            <bean class="com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig"> 
                <property name="idType" value="AUTO"/>
                <property name="tablePrefix" value="tb_"/>
            </bean> 
            </property> 
        </bean> 
        </property> 
    </bean>
    

    <property name=“tablePrefix” value=“tb_”/>

版权声明:程序员胖胖胖虎阿 发表于 2022年11月12日 下午6:24。
转载请注明:MyBatisPuls配置详解 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...