model对象之setter方法使用,解决去除空格和将数字转成字符串展示方法

时间:2023-01-17 21:10:07

1.系统中手机号注册的时候,不能含有前后空格。在model对象中过滤~!

private String mobile;
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
mobile = mobile.trim(); //过滤空格
this.mobile = mobile;
}

如果controller转递的是Map对象,封装表单的数据。那么可以通过过滤Map中字符串的空格达到要求。

Iterator itr = map3.keySet().iterator();
while (itr.hasNext()) { //循环遍历map集合
Object object = (Object) itr.next();
System.out.println("键:" + object);
Object value = map3.get(object);
System.out.println("值:" + value);
if(value instanceof String){
map3.put(object, ((String) value).trim());
}
}

2.另外在sql中可以使用trim() sql函数过滤空格。ltrim()去掉左边空格,rtrim()去掉右边空格。

SELECT id,CONCAT("[",TRIM(hbox_id),"]"), CONCAT("[",LTRIM(hbox_id),"]"),  CONCAT("[",RTRIM(hbox_id),"]"), STATUS FROM car_hw WHERE id > 105;

3.数字格式的日期转换成字符日期格式,交易状态在数据库中数字表示转换成系统字符串表示。

private BigInteger orderTime;
private int status;
//展示效果
private String orderTimeView;
private String statusView;
public BigInteger getOrderTime() {
return orderTime;
}
public void setOrderTime(BigInteger orderTime) {
this.orderTime = orderTime;
this.setOrderTimeView(this.converse(orderTime));
} private String converse(BigInteger big) {
String old = String.valueOf(big);
String year = old.substring(0, 4);
String month = old.substring(4, 6);
String day = old.substring(6, 8);
String hour = old.substring(8, 10);
String minute = old.substring(10, 12);
String second = old.substring(12, 14);
return year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second;
} public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
switch (status) {
case 1:
this.setStatusView("请求已发出/等待车主确认");
break;
case 2:
this.setStatusView("请求已接受/车主已确认");
break;
default:
this.setStatusView("未知");
break;
} }

4.也可以在sql中进行转换

<select id="queryTrans" resultMap="transResultMap">
SELECT t.*,CONCAT(MID(req_Time,1,4),'/',MID(req_Time,5,2),'/',MID(req_Time,7,2),' ',MID(req_Time,9,2),':',MID(req_Time,11,2),':',MID(req_Time,13,2)) AS _reqTime,
CONCAT(MID(rent_Time,1,4),'/',MID(rent_Time,5,2),'/',MID(rent_Time,7,2),' ',MID(rent_Time,9,2),':',MID(rent_Time,11,2),':',MID(rent_Time,13,2)) AS _rentTime,
CONCAT(MID(revert_Time,1,4),'/',MID(revert_Time,5,2),'/',MID(revert_Time,7,2),' ',MID(revert_Time,9,2),':',MID(revert_Time,11,2),':',MID(revert_Time,13,2)) AS _revertTime,
CONCAT(MID(pay_Time,1,4),'/',MID(pay_Time,5,2),'/',MID(pay_Time,7,2),' ',MID(pay_Time,9,2),':',MID(pay_Time,11,2),':',MID(pay_Time,13,2)) AS _payTime FROM trans t <include refid="whereCondition"/> ORDER BY id DESC LIMIT #{pb.startIndex}, #{pb.pageSize}
</select>