mybatis怎么进行使用
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,mybatis怎么进行使用,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。1.数据源DataSource
千家信息网最后更新 2025年12月03日mybatis怎么进行使用
mybatis怎么进行使用,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
1.数据源DataSource
2.mapper.xml文件
3.Dao类的实例化
方法一:原生方法的实例化
自定义一个BaseDao抽象类实现SqlSessionDaoSupport并注入sqlSessionFactory,通过namespace+id找到mapper.xml文件
方法二:spring-mytabis动态代理生成实例化
4.分页插件
4.1自定义分页插件
package com.unilife.commons.utils;import com.unilife.commons.dto.Page;import org.apache.ibatis.executor.ErrorContext;import org.apache.ibatis.executor.ExecutorException;import org.apache.ibatis.executor.statement.BaseStatementHandler;import org.apache.ibatis.executor.statement.RoutingStatementHandler;import org.apache.ibatis.executor.statement.StatementHandler;import org.apache.ibatis.mapping.BoundSql;import org.apache.ibatis.mapping.MappedStatement;import org.apache.ibatis.mapping.ParameterMapping;import org.apache.ibatis.mapping.ParameterMode;import org.apache.ibatis.plugin.*;import org.apache.ibatis.reflection.MetaObject;import org.apache.ibatis.reflection.property.PropertyTokenizer;import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;import org.apache.ibatis.session.Configuration;import org.apache.ibatis.type.TypeHandler;import org.apache.ibatis.type.TypeHandlerRegistry;import org.springframework.util.StringUtils;import java.lang.reflect.Field;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.List;import java.util.Properties;@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }) })public class PagePlugin implements Interceptor { private String dialect = ""; // 数据库方言 private String pageSqlId = ""; // mapper.xml中需要拦截的ID(正则匹配) @Override public Object intercept(Invocation ivk) throws Throwable { if (ivk.getTarget() instanceof RoutingStatementHandler) { RoutingStatementHandler statementHandler = (RoutingStatementHandler) ivk .getTarget(); BaseStatementHandler delegate = (BaseStatementHandler) ReflectHelper .getValueByFieldName(statementHandler, "delegate"); MappedStatement mappedStatement = (MappedStatement) ReflectHelper .getValueByFieldName(delegate, "mappedStatement"); if (mappedStatement.getId().matches(pageSqlId)) { // 拦截需要分页的SQL BoundSql boundSql = delegate.getBoundSql(); Object parameterObject = boundSql.getParameterObject();// 分页SQL
4.2 开源分页插件
com.github.pagehelper
5.补充,可以通过mybatis-config.xml进行configuration对象的配置
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。
参数
对象
数据
实体
属性
实例
方法
插件
数据库
数据源
配置
文件
方言
注释
反射
帮助
生成
清楚
特殊
内容
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
周村企业oa软件开发公司
m26软件开发方法
校园建立健全网络安全责任制
医疗管理软件开发
我的世界服务器叫什么
计算机网络技术主要职位
阿里云服务器上的字母怎么写
关于护苗网络安全小报
elog服务器
软件测试计划数据库测试
生死狙击忘记哪个服务器了怎么办
关于网络安全法的正确说法有
阜阳财务软件开发
逻辑表 数据库怎么写
乡网络安全周宣传报告
税控管理服务器
嘉兴基础网络技术口碑推荐
dhcp服务器设置为自动吗
我的世界服务器必备指令击杀榜
阿里云服务器能玩什么游戏
楚天科技工业互联网
中国铁塔软件开发投资
宁夏银川软件开发公司案例
少年西游记忘记服务器
关于护苗网络安全小报
电脑怎么使用手机上的数据库
怎么做网络安全宣传
软件开发合同要不要签订服务期限
数据库性能维护
中学生网络安全教育专题