批处理就是一批一批的处理,只针对更新(增,删,改)语句,不包括查询。
对于mysql 默认是关闭批处理的,需要在连接url中添加参数
rewriteBatchedStatements=true
Statement批处理
多次调用statement类的addBatch(String sql)方法,将需要执行的所有SQL语句添加到“批中”,然后调用executeBatch()方法来执行当前“批”中的语句。
java mysql数据库操作类
package com.demo.system.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import org.apache.commons.lang3.StringUtils;
public class MySQLConnector {
public static int BATCH_ROW = 100000;
private String url;
private String username;
private String password;
public MySQLConnector(String url, String username, String password) {
this.url = url;
this.username = username;
this.password = password;
}
public Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* insert
*
* @param tableName
* @param columns
* @param values
* @return
*/
public int insert(String tableName, String[] columns, String[] values) {
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO `").append(tableName).append("` (`");
for (int i = 0; i < columns.length; i++) {
sb.append(columns[i]);
if (i < columns.length - 1) {
sb.append("`, `");
}
}
sb.append("`) VALUES (");
for (int i = 0; i < values.length; i++) {
sb.append("'").append(values[i]).append("'");
if (i < values.length - 1) {
sb.append(", ");
}
}
sb.append(")");
String sql = sb.toString();
return execSql(sql);
}
/**
* 删除操作
*
* @param tableName
* @param where
* @return
*/
public int delete(String tableName, String where) {
String sql = "DELETE FROM `" + tableName + "`";
if (where != null && !where.isEmpty()) {
sql = sql + " WHERE " + where;
}
return execSql(sql);
}
/**
* update
*
* @param tableName
* @param columns
* @param values
* @param where
* @return
*/
public int update(String tableName, String[] columns, String[] values, String where) {
StringBuilder sb = new StringBuilder();
sb.append("UPDATE `").append(tableName).append("` SET ");
for (int i = 0; i < columns.length; i++) {
sb.append(String.format("`%s`", columns[i])).append(" = '").append(values[i]).append("'");
if (i < columns.length - 1) {
sb.append(", ");
}
}
if (where != null && !where.isEmpty()) {
sb.append(" WHERE ").append(where);
}
String sql = sb.toString();
return execSql(sql);
}
/**
* 查询
*
* @param tableName
* @param columns
* @param where
* @return
*/
public LinkedHashMap> select(String tableName, String[] columns, String where) {
StringBuilder sb = new StringBuilder();
sb.append("SELECT ");
if (columns != null && columns.length > 0) {
for (int i = 0; i < columns.length; i++) {
sb.append(String.format("`%s`", columns[i]));
if (i < columns.length - 1) {
sb.append(", ");
}
}
} else {
sb.append("*");
columns = getAllFields(tableName); //将*替换成字段数组
}
sb.append(" FROM ").append(String.format("`%s`", tableName));
if (where != null && !where.isEmpty()) {
sb.append(" WHERE ").append(where);
}
String sql = sb.toString();
System.out.println("执行sql:" + sql);
ResultSet rs = querySql(sql);
LinkedHashMap> result = new LinkedHashMap();
try {
while (rs.next()) {
LinkedHashMap row = new LinkedHashMap();
for (int i = 0; i < columns.length; i++) {
row.put(columns[i], rs.getString(columns[i]));
}
result.put(rs.getRow(), row);
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/**
* 批量insert
*
* @param tableName
* @param columns
* @param values
* @return
*/
public int batchInsert(String tableName, LinkedHashMap columns,
HashMap> values) {
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO `" + tableName + "` (");
columns.forEach((key, value) -> {
sb.append(String.format("`%s`,", key));
});
sb.append(") VALUES (");
columns.forEach((key, value) -> {
sb.append("?,");
});
sb.append(")");
String sql = sb.toString();
sql = StringUtils.replace(sql, ",)", ")");
try {
Connection conn = new MySQLConnector(url, username, password).getConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
int batchSize = 4000;
int count = 0;
for (Entry> rows : values.entrySet()) {
int rowIndex = 1;
for (Entry entry : rows.getValue().entrySet()) {
String key = entry.getKey();
if (StringUtils.equalsIgnoreCase(columns.get(key), "int")) {
stmt.setInt(rowIndex, Integer.parseInt(entry.getValue()));
}
if (StringUtils.equalsIgnoreCase(columns.get(key), "string")) {
stmt.setString(rowIndex, entry.getValue());
}
rowIndex++;
}
stmt.addBatch();
count++;
if (count % batchSize == 0) {
stmt.executeBatch();
stmt.clearBatch();
count = 0;
}
}
if (count > 0) {
stmt.executeBatch();
stmt.clearBatch();
}
stmt.close(); //关闭数据的连接
} catch (SQLException e) {
e.printStackTrace();
}
return 1;
}
/**
* 分步执行sql,解决慢查询
*
* @param table
* @param sql
* @return
*/
public Boolean operatorTableDataByStep(String table, String sql) {
int page = 1;
String maxSql = "select max(id) as k from `" + table + "` limit 1";
int maxId = 0;
String max = selectOne(maxSql);
if (StringUtils.isNotEmpty(max)) {
maxId = Integer.parseInt(max);
}
Boolean result = false;
int end = 0;
do {
int start = (page - 1) * BATCH_ROW;
end = page * BATCH_ROW;
String opertor = StringUtils.containsIgnoreCase(sql, "where") ? "and" : "where";
String stepSql = sql + " " + opertor + " id>" + start + " and id<=" + end + ";";
++page;
if (execSql(stepSql) > 0) {
result = true;
}
} while (end < maxId);
return result;
}
/**
* 执行sql语句,返回数据集合
*
* @param sql
* @return
*/
public String selectOne(String sql) {
String result = null;
Connection conn = new MySQLConnector(url, username, password).getConnection();
if (conn != null) {
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
result = rs.getString("k");
}
stmt.close(); //关闭数据的连接
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 执行sql语句,返回受影响的条数
*
* @param sql
* @return
*/
public int execSql(String sql) {
int rs = 0;
Connection conn = new MySQLConnector(url, username, password).getConnection();
if (conn != null) {
try {
Statement stmt = conn.createStatement();
System.out.println("执行sql:" + sql);
rs = stmt.executeUpdate(sql);
stmt.close(); //关闭数据的连接
} catch (SQLException e) {
e.printStackTrace();
}
}
return rs;
}
/**
* 获取表的所有字段
*
* @param tableName
* @return
*/
private String[] getAllFields(String tableName) {
String[] columns = null;
ResultSet fields = querySql(String.format("SHOW COLUMNS FROM `%s`", tableName));
try {
List list = new ArrayList();
while (fields.next()) {
list.add(fields.getString("Field"));
}
columns = list.toArray(new String[list.size()]);
} catch (SQLException e) {
e.printStackTrace();
}
return columns;
}
/**
* 执行sql语句,返回数据集合
*
* @param sql
* @return
*/
private ResultSet querySql(String sql) {
ResultSet rs = null;
Connection conn = new MySQLConnector(url, username, password).getConnection();
if (conn != null) {
try {
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
return rs;
}
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/database_name";
String username = "your_username";
String password = "your_password";
MySQLConnector connector = new MySQLConnector(url, username, password);
connector.execSql("CREATE TABLE `table_name` (\n"
+ " `id` int(11) NOT NULL AUTO_INCREMENT,\n"
+ " `name` varchar(30) DEFAULT NULL,\n"
+ " `age` int(11) DEFAULT NULL,\n"
+ " PRIMARY KEY (`id`)\n"
+ ")");
// 插入数据
String[] columns = {"name", "age"};
String[] values = {"John", "25"};
connector.insert("table_name", columns, values);
// 删除数据
String condition = "age > 30";
connector.delete("table_name", condition);
// 更新数据
String[] updateColumns = {"name", "age"};
String[] updateValues = {"Jane", "28"};
String updateCondition = "id = 1";
connector.update("table_name", updateColumns, updateValues, updateCondition);
// 查询数据
String[] selectColumns = {"name", "age"};
String selectCondition = "age < 30";
LinkedHashMap> rows = connector
.select("table_name", selectColumns, selectCondition);
System.out.println(rows);
// 批量插入数据
// HashMap> values = new HashMap();
// LinkedHashMap row = new LinkedHashMap();
// row.put("name", "Tom");
// row.put("age", "23");
// values.put(1, row);
// LinkedHashMap row2 = new LinkedHashMap();
// row2.put("name", "afaf");
// row2.put("age", "66");
// values.put(2, row2);
// connector.batchInsert("table_name", columns, values);
}
}