「建议收藏」:深入浅出剖析Mybatis-Plus配置,看完你学会了吗
ztj100 2025-01-06 16:30 13 浏览 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; // 在数据库表中是不存在的
}
最后
大家看完有什么不懂的可以在下方留言讨论,最后觉得文章对你有帮助的话记得点个赞哦,点点关注不迷路,每天都有新鲜的干货分享!
相关推荐
- 如何将数据仓库迁移到阿里云 AnalyticDB for PostgreSQL
-
阿里云AnalyticDBforPostgreSQL(以下简称ADBPG,即原HybridDBforPostgreSQL)为基于PostgreSQL内核的MPP架构的实时数据仓库服务,可以...
- Python数据分析:探索性分析
-
写在前面如果你忘记了前面的文章,可以看看加深印象:Python数据处理...
- C++基础语法梳理:算法丨十大排序算法(二)
-
本期是C++基础语法分享的第十六节,今天给大家来梳理一下十大排序算法后五个!归并排序...
- C 语言的标准库有哪些
-
C语言的标准库并不是一个单一的实体,而是由一系列头文件(headerfiles)组成的集合。每个头文件声明了一组相关的函数、宏、类型和常量。程序员通过在代码中使用#include<...
- [深度学习] ncnn安装和调用基础教程
-
1介绍ncnn是腾讯开发的一个为手机端极致优化的高性能神经网络前向计算框架,无第三方依赖,跨平台,但是通常都需要protobuf和opencv。ncnn目前已在腾讯多款应用中使用,如QQ,Qzon...
- 用rust实现经典的冒泡排序和快速排序
-
1.假设待排序数组如下letmutarr=[5,3,8,4,2,7,1];...
- ncnn+PPYOLOv2首次结合!全网最详细代码解读来了
-
编辑:好困LRS【新智元导读】今天给大家安利一个宝藏仓库miemiedetection,该仓库集合了PPYOLO、PPYOLOv2、PPYOLOE三个算法pytorch实现三合一,其中的PPYOL...
- C++特性使用建议
-
1.引用参数使用引用替代指针且所有不变的引用参数必须加上const。在C语言中,如果函数需要修改变量的值,参数必须为指针,如...
- Qt4/5升级到Qt6吐血经验总结V202308
-
00:直观总结增加了很多轮子,同时原有模块拆分的也更细致,估计为了方便拓展个管理。把一些过度封装的东西移除了(比如同样的功能有多个函数),保证了只有一个函数执行该功能。把一些Qt5中兼容Qt4的方法废...
- 到底什么是C++11新特性,请看下文
-
C++11是一个比较大的更新,引入了很多新特性,以下是对这些特性的详细解释,帮助您快速理解C++11的内容1.自动类型推导(auto和decltype)...
- 掌握C++11这些特性,代码简洁性、安全性和性能轻松跃升!
-
C++11(又称C++0x)是C++编程语言的一次重大更新,引入了许多新特性,显著提升了代码简洁性、安全性和性能。以下是主要特性的分类介绍及示例:一、核心语言特性1.自动类型推导(auto)编译器自...
- 经典算法——凸包算法
-
凸包算法(ConvexHull)一、概念与问题描述凸包是指在平面上给定一组点,找到包含这些点的最小面积或最小周长的凸多边形。这个多边形没有任何内凹部分,即从一个多边形内的任意一点画一条线到多边形边界...
- 一起学习c++11——c++11中的新增的容器
-
c++11新增的容器1:array当时的初衷是希望提供一个在栈上分配的,定长数组,而且可以使用stl中的模板算法。array的用法如下:#include<string>#includ...
- C++ 编程中的一些最佳实践
-
1.遵循代码简洁原则尽量避免冗余代码,通过模块化设计、清晰的命名和良好的结构,让代码更易于阅读和维护...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- node卸载 (33)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- exceptionininitializererror (33)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)