• Mybatis-Plus:配置


    Mybatis-Plus:配置

    在MP中有⼤量的配置,其中有⼀部分是Mybatis原⽣的配置,另⼀部分是MP的配置,详情见官网:https://baomidou.com/

    MyBatis-Plus_19

    1、基本配置

    1.1、configLocation

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

    Spring Boot:

    mybatis-plus.config-location=classpath:sqlMapConfig.xml
    
    • 1

    Spring MVC:

    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="configLocation" value="classpath:sqlMapConfig.xml">property>
    bean>
    
    • 1
    • 2
    • 3

    1.2、mapperLocations

    MyBatis Mapper 所对应的 XML ⽂件位置,如果在 Mapper 中有⾃定义⽅法(XML 中有⾃定义实现),需要进⾏该配置,告诉 Mapper 所对应的 XML ⽂件位置。

    Spring Boot:

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

    Spring MVC:

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

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

    测试:

    UserMapper.xml:

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.tao.mapper.UserMapper">
        <select id="findById" resultType="com.tao.pojo.User">
            select * from user where id = #{id}
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在UserMapper接口中添加findById方法:

    /*
        自定义findById方法
     */
    public User findById(Long id);
    
    • 1
    • 2
    • 3
    • 4

    测试⽤例:

    /*
        测试自定义方法findById
     */
    @Test
    public void findById(){
        User user = userMapper.findById(12L);
        System.out.println(user);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    结果:

    User(id=12, name=小七, age=20, mail=null, address=null)
    
    • 1

    1.3、typeAliasesPackage

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

    Spring Boot:

    mybatis-plus.type-aliases-package=com.tao.pojo
    
    • 1

    Spring MVC:

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

    2、进阶配置

    本部分(Configuration)的配置⼤都为 MyBatis 原⽣⽀持的配置,这意味着您可以通过 MyBatis XML 配置⽂件的形式进⾏配置。

    2.1、mapUnderscoreToCamelCase

    • 类型: boolean
    • 默认值: true

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

    注意:

    此属性在 MyBatis 中原默认值为 false,在 MyBatis-Plus 中,此属性也将⽤于⽣成最终的 SQL 的 select body

    如果您的数据库命名符合规则⽆需使⽤ @TableField 注解指定数据库字段名

    示例(SpringBoot):

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

    2.2、cacheEnabled

    • 类型: boolean
    • 默认值: true

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

    示例:

    mybatis-plus.configuration.cache-enabled=false
    
    • 1

    3、DB 策略配置

    3.1、idType

    • 类型: com.baomidou.mybatisplus.annotation.IdType
    • 默认值: ID_WORKER

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

    示例:

    SpringBoot:

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

    SpringMVC:

    
    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3.2、tablePrefix

    • 类型: String
    • 默认值: null

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

    SpringBoot:

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

    SpringMVC:

    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    sql性能优化以及性能测试
    第六章(9): 高阶函数
    Docker指定容器使用内存
    聚焦私域营销降本提效,国联股份与火山引擎数智平台展开合作
    如何利用Web应用防火墙应对未知威胁
    docker 安装 zipkin,监控服务并持久化到mysql
    mysql(创建和管理表)
    第1章 Java基础(一)
    VMware安装Ubuntu以及利用vscode远程Ubuntu
    hdfs shell操作助记总结
  • 原文地址:https://blog.csdn.net/qq_37829947/article/details/125887981