mybatis怎么进行使用
发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,mybatis怎么进行使用,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。1.数据源DataSource
千家信息网最后更新 2025年02月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安全错误
数据库的锁怎样保障安全
网络安全审查重点评估
学校网站 网络安全
有一台服务器做什么好
抄车牌号上传数据库如何赚钱
哈尔滨小熊软件开发公司雷
服务器能插pcie网卡
账本服务器
怎么看网页服务器在哪
设计网络安全审计
软件开发学不会怎么办
神武新区服务器开多少升120
数据库使用场景
初中毕业学软件开发好就业吗
连接登录数据库服务器的命令
河南通用软件开发均价
服务器jar
建立sql数据库的步骤
兴创服务器是什么意思
未来有前景的郑州软件开发
电影电视数据库分享平台
网络通信与网络安全总结
网络技术为人类带来哪些好处
清空数据库事务日志
服务器停止的游戏怎么玩
外网服务器下载游戏
移动应用开发与软件开发
网络安全意识的总结
数据库远程用户
我的世界服务器领地地图怎么建
数据库坏块英语作文