百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

「建议收藏」:深入浅出剖析Mybatis-Plus配置,看完你学会了吗

ztj100 2025-01-06 16:30 29 浏览 0 评论

基本配置

configLocation

MyBatis 配置文件位置,如果有单独的 MyBatis 配置,请将其路径配置到 configLocation 中。 MyBatis Configuration 的具体内容请参考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>

mapperLocations

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

# 指定Mapper.xml文件的路径
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 文件)?UserMapper

<?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="cn.com.javakf.mapper.UserMapper">

    <select id="findById" resultType="cn.com.javakf.pojo.User">
        select * from tb_user where id = #{id}
    </select>

</mapper>
public interface UserMapper extends BaseMapper<User> {

    User findById(Long id);

}

测试用例

/**
 * 自定义的方法
 */
@Test
public void testFindById() {
    User user = this.userMapper.findById(2L);
    System.out.println(user);
}

测试结果

[main] [cn.com.javakf.mapper.UserMapper.findById]-[DEBUG] ==>  Preparing: select * from tb_user where id = ? 
[main] [cn.com.javakf.mapper.UserMapper.findById]-[DEBUG] ==> Parameters: 2(Long)
[main] [cn.com.javakf.mapper.UserMapper.findById]-[DEBUG] <==      Total: 1
[main] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@74dbb1ee]
User(id=2, userName=lisi, password=123456, name=李四, age=20, mail=null, address=null)

typeAliasesPackage

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

# 实体对象的扫描包
mybatis-plus.type-aliases-package = cn.com.javakf.pojo

Spring MVC

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

UserMapper

<?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="cn.com.javakf.mapper.UserMapper">

    <select id="findById" resultType="User">
        select * from tb_user where id = #{id}
    </select>

</mapper>

进阶配置

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

mapUnderscoreToCamelCase类型: boolean默认值: true?是否开启自动驼峰命名规则(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

cacheEnabled

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

# 禁用缓存
mybatis-plus.configuration.cache-enabled=false

DB 策略配置

idType

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

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

SpringMVC

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
    <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>

User配置

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User {

    // @TableId(type = IdType.AUTO) // 指定id类型为自增长
    private Long id;
    private String userName;
    @TableField(select = false) // 查询时不返回该字段的值
    private String password;
    private String name;
    private Integer age;
    @TableField(value = "email") // 指定数据表中字段名
    private String mail;
    @TableField(exist = false)
    private String address; // 在数据库表中是不存在的

}

tablePrefix

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

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

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>

User配置

@Data
@NoArgsConstructor
@AllArgsConstructor
//@TableName("tb_user")
public class User {

    @TableId(type = IdType.AUTO) // 指定id类型为自增长
    private Long id;
    private String userName;
    @TableField(select = false) // 查询时不返回该字段的值
    private String password;
    private String name;
    private Integer age;
    @TableField(value = "email") // 指定数据表中字段名
    private String mail;
    @TableField(exist = false)
    private String address; // 在数据库表中是不存在的

}

最后

大家看完有什么不懂的可以在下方留言讨论,最后觉得文章对你有帮助的话记得点个赞哦,点点关注不迷路,每天都有新鲜的干货分享!

相关推荐

sharding-jdbc实现`分库分表`与`读写分离`

一、前言本文将基于以下环境整合...

三分钟了解mysql中主键、外键、非空、唯一、默认约束是什么

在数据库中,数据表是数据库中最重要、最基本的操作对象,是数据存储的基本单位。数据表被定义为列的集合,数据在表中是按照行和列的格式来存储的。每一行代表一条唯一的记录,每一列代表记录中的一个域。...

MySQL8行级锁_mysql如何加行级锁

MySQL8行级锁版本:8.0.34基本概念...

mysql使用小技巧_mysql使用入门

1、MySQL中有许多很实用的函数,好好利用它们可以省去很多时间:group_concat()将取到的值用逗号连接,可以这么用:selectgroup_concat(distinctid)fr...

MySQL/MariaDB中如何支持全部的Unicode?

永远不要在MySQL中使用utf8,并且始终使用utf8mb4。utf8mb4介绍MySQL/MariaDB中,utf8字符集并不是对Unicode的真正实现,即不是真正的UTF-8编码,因...

聊聊 MySQL Server 可执行注释,你懂了吗?

前言MySQLServer当前支持如下3种注释风格:...

MySQL系列-源码编译安装(v5.7.34)

一、系统环境要求...

MySQL的锁就锁住我啦!与腾讯大佬的技术交谈,是我小看它了

对酒当歌,人生几何!朝朝暮暮,唯有己脱。苦苦寻觅找工作之间,殊不知今日之事乃我心之痛,难道是我不配拥有工作嘛。自面试后他所谓的等待都过去一段时日,可惜在下京东上的小金库都要见低啦。每每想到不由心中一...

MySQL字符问题_mysql中字符串的位置

中文写入乱码问题:我输入的中文编码是urf8的,建的库是urf8的,但是插入mysql总是乱码,一堆"???????????????????????"我用的是ibatis,终于找到原因了,我是这么解决...

深圳尚学堂:mysql基本sql语句大全(三)

数据开发-经典1.按姓氏笔画排序:Select*FromTableNameOrderByCustomerNameCollateChinese_PRC_Stroke_ci_as//从少...

MySQL进行行级锁的?一会next-key锁,一会间隙锁,一会记录锁?

大家好,是不是很多人都对MySQL加行级锁的规则搞的迷迷糊糊,一会是next-key锁,一会是间隙锁,一会又是记录锁。坦白说,确实还挺复杂的,但是好在我找点了点规律,也知道如何如何用命令分析加...

一文讲清怎么利用Python Django实现Excel数据表的导入导出功能

摘要:Python作为一门简单易学且功能强大的编程语言,广受程序员、数据分析师和AI工程师的青睐。本文系统讲解了如何使用Python的Django框架结合openpyxl库实现Excel...

用DataX实现两个MySQL实例间的数据同步

DataXDataX使用Java实现。如果可以实现数据库实例之间准实时的...

MySQL数据库知识_mysql数据库基础知识

MySQL是一种关系型数据库管理系统;那废话不多说,直接上自己以前学习整理文档:查看数据库命令:(1).查看存储过程状态:showprocedurestatus;(2).显示系统变量:show...

如何为MySQL中的JSON字段设置索引

背景MySQL在2015年中发布的5.7.8版本中首次引入了JSON数据类型。自此,它成了一种逃离严格列定义的方式,可以存储各种形状和大小的JSON文档,例如审计日志、配置信息、第三方数据包、用户自定...

取消回复欢迎 发表评论: