1、https://blog.csdn.net/xuanyonghao/article/details/73928497
2、https://www.cnblogs.com/cl1255674805/p/5486950.html
具体实现:
1、引入包:
<jackson.version>2.9.5</jackson.version>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
2、
<!-- 通过实体类返回json格式数据的关键配置 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<!--返回字符串格式json-->
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
3、页面(重点是dataType是json):
$.ajax({
//url: "/HospitalManage/data/patient.json",
url: "/hos/manager/getPatientList",
async: true,
dataType: "json",
success: function(result) {
self.patient_info = result.data;
console.log("length:"+result.data.length)
self.$nextTick(function() {
self.laypage.render({
elem: 'pages',
curr: self.page,
limit: self.pageSize,
count: result.data.length,
jump: function(obj, first) {
if(!first) {
self.page = obj.curr;
self.getPatientInfo();
}
}
});
});
}
});
4、后台Controller:
@ResponseBody
@RequestMapping("/getPatientList")
public Result getPatientList(){
Result result = new Result();
TPatientExample example = new TPatientExample();
example.createCriteria().andIdIsNotNull();
List<TPatient> patientList = patientService.getPatientList(example);
result.setData(patientList);
return result;
//return result.toString();//接收ajax请求,返回一个json数组
}