mybatis plus 批量保存_mybatis源码分析

原理图:

64e2da085b9b5748544db8592157870d.png

Configuration解析:

Configuration表示配置,该对象中维护了很多mybatis的配置参数;

大致可分为四部分:1.环境变量Environment 2.配置参数;3.缓存集合;4.插件及其他

1.1环境变量Environment

ce442b386b8d3cf03819f13e6bfb859f.png

Environment.java源码

通过Environment源码可以看出,Environment仅维护了一个id 唯一标识,TransactionFactory(事物工厂,负责事物管理,Mybatis提供了两套事物管理

1.org.apache.ibatis.transaction.jdbc 基于jdbc实现事物管理;

2.org.apache.ibatis.transaction.managed 用于扩展第三方事物管理);

和DataSource(数据源)。

1.2配置参数:

Configuration提供了一些默认配置参数,并可通过get/set方法修改参数值;

1.3缓存集合:

Configuration维护了很多缓存容器,如caches,resulrtMaps等’

Configuration初始化

1.api构建;2.xml初始化

9948cf08fcc6c9686288ef614ced6176.png

Configuration初始化

MappendStatement解析:

该对象是mapper.xml在对象中的体现,是整个mybatis框架中最为核心的对象,我们也可以不必通过xml文件来构建该对象,可以直接通过编码方式构建,像最常用的简单的增删改查操作完全可以手动构建mappedStatement对象并加入到mybatis容器中,这样我们就不需要在xml文件中手写CRUD操作了,mybatis-plus框架设计的思想就是鉴于此。

public final class MappedStatement { private String resource; private Configuration configuration; //sql的ID private String id; //尝试影响驱动程序每次批量返回的结果行数和这个设置值相等 private Integer fetchSize; //SQL超时时间 private Integer timeout; //Statement的类型,STATEMENT/PREPARE/CALLABLE private StatementType statementType; //结果集类型,FORWARD_ONLY/SCROLL_SENSITIVE/SCROLL_INSENSITIVE  private ResultSetType resultSetType; //表示解析出来的SQL private SqlSource sqlSource; //缓存 private Cache cache; //已废弃 private ParameterMap parameterMap; //对应的ResultMap private List resultMaps; private boolean flushCacheRequired; private boolean useCache; private boolean resultOrdered; //SQL类型,INSERT/SELECT/DELETE private SqlCommandType sqlCommandType; //和SELECTKEY标签有关 private KeyGenerator keyGenerator; private String[] keyProperties; private String[] keyColumns; private boolean hasNestedResultMaps; //数据库ID,用来区分不同环境 private String databaseId; private Log statementLog; private LanguageDriver lang; //多结果集时 private String[] resultSets; ...}

源码分析:

1.SqlSessionFactory 装配Configuration对象;

2.将配置文件中的mapers注册到configuration的mapperRegistry中

71d2365f463244ce7ab894b23325f74c.png

Mybatis代理模式原理分析:

4e4f8d127af234986ba8d33cc4c7cc01.png
febfb85b0b66a4b7902baee722dabf14.png
01497057da86e8400e00ca33b0044c7d.png
buildSqlSessionFactory方法:XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(), configuration, mapperLocation.toString(), configuration.getSqlFragments());xmlMapperBuilder.parse();

SqlSession提供select/insert/update/delete方法,在旧版本中使用使用SqlSession接口的这些方法,但是新版的Mybatis中就会建议使用Mapper接口的方法。

射器其实就是一个动态代理对象,进入到MapperMethod的execute方法就能简单找到SqlSession的删除、更新、查询、选择方法,从底层实现来说:通过动态代理技术,让接口跑起来,之后采用命令模式,最后还是采用了SqlSession的接口方法(getMapper()方法等到Mapper)执行SQL查询(也就是说Mapper接口方法的实现底层还是采用SqlSession接口方法实现的)。

解析XMLStatementBuilder.java:

将xml文件转换成MappedStatement对象

public void parseStatementNode() {

String id = context.getStringAttribute("id");

String databaseId = context.getStringAttribute("databaseId");

if (!databaseIdMatchesCurrent(id, databaseId, this.requiredDatabaseId)) {

return;

}

Integer fetchSize = context.getIntAttribute("fetchSize");

Integer timeout = context.getIntAttribute("timeout");

String parameterMap = context.getStringAttribute("parameterMap");

String parameterType = context.getStringAttribute("parameterType");

Class> parameterTypeClass = resolveClass(parameterType);

String resultMap = context.getStringAttribute("resultMap");

String resultType = context.getStringAttribute("resultType");

String lang = context.getStringAttribute("lang");

LanguageDriver langDriver = getLanguageDriver(lang);

Class> resultTypeClass = resolveClass(resultType);

String resultSetType = context.getStringAttribute("resultSetType");

StatementType statementType = StatementType.valueOf(context.getStringAttribute("statementType

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的使用Mybatis Plus函数的示例: 假设我们有一个表名为user,包含以下字段:id、name、age、email。 1. 首先,在pom.xml文件中添加Mybatis Plus的依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>latest version</version> </dependency> ``` 2. 在Mapper接口中定义查询方法,例如根据年龄age查询: ``` import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.apache.ibatis.annotations.Param; import java.util.List; public interface UserMapper extends BaseMapper<User> { List<User> selectByAge(@Param("age") Integer age); } ``` 3. 在Service实现类中调用函数查询方法,例如查询年龄大于20的用户: ``` import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import java.util.List; @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Override public List<User> getByAge(Integer age) { QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.gt("age", age); // 使用Mybatis Plus的gt函数,查询年龄大于age的用户 List<User> userList = baseMapper.selectList(queryWrapper); return userList; } } ``` 这样就可以使用Mybatis Plus的函数查询数据了。除了gt函数,Mybatis Plus还提供了很多其他的函数,例如eq、ne、like、between等,具体可以参考官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值