Minidao 自定義攔截器
===
### 1.MiniDao配置文件–攔截器配置
[1]、MiniDaoBeanScannerConfigurer 增加攔截器參數emptyInterceptor
```
<!-- Minidao攔截器配置參數 -->
<property name="emptyInterceptor" ref="minidaoInterceptor"></property>
```
[2]、spring配置文件,配置minidao 攔截器bean
```
<!-- minidao攔截器 -->
<bean name="minidaoInterceptor" class="examples.interceptor.MinidaoInterceptor"></bean>
```
### 2.MiniDao攔截器代碼 – 自定義示例
**說明:**自定義攔截器需要實現接口EmptyInterceptor,實現onInsert和onUpdate方法
**參考代碼:**
```
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.jeecgframework.minidao.aspect.EmptyInterceptor;
import org.springframework.stereotype.Service;
/**
* minidao攔截器實現【自動填充:創建人,創建時間,修改人,修改時間】
*/
@Service
public class MinidaoInterceptor implements EmptyInterceptor {
@Override
public boolean onInsert(Field[] fields, Object obj) {
Map<Object, Object> map = new HashMap<Object, Object>();
for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
String fieldName = fields[j].getName();
if ("createDate".equals(fieldName)) {
map.put("createDate", new Date());
}
if ("createBy".equals(fieldName)) {
map.put("createBy", "scott");
}
}
try {
//回寫Value值
setFieldValue(map, obj);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean onUpdate(Field[] fields, Object obj) {
Map<Object, Object> map = new HashMap<Object, Object>();
for (int j = 0; j < fields.length; j++) {
fields[j].setAccessible(true);
String fieldName = fields[j].getName();
if ("updateBy".equals(fieldName)) {
map.put("updateBy", "scott");
}
if ("updateDate".equals(fieldName)) {
map.put("updateDate", new Date());
}
}
try {
//回寫Value值
setFieldValue(map, obj);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 設置bean 屬性值
*
* @param map
* @param bean
* @throws Exception
*/
private static void setFieldValue(Map<Object, Object> map, Object bean) throws Exception {
Class<?> cls = bean.getClass();
Method methods[] = cls.getDeclaredMethods();
Field fields[] = cls.getDeclaredFields();
for (Field field : fields) {
String fldtype = field.getType().getSimpleName();
String fldSetName = field.getName();
String setMethod = pareSetName(fldSetName);
if (!checkMethod(methods, setMethod)) {
continue;
}
if(!map.containsKey(fldSetName)){continue;}
Object value = map.get(fldSetName);
Method method = cls.getMethod(setMethod, field.getType());
if (null != value) {
if ("String".equals(fldtype)) {
method.invoke(bean, (String) value);
} else if ("Double".equals(fldtype)) {
method.invoke(bean, (Double) value);
} else if ("int".equals(fldtype)) {
int val = Integer.valueOf((String) value);
method.invoke(bean, val);
}else{
method.invoke(bean, value);
}
}
}
}
/**
* 拼接某屬性set 方法
*
* @param fldname
* @return
*/
private static String pareSetName(String fldname) {
if (null == fldname || "".equals(fldname)) {
return null;
}
String pro = "set" + fldname.substring(0, 1).toUpperCase() + fldname.substring(1);
return pro;
}
/**
* 判斷該方法是否存在
*
* @param methods
* @param met
* @return
*/
private static boolean checkMethod(Method methods[], String met) {
if (null != methods) {
for (Method method : methods) {
if (met.equals(method.getName())) {
return true;
}
}
}
return false;
}
}
```
- 前言
- MiniDao的介紹
- Springboot 快速集成minidao
- springmvc 快速集成minidao
- Minidao 技術架構
- Minidao Vs Mybatis
- MiniDao簡介及特征
- MiniDao簡介及特征
- MiniDao SQL分離寫法
- Minidao SQL注解寫法
- Minidao SQL參數用法
- MiniDao主鍵策略
- 數據庫支持類型
- MiniDao安裝及配置
- Minidao安裝
- Minidao配置
- 自定義攔截器
- MiniDao基本概念
- Minidao基本理念
- DAO定義
- DAO注解
- SQL條件語法
- 實戰技巧篇
- minidao條件like寫法
- 參數格式化工具類:DaoFormat
- 批量查詢寫法