一、关联的多对多实体类在读取数据并json格式时,产生死循环
解决方法:在某一方与另一方产生关联的属性的get方法加上@JsonIgnore注解
例:
@ManyToMany(mappedBy = "majors")
@JsonIgnore//处理的注解
public Set<School> getSchools() {
return schools;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "school_major", joinColumns = { @JoinColumn(name = "schoolID") }, inverseJoinColumns = {
@JoinColumn(name = "MajorId") })
public List<Major> getMajors() {
return majors;
}
二、控制器中的方法类型:
(一)、ajax请求的方法,返回json数据
解决方法: 控制器的方法前加上注解:@ResponseBody
// 模糊查询
@ResponseBody
@RequestMapping(value = "/fuzzyQueryMajor")//常规注解
public List<Major> fuzzyQuerySchool(String keyword) {
return majorService.fuzzyQuery(keyword);
}
(二)、普通的请求,需要跳转页面
方法:只加常规注解 @RequestMapping("xxx")
// 查询一个学校的具体信息
@RequestMapping("/selSchoolInfo")
public String selSchoolInfo(String schoolName, Model model) throws Exception {
schoolName = new String(schoolName.getBytes("ISO-8859-1"), "UTF-8");
School school = schoolService.findOneSchool(schoolName);
model.addAttribute("school", school);
return "/WEB-INF/jsp/schoolMain.jsp";
}
三、通过sessionFactory方法获得session不能获取当前线程
解决方法:在web.xml加入如下配置
<!-- openSessionInView配置 --> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
四、数据库乱码
解决方法:在config.properties的jdbc_url参数后加”?useUnicode=true&characterEncoding=utf-8”
jdbc_url=jdbc:mysql://localhost:3306/mrs?useUnicode=true&characterEncoding=utf-8