diff --git a/src/main/resources/modules/SpringBoot/java/frame/utils/ClassUtil.java b/src/main/resources/modules/SpringBoot/java/frame/utils/ClassUtil.java index 4013577e..823562f8 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/utils/ClassUtil.java +++ b/src/main/resources/modules/SpringBoot/java/frame/utils/ClassUtil.java @@ -47,7 +47,7 @@ public class ClassUtil { * @param pojoClass POJO对象 * @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); try { return pojoClass.getMethod(getMethodName); @@ -73,7 +73,7 @@ public class ClassUtil { try { return pojoClass.getMethod(setMethodName, type); } catch (Exception e) { - return null; + throw new RuntimeException("can not find[" + name + "]method"); } } diff --git a/src/main/resources/modules/SpringBoot/java/frame/utils/SqlUtil.java b/src/main/resources/modules/SpringBoot/java/frame/utils/SqlUtil.java new file mode 100644 index 00000000..5b18a890 --- /dev/null +++ b/src/main/resources/modules/SpringBoot/java/frame/utils/SqlUtil.java @@ -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 List select(String sql, Class t) { + List 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; + } + +}