一些开发过程中遇到的问题小结—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

posted @ 2020-08-02 17:00  Huathy  阅读(11)  评论(0)    收藏  举报  来源