/**
* 数据有效性检查拦截器
* @author marke.huang
* @date 2018/10/29 11:15
*/
@Intercepts({@Signature(
type = Executor.class,
method = "update",
args = {MappedStatement.class, Object.class})})
public class DataValidityCheckInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
// 参数
Object params = args[1];
MapperMethod.ParamMap<?> paramMap = (MapperMethod.ParamMap<?>) params;
// 实体类
Object et = paramMap.get(Constants.ENTITY);
// 反射获取实体类简称
Class<?> clazz = et.getClass().getSuperclass();
String simpleName = clazz.getSimpleName();
// 获取pk字段值
BeanWrapper beanWrapper = new BeanWrapperImpl(et);
Object pkValue = beanWrapper.getPropertyValue("pk");
if (null != pkValue) {
// 查询sql语句,逻辑删除
String querySql = "SELECT COUNT(*) FROM %s WHERE DEL_FLAG = '0' AND PK = %d";
querySql = String.format(querySql, simpleName, pkValue);
// spring获取mybatis查询工具类
QueryExecutor queryExecutor = SpringContextUtils.getBean(QueryExecutor.class);
// 执行查询sql语句
List<Map<String, Object>> list = queryExecutor.genericQuery(sql);
// 判断数据是否存在
if (CollectionUtils.isNotEmpty(list)) {
Object dataCount= list.get(0).values().iterator().next();
if (dataCount != null ) {
String dataCountStr = String.valueOf(dataCount );
if (!"1".equals(dataCountStr )) {
// 抛出数据不存在异常 TODO
}
}
}
}
}
}
/**
* SpringIOC容器工具类
*
* @author marke.huang
* @date 2018/7/11 9:58
*/
@Component("springContextUtil")
public final class SpringContextUtils implements ApplicationContextAware {
private SpringContextUtils() {
}
/**
* Spring上下文对象
*/
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.applicationContext = applicationContext;
}
/**
* 按类型获取托管对象
*
* @param clazz 类型
* @return T 对象
* @author marke.huang
* @date 2018/7/12 9:41
*/
public static <T> T getBean(Class<T> clazz) {
return SpringContextUtils.applicationContext.getBean(clazz);
}
}
/**
* MyBatis 查询工具类
* @author jiangming.huang
* @date 2018/10/31 0031 下午 2:05
*/
@Service
@Slf4j
public class QueryExecutor {
@Autowired
private SqlSessionFactory sessionFactory;
/**
* 执行SQL
*
* @param sql
* @return
*/
public List<Map<String, Object>> genericQuery(String sql) {
if (StringUtils.isEmpty(sql) || !(sql.toLowerCase().trim().startsWith("select") || sql.toLowerCase().trim().startsWith("call"))) {
return null;
}
SqlSession sqlSession = sessionFactory.openSession();
try {
GenericQueryMapper query = sqlSession.getMapper(GenericQueryMapper.class);
return query.query(sql);
} catch (Exception e) {
log.error("执行SQL查询时发生错误.sql == " + sql, e);
} finally {
sqlSession.close();
}
return null;
}
/**
* 执行SQL, SQL 参数的格式参照 MyBatis, 即用 #{param_name}
*
* @param sql
* @param params
* @return
*/
public List<Map<String, Object>> genericQuery(String sql, Object params) {
if (StringUtils.isEmpty(sql) || !(sql.toLowerCase().trim().startsWith("select") || sql.toLowerCase().trim().startsWith("call"))) {
return null;
}
SqlSession sqlSession = sessionFactory.openSession();
try {
GenericQueryMapper query = sqlSession.getMapper(GenericQueryMapper.class);
return query.queryWithParams(sql, params);
} catch (Exception e) {
log.error("执行SQL查询时发生错误.sql == " + sql, e);
} finally {
sqlSession.close();
}
return null;
}
}
/**
* 类功能描述:MyBatis动态执行sql语句
*
* @author marke.huang
* @date 2018/10/31 0031 下午 2:05
*/
public interface GenericQueryMapper {
/**
* 带条件查询
*
* @param sql
* @param params
* @return
*/
@Select("${sql}")
List<Map<String, Object>> queryWithParams(@Param("sql") String sql, @Param("params") Object params);
/**
* 不带条件查询
*
* @param sql
* @return
*/
@Select("${sql}")
List<Map<String, Object>> query(@Param("sql") String sql);
}