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 | 4 | import java.sql.Connection; |
| 5 | 5 | import java.sql.DriverManager; |
| 6 | 6 | import java.sql.PreparedStatement; |
| 7 | +import java.sql.ResultSet; | |
| 7 | 8 | import java.util.ArrayList; |
| 8 | 9 | import java.util.List; |
| 9 | 10 | |
| ... | ... | @@ -44,7 +45,15 @@ public class BatchSaveUtils<T> { |
| 44 | 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 | 57 | //获取泛型 T 的字节码 |
| 49 | 58 | Table table = clazz.getAnnotation(Table.class); |
| 50 | 59 | if(null == table){ |
| ... | ... | @@ -56,10 +65,12 @@ public class BatchSaveUtils<T> { |
| 56 | 65 | logger.info(sql); |
| 57 | 66 | |
| 58 | 67 | //每5000条批量入库一次 |
| 68 | + Connection conn = null; | |
| 69 | + PreparedStatement ps = null; | |
| 59 | 70 | try{ |
| 60 | - Connection conn = getConn(); | |
| 71 | + conn = getConn(); | |
| 61 | 72 | conn.setAutoCommit(false); |
| 62 | - PreparedStatement ps = conn.prepareStatement(sql); | |
| 73 | + ps = conn.prepareStatement(sql); | |
| 63 | 74 | |
| 64 | 75 | int fsize = fs.size(), count = 0; |
| 65 | 76 | for(T t : list){ |
| ... | ... | @@ -68,15 +79,20 @@ public class BatchSaveUtils<T> { |
| 68 | 79 | ps.setObject(i + 1, fs.get(i).get(t)); |
| 69 | 80 | } |
| 70 | 81 | |
| 82 | + ps.addBatch(); | |
| 71 | 83 | if(count % batchSize == 0){ |
| 72 | 84 | ps.executeBatch(); |
| 85 | + conn.commit(); | |
| 73 | 86 | ps.clearBatch(); |
| 74 | 87 | } |
| 75 | 88 | } |
| 76 | 89 | ps.executeBatch(); |
| 90 | + conn.commit(); | |
| 77 | 91 | }catch(Exception e){ |
| 78 | 92 | logger.error("",e); |
| 79 | 93 | return -1; |
| 94 | + }finally { | |
| 95 | + closeAll(conn, ps, null); | |
| 80 | 96 | } |
| 81 | 97 | |
| 82 | 98 | return 0; |
| ... | ... | @@ -84,7 +100,7 @@ public class BatchSaveUtils<T> { |
| 84 | 100 | |
| 85 | 101 | public String createSql(Table table, List<Field> fs){ |
| 86 | 102 | String sqlBefore = "insert into " + table.name() + "(" |
| 87 | - ,sqlValues = "("; | |
| 103 | + ,sqlValues = " values("; | |
| 88 | 104 | for(Field field : fs){ |
| 89 | 105 | sqlBefore += (propertyToField(field.getName()) + ","); |
| 90 | 106 | sqlValues += "?,"; |
| ... | ... | @@ -98,7 +114,7 @@ public class BatchSaveUtils<T> { |
| 98 | 114 | public static void main(String[] args) { |
| 99 | 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 | 157 | Connection conn = DriverManager.getConnection(url,uname,pwd); |
| 142 | 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 | } | ... | ... |