1、SqlUtil

Former-commit-id: a1a423563128c66a83caef1a90666756c61267d1
master
wangbing 5 years ago
parent 52403a3b92
commit 608502b7bc

@ -47,7 +47,7 @@ public class ClassUtil {
* @param pojoClass POJO * @param pojoClass POJO
* @return * @return
*/ */
public static Method getMethod(String name, Class<?> pojoClass) throws RuntimeException { public static Method getMethod(String name, Class<?> pojoClass) {
String getMethodName = "get" + StringUtil.upperFirstWord(name); String getMethodName = "get" + StringUtil.upperFirstWord(name);
try { try {
return pojoClass.getMethod(getMethodName); return pojoClass.getMethod(getMethodName);
@ -73,7 +73,7 @@ public class ClassUtil {
try { try {
return pojoClass.getMethod(setMethodName, type); return pojoClass.getMethod(setMethodName, type);
} catch (Exception e) { } catch (Exception e) {
return null; throw new RuntimeException("can not find[" + name + "]method");
} }
} }

@ -0,0 +1,125 @@
package ${basePackage}.frame.utils;
import ${basePackage}.frame.auth.LocalData;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class SqlUtil {
public static void exec(String sql) {
try {
SqlSessionFactory factory = LocalData.getBean(SqlSessionFactory.class);
SqlSession sqlSession = factory.openSession(true);
Connection connection = sqlSession.getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
statement.execute();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void insert(String sql) {
exec(sql);
}
public static int delete(String sql) {
return update(sql);
}
public static int update(String sql) {
int result = 0;
try {
SqlSessionFactory factory = LocalData.getBean(SqlSessionFactory.class);
SqlSession sqlSession = factory.openSession(true);
Connection connection = sqlSession.getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
result = statement.executeUpdate();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public static <T> List<T> select(String sql, Class<T> t) {
List<T> result = new ArrayList<>();
try {
SqlSessionFactory factory = LocalData.getBean(SqlSessionFactory.class);
SqlSession sqlSession = factory.openSession(true);
Connection connection = sqlSession.getConnection();
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
T instance = t.newInstance();
Field[] fields = t.getDeclaredFields();
for (Field field : fields) {
Method method = ClassUtil.setMethod(field.getName(), t, field.getType());
if (field.getType() == String.class) {
String v = resultSet.getString(field.getName());
method.invoke(instance, v);
} else if (field.getType() == Boolean.class || field.getType() == boolean.class) {
boolean v = resultSet.getBoolean(field.getName());
method.invoke(instance, v);
} else if (field.getType() == Byte.class || field.getType() == byte.class) {
byte v = resultSet.getByte(field.getName());
method.invoke(instance, v);
} else if (field.getType() == Short.class || field.getType() == short.class) {
short v = resultSet.getShort(field.getName());
method.invoke(instance, v);
} else if (field.getType() == Character.class || field.getType() == char.class) {
short v = resultSet.getShort(field.getName());
method.invoke(instance, (char) v);
} else if (field.getType() == Integer.class || field.getType() == int.class) {
int v = resultSet.getInt(field.getName());
method.invoke(instance, v);
} else if (field.getType() == Long.class || field.getType() == long.class) {
long v = resultSet.getLong(field.getName());
method.invoke(instance, v);
} else if (field.getType() == Float.class || field.getType() == float.class) {
float v = resultSet.getFloat(field.getName());
method.invoke(instance, v);
} else if (field.getType() == Double.class || field.getType() == double.class) {
double v = resultSet.getDouble(field.getName());
method.invoke(instance, v);
} else if (field.getType() == Byte[].class || field.getType() == byte[].class) {
byte[] v = resultSet.getBytes(field.getName());
method.invoke(instance, v);
} else if (field.getType() == Date.class) {
Date v = resultSet.getDate(field.getName());
method.invoke(instance, v);
} else {
String v = resultSet.getString(field.getName());
method.invoke(instance, v);
}
}
result.add(instance);
}
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return result;
}
}
Loading…
Cancel
Save

Powered by TurnKey Linux.