POI读取文件:
1.读取EXCEL文件空指针异常:
解决:
复制代码
1
2
3
4
5
6
7int target_qty = 0; //添加Row.MissingCellPolicy.CREATE_NULL_AS_BLANK参数即可解决空指针异常 if(row.getCell(i+3,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getCellType().equals(CellType.BLANK)) { target_qty = 0; }else{ target_qty = (int) row.getCell(i).getNumericCellValue(); }
2.读取合并单元格
解决:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114String excelOldSuffix = "xls"; String excelNewSuffix = "xlsx"; public void doParse(ParseParam param){ String fileName = param.getFile().getOriginalFilename(); InputStream stream = param.getFile().getInputStream(); Workbook workbook = null; if (fileName.endsWith(excelOldSuffix)) { workbook = new HSSFWorkbook(stream); } else if (fileName.endsWith(excelNewSuffix)) { workbook = new XSSFWorkbook(stream); } else { throw new Exception("不是一个Excel文件,不能解析"); } if (workbook.getSheetAt(0).getSheetName().equals("Sheet1")) { ArrayList<OutputConfigToPn> list = new ArrayList<>(); Sheet sheet = workbook.getSheetAt(0); int lastRowNum = sheet.getLastRowNum(); if (lastRowNum <= 0) { throw new Exception("没有内容"); } for (int j = 1; j <= lastRowNum; j++) { Row row = sheet.getRow(j); OutputConfigToPn outputConfigToPn = new OutputConfigToPn(); int lineNum = 0; for (Cell c : row) { boolean isMerge = isMergedRegion(sheet, j, c.getColumnIndex()); if (isMerge) { String value = getMergedRegionValue(sheet, row.getRowNum(), c.getColumnIndex()); if (lineNum == 0) { outputConfigToPn.setModel(value); } else { outputConfigToPn.setConfig(value); } } else { if(lineNum == 2){ outputConfigToPn.setConfig(c.getStringCellValue()); }else { String modelName = c.getStringCellValue().substring(0, 9); outputConfigToPn.setModelName(modelName); } } lineNum++; } list.add(outputConfigToPn); } if (!list.isEmpty()) { //先删后插 outputConfigToPnRepo.deleteAll(); outputConfigToPnRepo.saveAll(list); } } } private String getMergedRegionValue(Sheet sheet, int row, int column) { int sheetMergeCount = sheet.getNumMergedRegions(); for (int i = 0; i < sheetMergeCount; i++) { CellRangeAddress ca = sheet.getMergedRegion(i); int firstColumn = ca.getFirstColumn(); int lastColumn = ca.getLastColumn(); int firstRow = ca.getFirstRow(); int lastRow = ca.getLastRow(); if (row >= firstRow && row <= lastRow) { if (column >= firstColumn && column <= lastColumn) { Row fRow = sheet.getRow(firstRow); Cell fCell = fRow.getCell(firstColumn); return getCellValue(fCell); } } } return null; } private static String getCellValue(Cell cell) { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); String cellValue = ""; if (cell.getCellType().equals(CellType.STRING)) { cellValue = cell.getStringCellValue(); } else if (cell.getCellType().equals(CellType.NUMERIC)) { if (DateUtil.isCellDateFormatted(cell)) { cellValue = fmt.format(cell.getDateCellValue()); // 日期型 } else { cellValue = String.valueOf((int) cell.getNumericCellValue()); // 数字 } } else if (cell.getCellType().equals(CellType.BOOLEAN)) { cellValue = String.valueOf(cell.getBooleanCellValue()); } else if (cell.getCellType().equals(CellType.BLANK)) { cellValue = cell.getStringCellValue(); } else if (cell.getCellType().equals(CellType.ERROR)) { cellValue = "错误"; } else if (cell.getCellType().equals(CellType.FORMULA)) { cellValue = "错误"; } else { cellValue = "错误"; } return cellValue; } private boolean isMergedRegion(Sheet sheet, int row, int column) { int sheetMergeCount = sheet.getNumMergedRegions(); for (int i = 0; i < sheetMergeCount; i++) { CellRangeAddress range = sheet.getMergedRegion(i); int firstColumn = range.getFirstColumn(); int lastColumn = range.getLastColumn(); int firstRow = range.getFirstRow(); int lastRow = range.getLastRow(); if (row >= firstRow && row <= lastRow) { if (column >= firstColumn && column <= lastColumn) { return true; } } } return false; }
3.获取单元格样式,分别处理
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24private static String getCellValue(Cell cell) { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); String cellValue = ""; if (cell.getCellType().equals(CellType.STRING)) { cellValue = cell.getStringCellValue(); } else if (cell.getCellType().equals(CellType.NUMERIC)) { if (DateUtil.isCellDateFormatted(cell)) { cellValue = fmt.format(cell.getDateCellValue()); // 日期型 } else { cellValue = String.valueOf((int) cell.getNumericCellValue()); // 数字 } } else if (cell.getCellType().equals(CellType.BOOLEAN)) { cellValue = String.valueOf(cell.getBooleanCellValue()); } else if (cell.getCellType().equals(CellType.BLANK)) { cellValue = cell.getStringCellValue(); } else if (cell.getCellType().equals(CellType.ERROR)) { cellValue = "错误"; } else if (cell.getCellType().equals(CellType.FORMULA)) { cellValue = "错误"; } else { cellValue = "错误"; } return cellValue; }
数据库:
1.在做字符串操作时先除空值
复制代码
1
2select DISTINCT concat(Model,'') from tb_output_shipment select DISTINCT substr((Model,''),0,instr(concat(Model,''),'',1,1)-1) from tb_output_shipment
2.截取空格之前的字符
复制代码
1
2
3
4
5
6
7//mysql select substr(concat(Model,' '),1,instr(concat(Model,' '),' ')-1) from tb_output_shipment //oracle SUBSTR(CONCAT( b.ECN_REV, ' ' ),0,INSTR( CONCAT( b.ECN_REV, ' ' ), ' ', 1, 1 ) - 1 ) AS Model //应用 update tb_output_shipment set Model = substr(concat(Model,' '),1,instr(concat(Model,' '),' ')-1) where ProductSeries = 'iPad'
3.避免注入攻击
注意:只是用jdbcTemplate不能避免SQL注入问题
mybatis使用#{}语法时,会自动生成PreparedStatement,使用参数绑定(?)的方式来设置值,因此#{}可以防止SQL注入,而使用${}语法时,会直接注入原始字符串,即相当于拼接字符串,因而会导致SQL注入
复制代码
1
2
3
4
5<select id="getByName" resultType="org.example.User"> select * from user where name = '${name}' limit 1 </select> 当name值为' or '1' = '1,实际执行的语句为 select * from user where name = '' or '1' = '1' limit 1
使用jpa可以避免SQL注入问题,底层参数已转换成 ?。
复制代码
1Hibernate: select user0_.id as id1_0_, user0_.name as name2_0_ from user user0_ where user0_.name=?
4.jpa读取特殊结构体对象
jpa存储
复制代码
1
2
3
4
5
6
7
8
9如果我们希望student的seq值由系统自动生成,且生成规则为“yyMMdd + 8位自增序列”(例如19060310000000)又该如何实现呢? 首先想到的是该如何生成这一串序列,mysql不像oracle自身支持sequence,因此在这里可以借用函数以及额外的sequence表来实现这一操作,网上有很多实现方式,这里就不再赘述。 现在已经有了函数getseq('student_seq')可以获取到该序列,该如何将其应用到保存对象的方法中?显然的一个问题是,像上面那样再直接调用save方法已经行不通了,应该得需要自定义插入的sql实现。 @Transactional @Modifying @Query(value = "INSERT INTO t_student(seq, name, sex) VALUES (getseq('student_seq'), :#{#student.name}, :#{#student.sex})", nativeQuery = true) int insert(@Param("student") StudentDO student);
5.by日,by周,by月,by年
复制代码
1
2
3
4/******1/25日:7 周:8 月:6 年:5*****/ select count(*),DATE_FORMAT(ReportTime,'%Y-%m') AS TIME FROM tb_output_station where ((Station = 'PACKING' and MoType = 'NORMAL') or (Station = 'VMI5' and MoType = 'REWORK')) and DATE_FORMAT(ReportTime,'%Y-%m') <= '2020-12' ; select FactoryCode as factory,ProductSeries as product,sum(QTY) as qty,DATE_FORMAT(DATE_ADD(ReportTime, INTERVAL - 8 HOUR ),if('2' = 1, '%Y', if('2' = 2, '%Y-%m', if('2' = 3, '%Y-WK%u','%Y-%m-%d')))) as idate from tb_output_station where Station = 'PACKING' and MoType = 'NORMAL' and DATE_FORMAT(DATE_ADD(ReportTime, INTERVAL - 8 HOUR ),if('1' = 1, '%Y', if('1' = 2, '%Y-%m', if('1' = 3, '%Y-WK%u','%Y-%m-%d')))) <= '2020' GROUP BY idate,factory,product CONCAT('limit' , if('1' = 1, 5, if('1' = 2, 6, if('1' = 3, 8,7)))))
MyBatis:
1.查询多对多关系:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18//建立检查组和检查项多对多关系 public void setCheckGroupAndCheckItem(Integer checkGroupId,Integer[] checkitemIds){ if(checkitemIds != null && checkitemIds.length>0){ for (Integer checkitemId : checkitemIds) { Map<String,Integer> map = new HashMap<String,Integer>(); map.put("checkgroupId",checkGroupId); map.put("checkitemIds",checkitemId); checkGroupDao.setCheckGroupAndCheckItem(map); } } } <!--设置检查组和检查项多对多关系--> <insert id="setCheckGroupAndCheckItem" parameterType="map"> inser into t_checkgroup_checkitem(checkgeoup_id,checkitem_id) values (#{checkgroupId},#{checkitemId}) </insert>
2.插入数据后返回自增主键的值(结合上面使用,维护多张表关联信息)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18//新增检查组,同时需要让检查组关联检查项 public void add(CheckGroup checkGroup, Integer[] checkitemIds) { //新增检查组,操作t_checkgroup表 checkGroupDao.add(checkGroup); //设置检查组和检查项的多对多的关联关系,操作t_checkgroup_checkitem表 setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds); } <!--插入检查组数据--> <insert id="add" parameterType="com.xuyu.pojo.CheckGroup"> --LAST_INSERT_ID:获取刚才插入的主键Id order:执行顺序 keyPro:附着位置 <selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id"> select LAST_INSERT_ID() </selectKey> insert into t_checkgroup(code,name,sex,helpCode,remark,attention) values (#{code},#{name},#{sex},#{helpCode},#{remark},#{attention}) </insert>
3.多表查关联查询
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51查询套餐信息SetmealDao.xml: <!--根据套餐ID查询套餐详情(包含套餐基本信息,检查组信息,检查项信息)--> <resultMap id="baseResultMap" type="com.xuyu.pojo.Setmeal"> <id column="id" property="id"></id> <result column="name" property="name"/> <result column="code" property="code"/> <result column="helpCode" property="helpCode"/> <result column="sex" property="sex"/> <result column="age" property="age"/> <result column="price" property="price"/> <result column="remark" property="remark"/> <result column="attention" property="attention"/> <result column="img" property="img"/> </resultMap> <resultMap id="findByIdResultMap" type="com.xuyu.pojo.Setmeal" extends="baseResultMap"> <!--多对多映射--> <collection property="checkGroups" ofType="com.xuyu.pojo.CheckGroup" select="com.xuyu.dao.CheckGroupDao.findCheckGroupById" column="id"></collection> </resultMap> <select id="findById" parameterType="int" resultMap="findByIdResultMap"> select * from t_setmeal where id = #{id} </select> 查询套餐对应检查组信息CheckGroup.xml: <!--根据套餐Id查询关联的检查组详情--> <resultMap id="baseResultMap" type="com.xuyu.pojo.CheckGroup"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="code" property="code"/> <result column="helpCode" property="helpCode"/> <result column="sex" property="sex"/> <result column="remark" property="remark"/> <result column="attention" property="attention"/> </resultMap> <resultMap id="findByIdResultMap" type="com.xuyu.pojo.CheckGroup" extends="baseResultMap"> <!--检查组和检查项多对多关联查询--> <collection property="checkItem" ofType="com.xuyu.pojo.CheckItem" column="id" select="com.xuyu.dao.CheckItemDao.findCheckItemById"> </collection> </resultMap> <select id="findCheckGroupById" parameterType="int" resultMap="findByIdResultMap"> select * from t_checkgroup where id in(select checkgroup_id from t_setmeal_checkgroup where setmeal_id = #{setmeal_id}) </select> 查询检查组对应检查项信息CheckItemDao.xml: <!--根据检查组ID查询关联的检查项--> <select id="findCheckItemById" parameterType="int" resultType="com.xuyu.pojo.CheckItem"> select * from t_checkitem where id in (select checkitem_id from t_checkgroup_checkitem where checkgroup_id = #{id}) </select>
Redis单节点,集群,哨兵整合:
https://www.cnblogs.com/stupidMartian/p/12092578.html
最后
以上就是喜悦蜜蜂最近收集整理的关于项目中常见问题总结POI读取文件:数据库:的全部内容,更多相关项目中常见问题总结POI读取文件内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复