• MybatisPlus基础篇配置


    一. 基础配置

    1. configLocation

    MyBatis 配置文件位置,如果您有单独的 MyBatis 配置,请将其路径配置到 configLocation 中。 MyBatisConfiguration 的具体内容请参考MyBatis 官方文档。

    SpringBoot:

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

    Spring MVC:

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

    2. mapperLocations

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

    SpringBoot:

    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>

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

    3. typeAliasesPackage

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

    SpringBoot:

    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>

    二、进阶配置

    1. mapUnderscoreToCamelCase

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

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

    示例:

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

    2. cacheEnabled

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

    示例:

    mybatis-plus.configuration.cache-enabled =false 

    三、DB策略配置

    1. idType

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

    SpringBoot示例:

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

    Spring MVC示例:

    <!-- 这里使用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. tablePrefix

    表名前缀,全局配置后可省略 @TableName()配置。默认值:null

    SpringBoot示例:

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

    Spring MVC示例:

    <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

    四、配置参考

    # 指定全局的配置文件
    #mybatis-plus.config-location=classpath:mybatis-config.xml
    # 指定Mapper.xml文件的路径
    mybatis-plus.mapper-locations = classpath*:mybatis/*.xml
    # 实体对象的扫描包
    mybatis-plus.type-aliases-package = cn.itcast.mp.pojo
    # 禁用自定的驼峰映射
    mybatis-plus.configuration.map-underscore-to-camel-case=true
    # 禁用缓存
    mybatis-plus.configuration.cache-enabled=false
    # 全局的id生成策略
    mybatis-plus.global-config.db-config.id-type=auto
    # 全局的表名的前缀
    mybatis-plus.global-config.db-config.table-prefix=tb_
  • 相关阅读:
    service structure flowchart with full stack functionality in a brife map
    service structure flowchart [mobile to server via HTTP RESTful API and TCP/IP in a map]
    service structure flowchart [mobile to server via TCP/IP protocol]
    service structure flowchart [mobile to server via HTTP RESTful API]
    Python -- machine learning, neural network -- PyBrain 机器学习 神经网络
    Deploying OpenFire for IM (instant message) service (TCP/IP service) with database MySQL , client Spark on linux部署OpenFire IM 消息中间件服务
    做自己想做的事,成为想成为的人(转)
    PCB工艺要求
    看了三遍,沉默了五天
    850和940红外发射管的区别
  • 原文地址:https://www.cnblogs.com/elian91/p/15844470.html
Copyright © 2020-2023  润新知