一些开发过程中遇到的问题小结—2020/08/02—MySQL时间函数、MyBatis时间查询成为时间戳、SpringMVC中ResponseBody乱码、java中cookie存取值

MySQL中获取当前时间的函数 now()

select now()

MyBatis中查询时间,却显示成时间戳,要求会显示为字符串的问题

可以使用 DATE_FORMAT(datetime,'%Y-%m-%d %H:%i:%s') 来解决

SELECT  DATE_FORMAT(download_time,'%Y-%m-%d %H:%i:%s') download_time  FROM  download_record

SpringMvc @ResponseBody字符串中文乱码原因及解决方案

  1. 使用xml配置消息转换器,指定响应的字符集为utf-8
<mvc:annotation-driven conversion-service="conversionService">
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="defaultCharset" value="UTF-8"/>
            <property name="writeAcceptCharset" value="false"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
  1. 使用注解,指定响应的字符集为utf-8
@RequestMapping(value = "/demo1",produces = {"text/plain;charset=utf-8","text/html;charset=utf-8"})
@ResponseBody
public String demo1(){
	return "我是中文测试";
}
  1. 使用注解,使结果转为JSON返回客户端
@RequestMapping(value = "/demo1",produces = {"application/json;charset=utf-8"})
@ResponseBody
public String demo1() throws JsonProcessingException {
    return new ObjectMapper().writeValueAsString("我是中文测试");
}

参照:https://www.cnblogs.com/lvbinbin2yujie/p/10611415.html

java中cookie存取值

Cookie userCookie=new Cookie("loginInfo",loginInfo); 
userCookie.setMaxAge(30*24*60*60);   //存活期为一个月 30*24*60*60
userCookie.setPath("/");		//设置保存的路径
response.addCookie(userCookie);
Cookie[] cookies = request.getCookies();
for(Cookie cookie : cookies){
	if(cookie.getName().equals("loginInfo")){
		String loginInfo = cookie.getValue();
		String username = loginInfo.split(",")[0];
		String password = loginInfo.split(",")[1];
		request.setAttribute("username", username);
		request.setAttribute("password", password);
	}
}

参照:https://www.cnblogs.com/super-chao/p/6118739.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Huathy-雨落江南,浮生若梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值