MyBatis学习笔记(三)mybatis-config.xml都有哪些配置之typeAliases与plugins

类型别名(typeAlicases)

在mybatis-config.xml配置文件中加入类描述

  
        
    

或者添加要扫描的包名

    
       
        
    

 并在实体类添加注解@Alias

package org.mybatis.example;

import org.apache.ibatis.type.Alias;

@Alias("User")
public class User {

    public User(String userName) {
        this.userName = userName;
    }

    public User() {
    }

    private String userName;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

然后在映射文件xml中就可以用别名了

插件(plugins)

在mybatis-config.xml配置文件中加入插件类的配置

    
        
        
    

只需实现 Interceptor 接口,并指定想要拦截的方法签名即可,目前项目中遇到的是拿这个插件功能做了查询分页

package org.mybatis.example;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;

import java.util.Properties;

@Intercepts({@Signature(
        type= Executor.class,
        method = "query",
        args = {MappedStatement.class,Object.class})})
public class ExamplePlugin implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}

 

你可能感兴趣的:(MyBatis学习笔记)