个人日常开发工具、问题收集
一、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(); } } } }
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
二维码