一、SpringBoot try…catch回滾事務
-
方法上添加注解
@Transactional -
標記回滾開始節點
Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint(); -
啟動回滾
TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
二、MyBatis查詢關聯
-
mybatis一對多(ofType:關聯表,column:外鍵)
<resultMap id="resultMap" type="com.wade.www.entity.Entity1"> <result property="id" column="id"/> <result property="name" column="name"/> <collection property="entity2List" column="{entity2Id=id}" ofType="com.wade.www.entity.Entity2" javaType="ArrayList" select="com.wade.www.mapper.Entity2Mapper.queryById"> </collection> </resultMap>
三、檔案與String的相互轉換
-
檔案轉String
private String fileToString(String pathStr) { File file = new File(pathStr); try { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); byte[] data = bos.toByteArray(); bos.close(); return Base64.getEncoder().encodeToString(data); } catch (Exception e) { e.printStackTrace(); } return null; } -
String轉檔案
public static void StringToFile(String base64file, String filePath, String fileName) throws Exception { byte[] bytes = Base64.getDecoder().decode(base64file); BufferedOutputStream bos = null; FileOutputStream fos = null; File file; try { File dir = new File(filePath); if (!dir.exists() && !dir.isDirectory()) {// 判斷檔案目錄是否存在 dir.mkdirs(); } file = new File(filePath + "\\" + fileName); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bytes); } catch (Exception e) { throw new Exception("檔案傳輸失敗!"); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/388103.html
標籤:其他
下一篇:用串列實體化一個類
