All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
net.eudu.db.interceptlogic.MyInterceptor Maven / Gradle / Ivy
package net.eudu.db.interceptlogic;
import net.eudu.db.SqlUtils;
import net.eudu.db.annotation.*;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
//@Component
//@MyDBInterceptor
public class MyInterceptor implements MethodInterceptor, AfterAdvice, Serializable {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object[] parameters = invocation.getArguments();
Method method = invocation.getMethod();
Annotation[] annotations = method.getDeclaredAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof MyCount) {
MyCount myCount = (MyCount) annotation;
String value = myCount.value();
SqlUtils sqlUtils = new SqlUtils(value, parameters);
return count(sqlUtils.getSql(), sqlUtils.getArgs());
} else if (annotation instanceof MyBatchUpdate) {
MyBatchUpdate myBatchUpdate = (MyBatchUpdate) annotation;
String value = myBatchUpdate.value();
List list = null;
if (parameters.length != 0) {
list = (List) parameters[0];
}
return batchUpdate(value, list);
} else if (annotation instanceof MyInsertWithKey) {
MyInsertWithKey myInsertWithKey = (MyInsertWithKey) annotation;
String value = myInsertWithKey.value();
SqlUtils sqlUtils = new SqlUtils(value, parameters);
return updateWithKey(sqlUtils.getSql(), sqlUtils.getArgs());
} else if (annotation instanceof MyUpdate) {
MyUpdate myUpdate = (MyUpdate) annotation;
String value = myUpdate.value();
SqlUtils sqlUtils = new SqlUtils(value, parameters);
return update(sqlUtils.getSql(), sqlUtils.getArgs());
} else if (annotation instanceof MyQueryForList) {
MyQueryForList myQueryForList = (MyQueryForList) annotation;
String value = myQueryForList.value();
SqlUtils sqlUtils = new SqlUtils(value, parameters);
return queryForList(sqlUtils.getSql(), sqlUtils.getArgs());
} else if (annotation instanceof MyQueryForMap) {
MyQueryForMap myQueryForMap = (MyQueryForMap) annotation;
String value = myQueryForMap.value();
SqlUtils sqlUtils = new SqlUtils(value, parameters);
return queryForMap(sqlUtils.getSql(), sqlUtils.getArgs());
} else {
throw new RuntimeException("不支持的注解");
}
}
return "wp";
}
public Object queryForList(String sql, Object... args) {
if (isArrayNullOrEmpty(args))
return jdbcTemplate.queryForList(sql);
System.out.println(Arrays.asList(args));
return jdbcTemplate.queryForList(sql, args);
}
public Object queryForMap(String sql, Object... args) {
if (isArrayNullOrEmpty(args))
return jdbcTemplate.queryForMap(sql);
return jdbcTemplate.queryForMap(sql, args);
}
public Object count(String sql, Object... args) {
Map map = (Map) queryForMap(sql, args);
Object[] objects = new Object[1];
map.forEach(new BiConsumer() {
@Override
public void accept(Object o, Object o2) {
objects[0] = o2;
}
});
return objects[0];
}
public int update(String sql, Object... args) {
if (isArrayNullOrEmpty(args))
return jdbcTemplate.update(sql);
return jdbcTemplate.update(sql, args);
}
public int[] batchUpdate(String sql, List batchArgs) {
return jdbcTemplate.batchUpdate(sql, batchArgs);
}
public Object updateWithKey(String sql, Object... args) {
PreparedStatementCreator psc = new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement pst = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
for (int i = 0; i < args.length; i++) {
pst.setObject(i + 1, args[i]);
}
return pst;
}
};
KeyHolder generatedKeyHolde = new GeneratedKeyHolder();
int res = jdbcTemplate.update(psc, generatedKeyHolde);
Number number = generatedKeyHolde.getKey();
return number.longValue();
}
private boolean isArrayNullOrEmpty(Object[] objects) {
if (objects == null || objects.length == 0)
return true;
return false;
}
public static void main(String[] args) {
SqlUtils sqlUtil = new SqlUtils("select * from users where ( id in ((?1) )?1 and ) ?1 (name = ?2)?2",
new Object[]{null, 2});
}
}