Commit 13e8707ecaf2d8473202bb6df7509f1154d43ce3
1 parent
a2591f68
add 批量插入工具
Showing
1 changed file
with
37 additions
and
5 deletions
src/main/java/com/bsth/util/BatchSaveUtils.java
| @@ -4,6 +4,7 @@ import java.lang.reflect.Field; | @@ -4,6 +4,7 @@ import java.lang.reflect.Field; | ||
| 4 | import java.sql.Connection; | 4 | import java.sql.Connection; |
| 5 | import java.sql.DriverManager; | 5 | import java.sql.DriverManager; |
| 6 | import java.sql.PreparedStatement; | 6 | import java.sql.PreparedStatement; |
| 7 | +import java.sql.ResultSet; | ||
| 7 | import java.util.ArrayList; | 8 | import java.util.ArrayList; |
| 8 | import java.util.List; | 9 | import java.util.List; |
| 9 | 10 | ||
| @@ -44,7 +45,15 @@ public class BatchSaveUtils<T> { | @@ -44,7 +45,15 @@ public class BatchSaveUtils<T> { | ||
| 44 | pwd = t.getValue("spring.datasource.password"); | 45 | pwd = t.getValue("spring.datasource.password"); |
| 45 | } | 46 | } |
| 46 | 47 | ||
| 47 | - public int saveListMysql(List<T> list, Class<T> clazz){ | 48 | + /** |
| 49 | + * | ||
| 50 | + * @Title: saveListMysql | ||
| 51 | + * @Description: TODO(批量对象入库,请自行准备好ID) | ||
| 52 | + * @param @param list | ||
| 53 | + * @param @param clazz | ||
| 54 | + * @throws | ||
| 55 | + */ | ||
| 56 | + public int saveList(List<T> list, Class<T> clazz){ | ||
| 48 | //获取泛型 T 的字节码 | 57 | //获取泛型 T 的字节码 |
| 49 | Table table = clazz.getAnnotation(Table.class); | 58 | Table table = clazz.getAnnotation(Table.class); |
| 50 | if(null == table){ | 59 | if(null == table){ |
| @@ -56,10 +65,12 @@ public class BatchSaveUtils<T> { | @@ -56,10 +65,12 @@ public class BatchSaveUtils<T> { | ||
| 56 | logger.info(sql); | 65 | logger.info(sql); |
| 57 | 66 | ||
| 58 | //每5000条批量入库一次 | 67 | //每5000条批量入库一次 |
| 68 | + Connection conn = null; | ||
| 69 | + PreparedStatement ps = null; | ||
| 59 | try{ | 70 | try{ |
| 60 | - Connection conn = getConn(); | 71 | + conn = getConn(); |
| 61 | conn.setAutoCommit(false); | 72 | conn.setAutoCommit(false); |
| 62 | - PreparedStatement ps = conn.prepareStatement(sql); | 73 | + ps = conn.prepareStatement(sql); |
| 63 | 74 | ||
| 64 | int fsize = fs.size(), count = 0; | 75 | int fsize = fs.size(), count = 0; |
| 65 | for(T t : list){ | 76 | for(T t : list){ |
| @@ -68,15 +79,20 @@ public class BatchSaveUtils<T> { | @@ -68,15 +79,20 @@ public class BatchSaveUtils<T> { | ||
| 68 | ps.setObject(i + 1, fs.get(i).get(t)); | 79 | ps.setObject(i + 1, fs.get(i).get(t)); |
| 69 | } | 80 | } |
| 70 | 81 | ||
| 82 | + ps.addBatch(); | ||
| 71 | if(count % batchSize == 0){ | 83 | if(count % batchSize == 0){ |
| 72 | ps.executeBatch(); | 84 | ps.executeBatch(); |
| 85 | + conn.commit(); | ||
| 73 | ps.clearBatch(); | 86 | ps.clearBatch(); |
| 74 | } | 87 | } |
| 75 | } | 88 | } |
| 76 | ps.executeBatch(); | 89 | ps.executeBatch(); |
| 90 | + conn.commit(); | ||
| 77 | }catch(Exception e){ | 91 | }catch(Exception e){ |
| 78 | logger.error("",e); | 92 | logger.error("",e); |
| 79 | return -1; | 93 | return -1; |
| 94 | + }finally { | ||
| 95 | + closeAll(conn, ps, null); | ||
| 80 | } | 96 | } |
| 81 | 97 | ||
| 82 | return 0; | 98 | return 0; |
| @@ -84,7 +100,7 @@ public class BatchSaveUtils<T> { | @@ -84,7 +100,7 @@ public class BatchSaveUtils<T> { | ||
| 84 | 100 | ||
| 85 | public String createSql(Table table, List<Field> fs){ | 101 | public String createSql(Table table, List<Field> fs){ |
| 86 | String sqlBefore = "insert into " + table.name() + "(" | 102 | String sqlBefore = "insert into " + table.name() + "(" |
| 87 | - ,sqlValues = "("; | 103 | + ,sqlValues = " values("; |
| 88 | for(Field field : fs){ | 104 | for(Field field : fs){ |
| 89 | sqlBefore += (propertyToField(field.getName()) + ","); | 105 | sqlBefore += (propertyToField(field.getName()) + ","); |
| 90 | sqlValues += "?,"; | 106 | sqlValues += "?,"; |
| @@ -98,7 +114,7 @@ public class BatchSaveUtils<T> { | @@ -98,7 +114,7 @@ public class BatchSaveUtils<T> { | ||
| 98 | public static void main(String[] args) { | 114 | public static void main(String[] args) { |
| 99 | List<ScheduleRealInfo> list = new ArrayList<>(); | 115 | List<ScheduleRealInfo> list = new ArrayList<>(); |
| 100 | 116 | ||
| 101 | - new BatchSaveUtils<ScheduleRealInfo>().saveListMysql(list, ScheduleRealInfo.class); | 117 | + new BatchSaveUtils<ScheduleRealInfo>().saveList(list, ScheduleRealInfo.class); |
| 102 | } | 118 | } |
| 103 | 119 | ||
| 104 | /** | 120 | /** |
| @@ -141,4 +157,20 @@ public class BatchSaveUtils<T> { | @@ -141,4 +157,20 @@ public class BatchSaveUtils<T> { | ||
| 141 | Connection conn = DriverManager.getConnection(url,uname,pwd); | 157 | Connection conn = DriverManager.getConnection(url,uname,pwd); |
| 142 | return conn; | 158 | return conn; |
| 143 | } | 159 | } |
| 160 | + | ||
| 161 | + public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs){ | ||
| 162 | + try { | ||
| 163 | + if(conn != null){ | ||
| 164 | + conn.close(); | ||
| 165 | + } | ||
| 166 | + if(ps != null){ | ||
| 167 | + ps.close(); | ||
| 168 | + } | ||
| 169 | + if(rs != null){ | ||
| 170 | + rs.close(); | ||
| 171 | + } | ||
| 172 | + } catch (Exception e) { | ||
| 173 | + e.printStackTrace(); | ||
| 174 | + } | ||
| 175 | + } | ||
| 144 | } | 176 | } |