常用小方法
Categories: 杂文
项目一些常见问题的解决方案
1.数据库获取时间类型数据在jsp中显示
1.实体类中在时间类型的get方法上添加注释
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") public Date getDate() { return date; }
2.jsp中格式化显示时间
<fmt:formatDate value="${sessionScope.user.start}" pattern="yyyy-MM-dd"/>
2.添加视图解析器前后缀仍需要访问.html路径
1.springMVC中添加
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean>
2.web.xml中配置
<servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--跟随tomcat启动同时加载--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>
3.如需在controller的返回值中跳转到.html时
@RequestMapping("/logout.html") public String logout(HttpSession session) { session.invalidate(); return "redirect:/login.html"; }
3.返回json数据时不显示密码类属性
在实体类中属性的get方法上添加注解
@JsonIgnoreProperties public String getPwd() { return pwd; }
4.mybatis条件查询
<select id="getMemberByName" parameterType="Member" resultMap="memberMap"> select * from member <where> <if test="name != null and name != ''">name like concat('%',#{name},'%')</if> </where> </select>
5.mybatis查询时传入多个参数
1.dao接口中对传入的参数添加注解
User getStudentByEmail(@Param("email")String email, @Param("type")Integer type);
2.mapper中直接使用与注解对应的名称取值
<select id="getStudentByEmail" resultMap="stuMap"> select * from user where email=#{email} and type=#{type} </select>
6.date格式传入和传出注解
@DateTimeFormat(pattern = "yyyy-MM-dd") public void setTel(String tel) { this.tel = tel; } @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") public Date getStart() { return start; }
7.加载配置文件常量
resources下创建constant.properties(后缀非限定)配置文件,内容如
IMG_URL=http://localhost:8080/upload/
springMVC-servlet文件中 加载配置文件
<context:property-placeholder location="classpath:constant.properties" ignore-unresolvable="true"/>
Controller(非限定)中调用常量
@Value("${IMG_URL}")
private String IMG_URL;
8.重置mysql索引归零
TRUNCATE TABLE 表名;
9.返回json前台乱码
在 @RequestMapping 中添加 produces = "text/html;charset=UTF-8" 例如
@RequestMapping(value = "/get.html", produces = "text/html;charset=UTF-8")
10.idea创建web项目 webapp目录未被识别
1.将pom.xml的dependencies、build删除,项目右键——>Maven——>Reimport
2.将Web Resource Directory指向项目的webapp目录(不推荐)
11.项目同时提供接口和页面展示
项目提供api接口,例如 /hero/all ,同时提供页面访问,例如 /hero.html,修改web.xml
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--跟随tomcat启动同时加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
12.修改常量类的常量值不起作用
在修改A中常量并替换class文件后,实际不生效,因为B的class在编译后对final值引用为实际值,所以在修改时需要把所有对应引用的类重新编译替换
class A {
public static final int MAX = 2000 * 1024 * 1024;
}
class B {
A.MAX;
}
13.7zip批量压缩
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X\"
14.chromium浏览器自带二维码生成
chrome://flags/#sharing-qr-code-generator
Enable sharing page via QR Code设置为Enabled,重启浏览器
15.Mysql查询某字段值重复的数据
select user_name,count(*) as count from user group by user_name having count>1;