mybatis插件开发

mybatis插件开发    
                mybatis的四大核心部件:
                    1. Executor:拦截执行器的方法。
                    2. ParameterHandler:拦截参数的处理。
                    3. ResultHandler:拦截结果集的处理。
                    4. StatementHandler:拦截Sql语法构建的处理


                    Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
                    ParameterHandler (getParameterObject, setParameters)
                    ResultSetHandler (handleResultSets, handleOutputParameters)
                    StatementHandler (prepare, parameterize, batch, update, query)

 创建拦截器需要实现的三大接口:
                    Java8.0版本以前的接口
                    Object intercept(Invocation invocation) throws Throwable;
                    Object plugin(Object target);
                    void setProperties(Properties properties);

 Java8.0版本以后的接口
                     Object intercept(Invocation invocation) throws Throwable;
                     
                     //default只能使用在接口方法中,当使用该修饰符的时候,方法被视为普通方法,可以直接写方法体。
                      default Object plugin(Object target) {
                        return Plugin.wrap(target, this);
                      }

                      default void setProperties(Properties properties) {
                        // NOP
                      }

1.setProperties方法:
                    mybatis-config.xm1中配置:
                        <plugin interceptor="com.plugin.mybatis.MyInterceptor">
                         <property name="username" value="root"/>
                         <property name="password" value="123"/>
                    </plugin>
                    
                    方法中获取参数:properties.getProperty("username");

2.plugin方法:
                    该方法的作用是就是让mybatis判断,是否要进行拦截,然后做出决定是否生成一个代理。
                    public Object plugin(Object target) {
                        if (target instanceof StatementHandler) {
                            return Plugin.wrap(target, this);
                        }
                        return target;
                    }

3.intercept(Invocation invocation)方法:
            user.executeQuery()    //查询方法
            user.executeUpdate()//添加、修改、删除
            SQL预处理对象:parapareStatement 是Statement的子类
                parapareStatement可以有效的防止SQL注入、

接口中的方法都是抽象方法
 规定:能不能有方法体?不能
 重新抽象方法,implement实现它

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值