- 浏览: 3362391 次
- 性别:
- 来自: 珠海
文章分类
- 全部博客 (1633)
- Java (250)
- Android&HTML5 (111)
- Struts (10)
- Spring (236)
- Hibernate&MyBatis (115)
- SSH (49)
- jQuery插件收集 (55)
- Javascript (145)
- PHP (77)
- REST&WebService (18)
- BIRT (27)
- .NET (7)
- Database (105)
- 设计模式 (16)
- 自动化和测试 (19)
- Maven&Ant (43)
- 工作流 (36)
- 开源应用 (156)
- 其他 (16)
- 前台&美工 (119)
- 工作积累 (0)
- OS&Docker (83)
- Python&爬虫 (28)
- 工具软件 (157)
- 问题收集 (61)
- OFbiz (6)
- noSQL (12)
最新评论
-
HEZR曾嶸:
你好博主,这个不是很理解,能解释一下嘛//左边+1,上边+1, ...
java 两字符串相似度计算算法 -
天使建站:
写得不错,可以看这里,和这里的这篇文章一起看,有 ...
jquery 遍历对象、数组、集合 -
xue88ming:
很有用,谢谢
@PathVariable映射出现错误: Name for argument type -
jnjeC:
厉害,困扰了我很久
MyBatis排序时使用order by 动态参数时需要注意,用$而不是# -
TopLongMan:
非常好,很实用啊。。
PostgreSQL递归查询实现树状结构查询
原文 http://3131854.blog.51cto.com/3121854/1147446
mybatis使用拦截器显示sql,使用druid配置连接信息 http://www.cnblogs.com/babyhhcsy/p/4500884.html
@Intercepts( {
@Signature(method = "query", type = Executor.class, args = {
MappedStatement.class, Object.class, RowBounds.class,
ResultHandler.class }),
@Signature(method = "prepare", type = StatementHandler.class, args = { Connection.class }) })
@Intercepts标记了这是一个Interceptor,然后在@Intercepts中定义了两个@Signature,即两个拦截点。第一个@Signature我们定义了该Interceptor将拦截Executor接口中参数类型为MappedStatement、Object、RowBounds和ResultHandler的query方法;第二个@Signature我们定义了该Interceptor将拦截StatementHandler中参数类型为Connection的prepare方法。
这次项目中要求把所有mybatis所执行的sql都记录到一个文件中,下面是自己写的一个插件(源是参考网上一个重写mybatis分页插件的例子):
1.配置插件(在mybatis的配置文件里mybatis.xml配置自己写的这个插件(拦截器)):
2.MyBatisSQLInterceptor的内容:
mybatis使用拦截器显示sql,使用druid配置连接信息 http://www.cnblogs.com/babyhhcsy/p/4500884.html
@Intercepts( {
@Signature(method = "query", type = Executor.class, args = {
MappedStatement.class, Object.class, RowBounds.class,
ResultHandler.class }),
@Signature(method = "prepare", type = StatementHandler.class, args = { Connection.class }) })
@Intercepts标记了这是一个Interceptor,然后在@Intercepts中定义了两个@Signature,即两个拦截点。第一个@Signature我们定义了该Interceptor将拦截Executor接口中参数类型为MappedStatement、Object、RowBounds和ResultHandler的query方法;第二个@Signature我们定义了该Interceptor将拦截StatementHandler中参数类型为Connection的prepare方法。
这次项目中要求把所有mybatis所执行的sql都记录到一个文件中,下面是自己写的一个插件(源是参考网上一个重写mybatis分页插件的例子):
1.配置插件(在mybatis的配置文件里mybatis.xml配置自己写的这个插件(拦截器)):
<plugins> <!-- mybatis写出sql记录控件(拦截器) --> <plugin interceptor="com.zqgame.interceptors.MyBatisSQLInterceptor"> <!-- 自己写的那个拦截器 --> <property name="dialect" value="mysql"/> <!-- mysql的方言 --> </plugin> </plugins>
2.MyBatisSQLInterceptor的内容:
package com.zqgame.interceptors; import java.io.File; import java.sql.Connection; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Properties; import net.sf.json.JSONObject; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.time.DateFormatUtils; import org.apache.ibatis.executor.statement.StatementHandler; /*import org.apache.ibatis.mapping.BoundSql;*/ import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.reflection.MetaObject; import com.zqgame.common.Constant; /** * 拦截StatementHandler里的 prepare方法把执行的sql进行记录到文件里 * @author panguixiang * */ @Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }) }) public class MyBatisSQLInterceptor implements Interceptor { public Object intercept(Invocation invocation) throws Throwable { StatementHandler statementHandler = (StatementHandler) invocation.getTarget(); MetaObject metaStatementHandler = MetaObject.forObject(statementHandler); String originalSql = (String) metaStatementHandler.getValue("delegate.boundSql.sql");//获得sql /* * if(log.isDebugEnabled()){ log.debug("生成分页SQL : "+boundSql.getSql());* }*/ File sqlFile = null; List<String> lines = null; @SuppressWarnings("unchecked") HashMap<String, String> mapParam = (HashMap<String, String>) metaStatementHandler.getValue("delegate.boundSql.parameterObject"); synchronized (this) { sqlFile = new File(Constant.SQLFILE.concat( DateFormatUtils.format(new Date(), "yyyyMMdd")).concat("-sql.txt"));/*此处是构造sql文件名称,那个Constant.SQLFILE是自己配的一个常量内容比如为:d:\\,可以随便写*/ lines = new ArrayList<String>(); lines.add(originalSql.replaceAll("\n", "").replaceAll("\t", "").replaceAll(" +", " ")); JSONObject jsonObject = JSONObject.fromObject(mapParam); lines.add(jsonObject.toString()); FileUtils.writeLines(sqlFile, "utf-8", lines, true);//将sqlString 写入文件 } return invocation.proceed(); } public Object plugin(Object target) { return Plugin.wrap(target, this); } public void setProperties(Properties properties) { // TODO Auto-generated method stub } }
发表评论
-
spring配置事物的方式:注解和aop配置
2016-05-14 00:26 4057参考: Spring AOP中pointcut express ... -
Mybatis分库分表扩展插件
2016-05-12 15:47 1585http://fangjialong.iteye.com/bl ... -
spring+mybatis+atomikos 实现JTA事务
2016-05-11 22:00 5470sping配置多个数据源 不同用户操作不同数据库 http:/ ... -
ibatis扩展支持主键生成的方法(非数据库方式)
2016-04-13 22:01 939http://blog.csdn.net/warison200 ... -
ibatis 动态 Mapped Statement
2016-03-31 17:02 819http://www.cnblogs.com/lcngu/p/ ... -
分布式事务管理
2016-03-31 16:43 993http://my.oschina.net/pingpangk ... -
ibatis中动态查询表返回用resultClass="java.util.HashMap" 的问题
2016-03-11 10:49 1078http://blog.csdn.net/dyllove98/ ... -
Mybatis传多个参数(三种解决方案)
2016-03-07 17:46 1401http://my.oschina.net/ydsakyclg ... -
mybatis --mapper配置文件中大于小于怎么处理
2016-03-04 14:21 2323http://blog.csdn.net/nich002/ar ... -
Mybatis 数据库物理分页插件 PageHelper
2015-11-28 01:09 2743http://www.cnblogs.com/digdeep/ ... -
Druid 的SQL翻译功能如何使用
2015-11-27 21:40 1925Druid是阿里巴巴开源平台上的一个项目,整个项目由数据库连接 ... -
[MyBatis]mapperLocations属性通配符的使用
2015-11-26 10:49 6162http://blog.csdn.net/szwangdf/a ... -
Hibernate Validation使用示例及讲解
2015-11-22 21:06 2702http://wdmcygah.iteye.com/blog/ ... -
MyBatis排序时使用order by 动态参数时需要注意,用$而不是#
2015-11-17 17:54 20008http://blog.csdn.net/weibing_hu ... -
SpringMVC + Mybatis + Shiro 权限整合
2015-11-14 14:06 8697详细见参考文章: 基于Spring + Spring MVC ... -
MyBatis3 的Mapper XML 文件详解
2015-11-13 11:54 1310MyBatis3 的Mapper XML 文件http://m ... -
HibernateDao.java
2015-11-10 15:55 1128http://my.oschina.net/lizy/blog ... -
Hibernate envers开发指南
2015-10-24 00:44 1192http://www.blogjava.net/xmatthe ... -
Spring+Ibatis数据库水平分库
2015-09-13 21:15 1257Spring+Ibatis数据库水平分库 http://blo ... -
mybatis 功能 beetl 实现 beetlsql
2015-08-26 11:23 2038http://www.oschina.net/p/beetls ...
相关推荐
只需要在springboot的配置文件做简单的配置,mybatis拦截器将SQL中所有参数自动做了填充。拦截器监控慢SQL并将完整的可执行的SQL语句打印在日志文件中,复制该SQL语句即可在数据库工具中执行。 使用方法: 找到你...
MyBatis拦截器分页与动态修改SQL及其参数值 提取SQL Like 字段
1.网上搜索了很多,几乎都是能修改sql, 但是修改后的sql不生效,还是执行原来的sql. 2.这个版本亲测可以生效。 3.支持分页查询
通过Mybatis拦截器自动定位慢SQL并记录日志
将日志输出的sql语句中的问号 ? 替换成真正的参数值。 通过 "Tools -> MyBatis Log Plugin" 菜单或快捷键 "Ctrl+Shift+Alt+O" 启用。 点击窗口左边的 "Filter" 按钮,可以过滤不想要输出的sql语句。 点击窗口左边...
主要给大家介绍了关于mybatis拦截器实现通用权限字段添加的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用mybatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
通过mybatis的拦截器,实现为所有sql(或指定sql) 统一添加查询条件,譬如通过线程变量传递某参数(日期),来实现对指定参数的数据筛选,而不需要在每个查询前,手动将该条件注入到查询中。因该资料网络较少,故特此...
Mybatis中执行String类型的自己拼写的sql,不执行配置文件中的sql mybatis sql connection mybatis中使用sql,不实用配置文件中的sql实例
Mybatis 如何防止 sql 注入?mybatis 拦截器了解过吗,应用场景是什么答案 Mybatis 如何防止 sql 注入?mybatis 拦截器了解过吗,应用场景是什么答案
mybatis拦截器的完整实现,test.sql是数据库测试脚本,主要目的是生成mybatis最终执行的sql语句,并打印出来,方便调试。 基于此,可以实现自动化分页。
06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo...
Mybatis拦截器记录数据更新历史记录到MongoDB的源码,另外需要配置拦截器到mybatis配置文件中。
NULL 博文链接:https://zhanghteye.iteye.com/blog/2373231
使用mybatis的拦截器功能实现分页操作,使分页代码在整个项目中通用,减少代码冗余。
Mybatis 如何防止 sql 注入?mybatis 拦截器了解过吗,应用场景是什么.详情介绍Mybatis 如何防止 sql 注入?mybatis 拦截器了解过吗,应用场景是什么.详情介绍Mybatis 如何防止 sql 注入?mybatis 拦截器了解过吗,...
Mybatis 中使用插件,需要实现接口org.apache.ibatis.plugin.Interceptor,如下所示: public interface Interceptor { Object intercept(Invocation invocation) throws Throwable; default Object plugin...
Mybatis执行SQL语句的方式