mycat整合springboot和mybatis

2018-09-30 09:25:00    admin    2142    原创

1、mycat在linux下安装

http://dl.mycat.io/1.6-RELEASE/

安装的目录

cd usr/lib/mycat/

tar -zxvf  Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz

配置一下环境变量

cd etc/profice

export MYCAT_HOME=/usr/lib/mycat

source profice

赋予一下权限

chmod -R mycat

启动mycat

cd usr/lib/mycat/bin

启动命令

./mycat start

关闭命令

./mycat stop

2、mycat设置

server.xml,开启端口号,设置一下登录账号和密码,见附件中


3、设置一下datanode,见附件

schema.xml

3、mybatis添加拦截器对sql语句进行预处理


import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.util.Properties;

@Intercepts(value = {
         @Signature(type = StatementHandler.class,
                method = "prepare",
                args = {Connection.class,Integer.class})})
public class MyBatisInterceptor implements Interceptor {
    private static final String preState="/*!mycat:datanode=";
    private static final String afterState="*/";
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler statementHandler=(StatementHandler)invocation.getTarget();
        MetaObject metaStatementHandler= SystemMetaObject.forObject(statementHandler);
        Object object=null;
        //分离代理对象链
        while(metaStatementHandler.hasGetter("h")){
            object=metaStatementHandler.getValue("h");
            metaStatementHandler= SystemMetaObject.forObject(object);
        }
        String sql=(String)metaStatementHandler.getValue("delegate.boundSql.sql");
        String node=getRequest();
        if(node!=null) {
            if(node.equals("2")){
                sql = preState + "hmm" + afterState + sql;
            }else if(node.equals("1")){
                sql = preState + "sbed" + afterState + sql;
            }else if(node.equals("3")){
                sql = preState + "aupost" + afterState + sql;
            }

        }else{
            sql = preState + "sbed" + afterState + sql;
        }
        metaStatementHandler.setValue("delegate.boundSql.sql",sql);
        Object result = invocation.proceed();
        return result;
    }

    @Override
    public Object plugin(Object target) {

        return Plugin.wrap(target, this);
    }
    @Override
    public void setProperties(Properties properties) {
    }
    public static String  getRequest() {
        HttpServletRequest httpRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = httpRequest.getHeader("orgId");
        if(StringUtils.isBlank(token) ){
            token = httpRequest.getParameter("orgId");
        }
        if(StringUtils.isNotBlank(token)){
            return token;
        }else{
            token = httpRequest.getHeader("Access-Control-Request-Headers");
            if(StringUtils.isNotBlank(token)){
                token=token.replace("content-type","");
                String[] split=token.split(",");
                    token=split[0];
            }

        }
        return token;

    }
}

在mybatis.xml中添加拦截器

  <plugins>
        <plugin interceptor="io.sbed.common.interceptor.MyBatisInterceptor">
        </plugin>
    </plugins>
applicaion.yml中配置如下:

# Tomcat
server:
    tomcat:
        uri-encoding: UTF-8
        max-threads: 1000
        min-spare-threads: 30
    port: 9200

#部署区域,启动后有提示
area: china
spring:
    datasource:
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:8066/SBEDDB?serverTimezone=GMT
        password: root
        username: root



豫ICP备15006410号
蝉知 蝉知4.5.2