在前几篇文章中,有写过封装Jdbc的连接池,今天,来说说如何封装公共方法给大家用。
本博客所有文章皆是原创,欢迎读者阅读,转载时请务必标注出处。
话不多说,直接看代码:
/**
* @author PQF
* @category 根据SQL进行查询返回一个List实体集合
* @param 1.传送一个查询的sql语句
* @param 2.sql中使用占位符(?)所对应的实际值
* @param 3.你要查询的列
* @throws Exception
*/
public static List<Map<String, String>> queryEntityList(
HttpServletRequest request, String sql, List<String> list,
String[] column) throws Exception {
List<Map<String, String>> entityList = new ArrayList<>();
if (sql.isEmpty() || column.length < 1) {
throw new Exception("SQL语句不能为空,并且列的长度不能小于1");
}
Connection conn = getConnection();
PreparedStatement prepareStatement = null;
ResultSet result = null;
try {
prepareStatement = conn.prepareStatement(sql.toString());
if (BaseUtil.ObjectNotNull(list)) {
for (int i = 1; i <= list.size(); i++) {
prepareStatement.setString(i, list.get(i - 1));
}
}
result = prepareStatement.executeQuery();
Map<String, String> mapList = null;
while (result.next()) {
mapList = new HashMap<>();
for (Object col : column) {
mapList.put((String) col,result.getString((String) col));
}
entityList.add(mapList);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
release(conn, prepareStatement, result);
}
return entityList;
}
以上是公共的查询方法返回值类似[{"a"="1","b"="2","c"="3"},{"d"="4","e"="5","f"="6"}]其中getConnection()方法在前几篇博客中定义过,请查阅:《springboot手写JDBC面对2000并发量毫无压力》这篇文章
至于ObjectNotNull()判空的方法,这里不再叙述,可以自行定义,如果你不会写,那么只能说你还不适合做开发。
/**
* 插入语句的拼接
*
* @author PQF
*/
private static String insertJoin(String tableName, String[] colunmName) {
StringBuffer stringbuffer = new StringBuffer();
stringbuffer.append("INSERT INTO " + tableName);
stringbuffer.append(" (");
for (int i = 0; i < colunmName.length; i++) {
stringbuffer.append(colunmName[i]);
if (i < colunmName.length - 1) {
stringbuffer.append(",");
}
}
stringbuffer.append(") ");
stringbuffer.append("VALUES(");
for (int i = 0; i < colunmName.length; i++) {
stringbuffer.append("?");
if (i < colunmName.length - 1) {
stringbuffer.append(",");
}
}
stringbuffer.append(") ");
return stringbuffer.toString();
}
/**
* @category mysql公用的插入方法
* @author PQF
*/
public static int insertCommon(String tableName, String[] colunmName,
List<String> colunmValue) throws Exception {
Connection conn = getConnection();
int executeUpdate = 0;
String insertJoinSql = insertJoin(tableName, colunmName);
PreparedStatement statement = null;
try {
statement = conn.prepareStatement(insertJoinSql);
conn.setAutoCommit(false);
for (int i = 1; i <= colunmValue.size(); i++) {
statement.setString(i, colunmValue.get(i - 1));
}
executeUpdate = statement.executeUpdate();
if (executeUpdate > 0) {
conn.commit();
} else {
conn.rollback();
}
} catch (Exception e) {
conn.rollback();
e.printStackTrace();
} finally {
release(conn, statement, null);
}
return executeUpdate;
}
/**
* 删除语句的拼接
*
* @author PQF
*/
private static String getDeleteSQL(String tableName, String column) {
// 拼SQL语句
StringBuffer sql = new StringBuffer();
sql.append("DELETE FROM " + tableName);
sql.append(" WHERE ");
sql.append(column);
sql.append("=?");
return sql.toString();
}
/**
* @category 公共的删除方法
* @author PQF
*/
public static boolean deleteCommon(String tableName, String column,
String columnValue) throws Exception {
String sql = getDeleteSQL(tableName, column);
Connection conn = getConnection();
PreparedStatement ps = null;
boolean flag = false;
try {
ps = conn.prepareStatement(sql);
ps.setLong(1, Long.parseLong(columnValue));
flag = ps.executeUpdate() > 0 ? true : false;
} catch (Exception e) {
e.printStackTrace();
} finally {
release(conn, ps, null);
}
return flag;
}
/**
* 更改语句的拼接
* @param 1.表的名称 2.列的名称 3.where语句的查询名称
* @author PQF
*/
private static String getUpdateSql(String tableName, List<String> columnName,String whereColumn) {
StringBuffer sql = new StringBuffer();
sql.append("UPDATE "+tableName+" SET ");
for (int i = 0; i < columnName.size(); i++) {
sql.append(columnName.get(i));
sql.append("=?");
if (i < columnName.size() - 1) {
sql.append(",");
}
}
sql.append(" WHERE ");
sql.append(whereColumn);
sql.append("=?");
return sql.toString();
}
/**
* @category 表更新的公用方法
* @param 1.表名称 2.列的名称 3.列的具体值 4.where语句的查询名称
* @author PQF
*/
public static int updateCommon(String tableName, List<String> columnName,List<String> colunmValue,String whereColumn)
throws Exception {
String updateSql = getUpdateSql(tableName,columnName,whereColumn);
Connection conn = getConnection();
PreparedStatement statement = null;
int result = 0;
try {
statement = conn.prepareStatement(updateSql);
conn.setAutoCommit(false);
for (int i = 1; i <= colunmValue.size(); i++) {
statement.setString(i, colunmValue.get(i - 1));
}
result = statement.executeUpdate();
if (result > 0) {
conn.commit();
} else {
conn.rollback();
}
} catch (Exception e) {
conn.rollback();
e.printStackTrace();
} finally {
release(conn, statement, null);
}
return result;
}
以上便是所有的增删改查公共方法。或许你会问我这个东西怎么用?
我的回答是,作为一个开发,你要学会去读懂别人的代码,学会看源码,模仿着去写源码,不要衣来伸手饭来张口。因为我们毕竟是使用别人提供的JDK,这叫寄人篱下。所以就得用别人的方式去工作,这就是铁定的原理。
其实,我希望看过这篇文章的朋友们,不要盲目拷贝抄袭。根据我的代码,读懂我当时写公共方法时的想法,多提几个为什么,这样才能有进步。最后,我祝愿大家,在工作中都会成为骨干,都去写公共方法提升实力。加油,朋友们