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

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

yuyutoo 2024-10-12 00:03 9 浏览 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; // 在数据库表中是不存在的

}

最后

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

相关推荐

ETCD 故障恢复(etc常见故障)

概述Kubernetes集群外部ETCD节点故障,导致kube-apiserver无法启动。...

在Ubuntu 16.04 LTS服务器上安装FreeRADIUS和Daloradius的方法

FreeRADIUS为AAARadiusLinux下开源解决方案,DaloRadius为图形化web管理工具。...

如何排查服务器被黑客入侵的迹象(黑客 抓取服务器数据)

---排查服务器是否被黑客入侵需要系统性地检查多个关键点,以下是一份详细的排查指南,包含具体命令、工具和应对策略:---###**一、快速初步检查**####1.**检查异常登录记录**...

使用 Fail Ban 日志分析 SSH 攻击行为

通过分析`fail2ban`日志可以识别和应对SSH暴力破解等攻击行为。以下是详细的操作流程和关键分析方法:---###**一、Fail2ban日志位置**Fail2ban的日志路径因系统配置...

《5 个实用技巧,提升你的服务器安全性,避免被黑客盯上!》

服务器的安全性至关重要,特别是在如今网络攻击频繁的情况下。如果你的服务器存在漏洞,黑客可能会利用这些漏洞进行攻击,甚至窃取数据。今天我们就来聊聊5个实用技巧,帮助你提升服务器的安全性,让你的系统更...

聊聊Spring AI Alibaba的YuQueDocumentReader

序本文主要研究一下SpringAIAlibaba的YuQueDocumentReaderYuQueDocumentReader...

Mac Docker环境,利用Canal实现MySQL同步ES

Canal的使用使用docker环境安装mysql、canal、elasticsearch,基于binlog利用canal实现mysql的数据同步到elasticsearch中,并在springboo...

RustDesk:开源远程控制工具的技术架构与全场景部署实战

一、开源远程控制领域的革新者1.1行业痛点与解决方案...

长安汽车一代CS75Plus2020款安装高德地图7.5

不用破解原车机,一代CS75Plus2020款,安装车机版高德地图7.5,有红绿灯读秒!废话不多讲,安装步骤如下:一、在拨号状态输入:在电话拨号界面,输入:*#518200#*(进入安卓设置界面,...

Zookeeper使用详解之常见操作篇(zookeeper ui)

一、Zookeeper的数据结构对于ZooKeeper而言,其存储结构类似于文件系统,也是一个树形目录服务,并通过Key-Value键值对的形式进行数据存储。其中,Key由斜线间隔的路径元素构成。对...

zk源码—4.会话的实现原理一(会话层的基本功能是什么)

大纲1.创建会话...

Zookeeper 可观测性最佳实践(zookeeper能够确保)

Zookeeper介绍ZooKeeper是一个开源的分布式协调服务,用于管理和协调分布式系统中的节点。它提供了一种高效、可靠的方式来解决分布式系统中的常见问题,如数据同步、配置管理、命名服务和集群...

服务器密码错误被锁定怎么解决(服务器密码错几次锁)

#服务器密码错误被锁定解决方案当服务器因多次密码错误导致账户被锁定时,可以按照以下步骤进行排查和解决:##一、确认锁定状态###1.检查账户锁定状态(Linux)```bash#查看账户锁定...

zk基础—4.zk实现分布式功能(分布式zk的使用)

大纲1.zk实现数据发布订阅...

《死神魂魄觉醒》卡死问题终极解决方案:从原理到实战的深度解析

在《死神魂魄觉醒》的斩魄刀交锋中,游戏卡死犹如突现的虚圈屏障,阻断玩家与尸魂界的连接。本文将从技术架构、解决方案、预防策略三个维度,深度剖析卡死问题的成因与应对之策,助力玩家突破次元壁障,畅享灵魂共鸣...

取消回复欢迎 发表评论: