谢谢各位大神指导!
spring-hibernate.xml
<context:component-scan base-package="cn"></context:component-scan>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sampledb"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="5"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hiberante.format_sql">true</prop>
</props>
</property>
<!--
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
-->
<property name="mappingLocations" value="classpath:cn/bean/*.hbm.xml"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="NEVER"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* cn.dao.impl.UserDaoHibernateImpl.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
spring-core.xml
<context:annotation-config />
<context:component-scan base-package="cn" />
spring-servlet.xml
<!-- 注解扫描的包 -->
<context:component-scan base-package="cn" />
<mvc:annotation-driven />
<mvc:resources location="/img/" mapping="/img/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<!-- 视图解释类 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
<property name="suffix" value=".jsp"></property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>text/json;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 设置字符集 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 控制Session的开关 -->
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Controller
@Controller
@RequestMapping({"/", "/user"})
public class UserCURDController {
@Autowired
UserService us;
private static final Log logger = LogFactory.getLog(UserCURDController.class);
@RequestMapping(value={"/","/show"})
public String getAllUsers(Model model)
{
List<User> users = us.findAllUser();
model.addAttribute("users", users);
return "users/UserList";
}
@RequestMapping(value={"/","/show"}, params="json")
public @ResponseBody List<User> getAllUsers()
{
List<User> users = us.findAllUser();
return users;
}
@RequestMapping(value="/show/{id}", method=RequestMethod.GET)
public String getUser(@PathVariable("id") Integer id, Model model)
{
model.addAttribute("user", us.findUserById(id));
return "users/UserDetail";
}
@RequestMapping(value="/show/{id}", method=RequestMethod.GET, params="json")
public @ResponseBody User getUser(@PathVariable("id") Integer id)
{
User u =us.findUserById(id);
return u;
}
@RequestMapping(value="/edit/{id}", method=RequestMethod.GET)
public String editUser(@PathVariable("id") Integer id, Model model)
{
User u = us.findUserById(id);
model.addAttribute("user", u);
return "users/UserEditForm";
}
@RequestMapping(value = "/edit", method=RequestMethod.POST)
public String editUser(User u, Model model) {
System.out.println(u.getName()+" "+u.getPassword());
us.updateUser(u);
return "redirect:/user/show";
}
@RequestMapping(value="/add", method=RequestMethod.GET)
public String addUser(Model model){
model.addAttribute("user", new User());
return "users/UserAddForm";
}
@RequestMapping(value="/add", method=RequestMethod.POST)
public String addUser(User u, BindingResult ret, HttpServletResponse response) throws BindException
{
if(ret.hasErrors()){
throw new BindException();
}
us.addUser(u);
return "redirect:/user/show";
}
@RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
public String delete(@PathVariable("id") Integer id)
{
User u = us.findUserById(id);
us.deleteUser(u);
return "redirect:/user/show";
}
}