Mybatis-Plus:配置(基本配置、进阶配置、DB策略配置)、classpath*: 和classpath:区别

Mybatis-Plus中有大量的配置,其中有一部分是Mybatis原生的配置,另一部分是Mybatis-Plus`的配置,详情Mybatis-Plus官方配置文档

1. 基本配置


1.1 configLocation(配置文件的位置)


1.1.1 单独的 MyBatis 配置

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

在这里插入图片描述


1.1.2 Spring Boot

这里的代码承接自上一节
在这里插入图片描述

# 指定全局的配置文件
mybatis-plus.config-location=classpath:mybatis-config.xml

我们来配置一下分页的插件,看看配置文件是否生效。

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--    配置分页插件-->
    <plugins>
        <plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"/>
    </plugins>
</configuration>

编写测试文件
在这里插入图片描述

package com.tian.springbootmybatisplus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tian.mapper.UserMapper;
import com.tian.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class SpringbootMybatisplusApplicationTests {
    @Autowired
    UserMapper userMapper;

    // 测试分页查询
    @Test
    public void testSelectPage() {

        Page<User> page = new Page<>(1, 3); //查询第一页, 查询3条数据

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //设置查询条件
        wrapper.like("email", "itcast");

        IPage<User> iPage = this.userMapper.selectPage(page, wrapper);
        System.out.println("数据总条数: " + iPage.getTotal());
        System.out.println("数据总页数: " + iPage.getPages());
        System.out.println("当前页数: " + iPage.getCurrent());

        // 把拿到的数据打印出来
        List<User> records = iPage.getRecords();
        for (User record : records) {
            System.out.println(record);
        }
    }
}

运行结果:
在这里插入图片描述
成果拿到了分页的结果,说明配置文件生效了。


1.1.3 Spring MVC

    <!--这里使用MP提供的sqlSessionFactory,完成了Spring与MP的整合-->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="mybatis-config.xml"/>
    </bean>

在这里插入图片描述


1.2 mapperLocations(MyBatis Mapper 所对应的 XML 文件位置)

在有些时候,Mybatis-Plus提供的BaseMapper接口里面的方法可能不太够用,这时候就需要我们自己去定义查询的方法。

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


1.2.1 Spring Boot

这里的代码承接自上一节

# 指定Mapper.xml文件的路径
mybatis-plus.mapper-locations=classpath*:mapper/*.xml

在这里插入图片描述

上面配置的XML 文件位置就可以找到这里的XML文件。
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tian.mapper.UserMapper">

    <select id="findById2" resultType="com.tian.pojo.User">
        select *
        from tb_user
        where id = #{id}
    </select>

</mapper>

1.2.2 Spring MVC

在这里插入图片描述

    <!--这里使用MP提供的sqlSessionFactory,完成了Spring与MP的整合-->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="mapperLocations" value="classpath*:mybatis/*.xml"/>
    </bean>

1.3 typeAliasesPackage(MyBaits-Plus 别名包扫描路径 不区分大小写)

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


1.3.1 Spring Boot

# 实体对象的扫描包 (配置包别名)
mybatis-plus.type-aliases-package=com.tian.pojo

在这里插入图片描述

然后去改一下UserMapper.xml
在这里插入图片描述


1.3.2 Spring MVC

在这里插入图片描述

    <!--这里使用MP提供的sqlSessionFactory,完成了Spring与MP的整合-->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="pojo"/>
    </bean>

2. 进阶配置

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


2.1 mapUnderscoreToCamelCase(自动驼峰命名规则(camel case)映射)

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

  • 类型boolean
  • 默认值trueMybatis-Plus默认是开启映射的),此属性在 MyBatis 中原默认值为 false

2.1.1 Spring Boot

在这里插入图片描述
在这里插入图片描述

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

2.2 cacheEnabled(是否开启缓存)

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

  • 类型: boolean
  • 默认值: true

2.2.2 Spring Boot

# 关闭缓存 该参数也不能和mybatis-plus.config-location同时存在
mybatis-plus.configuration.cache-enabled=false

在这里插入图片描述


3. DB 策略配置


3.1 idType(配置主键类型)

对主键类型不太清楚的同学可以查看这篇博客
类型: com.baomidou.mybatisplus.annotation.IdType
默认值: ID_WORKER

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


3.1.1 Spring Boot

# 全局的id生成策略
mybatis-plus.global-config.db-config.id-type=auto

在这里插入图片描述

配置了这个就可以注释掉@TableId注解了
在这里插入图片描述


3.1.2 Spring MVC

在这里插入图片描述

        <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>

3.2 tablePrefix(表名前缀)

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

  • 类型String
  • 默认值null

说明:

在这里插入图片描述


3.2.1 Spring Boot

在这里插入图片描述

# 全局表名的前缀
mybatis-plus.global-config.db-config.table-prefix=tb_

有了这个注解就不需要@TableName注解,在映射表名的时候自动加上前缀tb_


3.2.2 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>

4. 补充:classpath*:classpath:区别

Maven 多模块项目的扫描路径需以 classpath*: 开头 (即加载多个 jar 包下的 XML 文件)
在这里插入图片描述



  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeJiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值