我在以前的文章中配置了Employee类的映射关系:
http://blog.youkuaiyun.com/ironrabbit/article/details/17137445
http://blog.youkuaiyun.com/ironrabbit/article/details/17143121
http://blog.youkuaiyun.com/ironrabbit/article/details/17142305
上述图片中的问题,应该是繁琐的实体关系导致的。那么问题来了:
- 我们在做Spring+Hibernate的项目中,表现层和持久层到底要怎么设计?是一个实体一直用到底,还是在中间做一次转换呢?
- 在Spring+Hibernate的配置中,到底有没有一种配置可以兼顾到Spring Controller返回JSON字串以及保持Hibernate实体间的映射呢?
- 或者,问题根本不在Spring+Hibernate,也许我们应该用其他的东西来避免?
我的rest-servlet.xml配置文件是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.homeland.myapp.controller" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/page/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/css/" mapping="/css/**" />
<bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean id="jsonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<!-- <bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg ref="jaxbMarshaller" /> <property name="supportedMediaTypes" value="application/xml"></property>
</bean> -->
<!-- <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound"> <list> <value>com.homeland.myapp.entity.Employee</value>
<value>com.homeland.myapp.entity.Department</value> <value>com.homeland.myapp.entity.EmployeeDetails</value>
<value>com.homeland.myapp.entity.Role</value> </list> </property> </bean> -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<context:annotation-config />
<!-- <bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringHttpMessageConverter" />
<ref bean="jsonHttpMessageConverter" />
<ref bean="marshallingHttpMessageConverter" />
</list>
</property>
</bean> -->
</beans>
请自动忽略注释部分。
我的Spring Controller是这样的:
package com.homeland.myapp.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.homeland.myapp.entity.Employee;
import com.homeland.myapp.service.EmployeeService;
@Controller
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@RequestMapping(method = RequestMethod.GET, value = "/employee/{id}")
@ResponseBody
public Employee getEmployee(@PathVariable String id) {
Employee e = employeeService.findById(Integer.parseInt(id));
return e;
}
}
思前想后半天,还是改设计吧。别用Hibernate的复杂映射关系了。咱把表和映射都弄简单点。别因为那些眼花缭乱的配置把自己给弄傻了。
简单,易上手,易维护就OK!