个人日常开发工具问题收集
Posted hi, wade~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了个人日常开发工具问题收集相关的知识,希望对你有一定的参考价值。
一、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();
以上是关于个人日常开发工具问题收集的主要内容,如果未能解决你的问题,请参考以下文章