记录一下遇到的问题和坑
最近做了一个纯粹增删改查的的小项目,记录一下问题和解决方法。shiro权限管理和付款功能还没写,以后再看情况学习和完善吧。
1.关于mybatis
mybatis-generator 无法自动生成字段类型为text的属性
在generatorConfig.xml文件下面的 table标签下加
注意是:jdbcType=“VARCHAR” ,不要写成 jdbcType="TEXT"
<table tableName="patient" domainObjectName="Patient" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<columnOverride column="pstory" javaType="java.lang.String" jdbcType="VARCHAR" />
<columnOverride column="psick" javaType="java.lang.String" jdbcType="VARCHAR" />
<columnOverride column="ptreat" javaType="java.lang.String" jdbcType="VARCHAR" />
</table>
resultmap中需要多个association实体属性
https://blog.youkuaiyun.com/qq_42397121/article/details/106052047
mybatis跨XML引用
https://blog.youkuaiyun.com/weixin_33824363/article/details/93836917
update语句未生效
发现修改的时候,update后数据并没有改变
检查发现前端的数据都能传入到后台。就是sql语句未生效。因为where语句没有收到主键的值
是因为前端没有显示主键。即使不需要主键也要写进去,不然缺少主键的id值,没法更新这个数据
不希望显示的话,type = "hidden"就好了
<div class="form-group">
<label class="col-form-label" for="idoid">xxx</label>
<input class="form-control" id="idoid" name="idoid" type="hidden" th:value="${idonate!=null}?${idonate.idoid}">
</div>
2.关于前端
input type="text"标签与textarea的区别
-
input type=“text”
即使设置行高了,文本依然只有一行,并且居中显示。 -
input type=“textarea”
应该是没有这样的设置方式的,这样设置的话,最终的显示结果和type="text"是一样的 -
texarea标签
thymeleaf的textarea数据回显用th:text,th:value不能回显
<div class="form-group">
<label class="col-form-label" for="pstory">患者故事</label>
<textarea class="form-control" id="pstory" name="pstory" th:text="${patient!=null}?${patient.pstory}">
</textarea>
</div>
<div class="form-group">
<label class="col-form-label" for="psick">病情信息</label>
<input class="form-control" id="psick" name="psick" type="textarea" style="height:300px" th:value="${patient!=null}?${patient.psick}">
</div>
<div class="form-group">
<label class="col-form-label" for="ptreat">治疗情况</label>
<input class="form-control" id="ptreat" name="ptreat" type="textarea" th:value="${patient!=null}?${patient.ptreat}">
</div>
审核功能
后台用一个审核状态的字段
审核通过 1;
审核不通过 0;
未审核 2
前端thymeleaf用三目运算符,要加括号,不加会无法解析
后面可以再加,感觉不是很有必要
<td th:text="${apply.astate}==2?'未审核':(${apply.astate}==1?'审核通过':'审核不通过')"></td>
接收和传输Date类型
后台-前端
th:value="${apply!=null}?${#dates.format(apply.adll,'yyyy-MM-dd HH:mm:ss')}"
前端-后台
在yml加
spring
mvc
date-format: yyyy-MM-dd
在实体类里面加
@DateTimeFormat(pattern = "yyyy-MM-dd")
上面两个方法的前提是,前端input name必须和实体里面设置的属性一样,不然无法完成封装。。前两个方法不管用可能是前端和后台名字设置的不一样。
我当时没发现我前端input name里面设置的和后台不一样。
所以我就用了个笨方法,接受后再在后台把这个数据转换成Date类型。重新设置
可以看出来就是实体里面定义的是adll,我前端里面写的是addl。。。。所以就一直收到的是null
public class Apply{
private String aid;
//@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date adll;
<div class="form-group">
<label class="col-form-label" for="addl">众筹期限</label>
<div class='input-group date' id='datetimepicker2'>
<input class="form-control" id="addl" name="addl" type="text" th:value="${apply!=null}?${#dates.format(apply.adll,'yyyy-MM-dd')}" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
@PostMapping("/apply")
public String add(Apply apply, @RequestParam("addl") String addl) throws ParseException {
System.out.println("添加");
SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
Date date = formatter.parse(addl);
//System.out.println("Date" + date);
apply.setAdll(date);
//System.out.println(apply.getAdll());
applyService.insertSelective(apply);
return "redirect:/applies";