| | |
| | | package cc.mrbird.febs.common.interceptor; |
| | | |
| | | import cc.mrbird.febs.common.entity.BaseEntity; |
| | | import cc.mrbird.febs.modules.api.entity.MemberEntity; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.ibatis.executor.Executor; |
| | | import org.apache.ibatis.mapping.MappedStatement; |
| | | import org.apache.ibatis.mapping.SqlCommandType; |
| | | import org.apache.ibatis.plugin.*; |
| | | import org.apache.ibatis.session.defaults.DefaultSqlSession; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Properties; |
| | | |
| | | /** |
| | | * mybatis拦截器,自动注入创建人、创建时间、修改人、修改时间 |
| | | * |
| | | * @author xxx |
| | | * @date 2020-05-13 |
| | | **/ |
| | | @Slf4j |
| | | @Component |
| | | @Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})}) |
| | | public class MybatisInterceptor implements Interceptor { |
| | | @Override |
| | | public Object intercept(Invocation invocation) throws Throwable { |
| | | MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; |
| | | SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType(); |
| | | |
| | | Object parameter = invocation.getArgs()[1]; |
| | | if (parameter == null) { |
| | | return invocation.proceed(); |
| | | } |
| | | |
| | | if (SqlCommandType.INSERT == sqlCommandType) { |
| | | if (parameter instanceof DefaultSqlSession.StrictMap) { |
| | | Map map = (Map) parameter; |
| | | List list = (List) map.get("list"); |
| | | for (Object o : list) { |
| | | injectForInsert(o); |
| | | } |
| | | } else { |
| | | injectForInsert(parameter); |
| | | } |
| | | } |
| | | |
| | | if (SqlCommandType.UPDATE == sqlCommandType) { |
| | | if (parameter instanceof DefaultSqlSession.StrictMap) { |
| | | Map map = (Map) parameter; |
| | | List list = (List) map.get("list"); |
| | | for (Object o : list) { |
| | | injectForUpdate(o); |
| | | } |
| | | } else { |
| | | injectForUpdate(parameter); |
| | | } |
| | | } |
| | | |
| | | return invocation.proceed(); |
| | | } |
| | | |
| | | @Override |
| | | public Object plugin(Object o) { |
| | | return Plugin.wrap(o, this); |
| | | } |
| | | |
| | | @Override |
| | | public void setProperties(Properties properties) { |
| | | |
| | | } |
| | | |
| | | public void injectForInsert(Object o) { |
| | | MemberEntity member = getLoginUser(); |
| | | if (o instanceof BaseEntity) { |
| | | BaseEntity baseEntity = (BaseEntity) o; |
| | | if (member != null) { |
| | | String by = member.getPhone(); |
| | | baseEntity.setCreateBy(by); |
| | | baseEntity.setUpdateBy(by); |
| | | } else { |
| | | baseEntity.setCreateBy("system"); |
| | | baseEntity.setUpdateBy("system"); |
| | | } |
| | | baseEntity.setCreateTime(new Date()); |
| | | baseEntity.setUpdateTime(new Date()); |
| | | } |
| | | } |
| | | |
| | | public void injectForUpdate(Object o) { |
| | | MemberEntity member = getLoginUser(); |
| | | if (o instanceof BaseEntity) { |
| | | BaseEntity baseEntity = (BaseEntity) o; |
| | | if (member != null) { |
| | | String by = member.getPhone(); |
| | | baseEntity.setUpdateBy(by); |
| | | } else { |
| | | baseEntity.setUpdateBy("system"); |
| | | } |
| | | baseEntity.setUpdateTime(new Date()); |
| | | } |
| | | } |
| | | |
| | | private MemberEntity getLoginUser() { |
| | | ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| | | if (attributes == null) { |
| | | return null; |
| | | } |
| | | |
| | | HttpServletRequest request = attributes.getRequest(); |
| | | return (MemberEntity) request.getSession().getAttribute("member"); |
| | | } |
| | | } |
| | | //package cc.mrbird.febs.common.interceptor; |
| | | // |
| | | //import cc.mrbird.febs.common.entity.BaseEntity; |
| | | //import lombok.extern.slf4j.Slf4j; |
| | | //import org.apache.ibatis.executor.Executor; |
| | | //import org.apache.ibatis.mapping.MappedStatement; |
| | | //import org.apache.ibatis.mapping.SqlCommandType; |
| | | //import org.apache.ibatis.plugin.*; |
| | | //import org.apache.ibatis.session.defaults.DefaultSqlSession; |
| | | //import org.springframework.stereotype.Component; |
| | | //import org.springframework.web.context.request.RequestContextHolder; |
| | | //import org.springframework.web.context.request.ServletRequestAttributes; |
| | | // |
| | | //import javax.servlet.http.HttpServletRequest; |
| | | //import java.util.Date; |
| | | //import java.util.List; |
| | | //import java.util.Map; |
| | | //import java.util.Properties; |
| | | // |
| | | ///** |
| | | // * mybatis拦截器,自动注入创建人、创建时间、修改人、修改时间 |
| | | // * |
| | | // * @author xxx |
| | | // * @date 2020-05-13 |
| | | // **/ |
| | | //@Slf4j |
| | | ////@Component |
| | | ////@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})}) |
| | | //public class MybatisInterceptor implements Interceptor { |
| | | // @Override |
| | | // public Object intercept(Invocation invocation) throws Throwable { |
| | | // MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; |
| | | // SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType(); |
| | | // |
| | | // Object parameter = invocation.getArgs()[1]; |
| | | // if (parameter == null) { |
| | | // return invocation.proceed(); |
| | | // } |
| | | // |
| | | // if (SqlCommandType.INSERT == sqlCommandType) { |
| | | // if (parameter instanceof DefaultSqlSession.StrictMap) { |
| | | // Map map = (Map) parameter; |
| | | // List list = (List) map.get("list"); |
| | | // for (Object o : list) { |
| | | // injectForInsert(o); |
| | | // } |
| | | // } else { |
| | | // injectForInsert(parameter); |
| | | // } |
| | | // } |
| | | // |
| | | // if (SqlCommandType.UPDATE == sqlCommandType) { |
| | | // if (parameter instanceof DefaultSqlSession.StrictMap) { |
| | | // Map map = (Map) parameter; |
| | | // List list = (List) map.get("list"); |
| | | // for (Object o : list) { |
| | | // injectForUpdate(o); |
| | | // } |
| | | // } else { |
| | | // injectForUpdate(parameter); |
| | | // } |
| | | // } |
| | | // |
| | | // return invocation.proceed(); |
| | | // } |
| | | // |
| | | // @Override |
| | | // public Object plugin(Object o) { |
| | | // return Plugin.wrap(o, this); |
| | | // } |
| | | // |
| | | // @Override |
| | | // public void setProperties(Properties properties) { |
| | | // |
| | | // } |
| | | // |
| | | // public void injectForInsert(Object o) { |
| | | // MemberEntity member = getLoginUser(); |
| | | // if (o instanceof BaseEntity) { |
| | | // BaseEntity baseEntity = (BaseEntity) o; |
| | | // if (member != null) { |
| | | // String by = member.getPhone(); |
| | | // baseEntity.setCreateBy(by); |
| | | // baseEntity.setUpdateBy(by); |
| | | // } else { |
| | | // baseEntity.setCreateBy("system"); |
| | | // baseEntity.setUpdateBy("system"); |
| | | // } |
| | | // baseEntity.setCreateTime(new Date()); |
| | | // baseEntity.setUpdateTime(new Date()); |
| | | // } |
| | | // } |
| | | // |
| | | // public void injectForUpdate(Object o) { |
| | | // MemberEntity member = getLoginUser(); |
| | | // if (o instanceof BaseEntity) { |
| | | // BaseEntity baseEntity = (BaseEntity) o; |
| | | // if (member != null) { |
| | | // String by = member.getPhone(); |
| | | // baseEntity.setUpdateBy(by); |
| | | // } else { |
| | | // baseEntity.setUpdateBy("system"); |
| | | // } |
| | | // baseEntity.setUpdateTime(new Date()); |
| | | // } |
| | | // } |
| | | // |
| | | // private MemberEntity getLoginUser() { |
| | | // ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| | | // if (attributes == null) { |
| | | // return null; |
| | | // } |
| | | // |
| | | // HttpServletRequest request = attributes.getRequest(); |
| | | // return (MemberEntity) request.getSession().getAttribute("member"); |
| | | // } |
| | | //} |