之前是完成了mycat的配置,现在来思考怎么把mycat与web项目结合。
公司里的大佬写了一个demo,我就负责研究好了以后用在项目里。
基本的逻辑是,页面上获取租户的信息(就是schema的名字),后台获取保存,Mybatis拦截器拦截sql语句添加前缀。
获取租户信息
前端填写信息,登录请求时放到header里。
ajax请求时,在beforeSend的函数里添加header。
现在前端数据好了,开始后端。
我们需要给请求加一个过滤器,实现HandlerInterceptor,
接口有三个方法:
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception{} public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {} public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {}
preHandle()在业务处理器处理请求之前被调用,我们这里就需要用到它。(其他的你们自己再去学习学习,我懒我就不bb了)
过滤器是需要配置的哈,不熟的同学别忘了。
然后你就可以去试试你的过滤器配置的对不对了,能不能从header里拿你前端设置好的tenant。