码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC&hibernate

时间:2015-05-09 11:54:13      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

一、web.xml

<!-- 通过上下文参数指定spring配置文件的位置 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:beans.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:springmvc.xml</param-value>
  </init-param>
 </servlet> 
  
 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.action</url-pattern>
 </servlet-mapping> 

二、数据源beans.xml

<!-- 注解驱动 -->
 <mvc:annotation-driven/>
 <!-- 组件扫描 -->
 <context:component-scan base-package="com.hcm.controller"></context:component-scan>
 
 <!-- 定义数据源 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="driverClass" value="com.mysql.jdbc.Driver" />
  <property name="jdbcUrl" value="jdbc:mysql://localhost/springmvc" />
  <property name="user" value="root" />
  <property name="password" value="root" />
  
  <property name="initialPoolSize" value="10"/>
  <property name="maxPoolSize" value="50"/>
  <property name="minPoolSize" value="10"/>
 </bean>
 
 <bean id="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  
  <!-- hibernate映射文件的位置 -->
  <property name="mappingDirectoryLocations">
   <value>classpath:com/hcm/domain/</value>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.Dialect">org.hibernate.dialect.MySQL5Dialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.hbm2ddl">update</prop>
   </props>
  </property>
 </bean>
 
 <!-- 事务管理器 -->
 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sf"></property>
 </bean>
 
 <!-- 配置事物的传播特性 (事物通知)-->
 <tx:advice id="txAdvice" transaction-manager="txManager">
  <tx:attributes>
   <tx:method name="save*" propagation="REQUIRED"/>
   <tx:method name="delete*" propagation="REQUIRED"/>
   <tx:method name="update*" propagation="REQUIRED"/>
   <tx:method name="find*" read-only="true"/>
   <tx:method name="*" read-only="true"/>
  </tx:attributes>
 </tx:advice>
 
 <aop:config>
  <aop:advisor pointcut="execution(* com.hcm.service.*.*(..))" advice-ref="txAdvice"/>
 </aop:config>

三、mvc模型解析器springmvc.xml

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsps/"></property>
  <property name="suffix" value=".jsp"></property>
 </bean>

四、自定义controller:

package com.hcm.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.hcm.domain.Person;
@Controller
@RequestMapping(value="/home")
public class HomeController {
 
 @RequestMapping(value="/hello.action", method=RequestMethod.GET)
 public String hello(String id, String name, String address, Map<String, Object> model) {
  model.put("id", id);
  model.put("name", name);
  model.put("address", address);
  return "hello";
 }
 
 @RequestMapping(value="/person.action")
 public String person(Person person, Map<String, Object> model) {
  model.put("person", person);
  return "person";
 }
 
 /**
  * 加上@RequestParam会出现HTTP ERROR 400的报错
  * @param uid
  * @param uname
  * @param address
  * @param model
  * @return
  */
 @RequestMapping(value="/helloH.action")
 public String hello2(@RequestParam(value="id") String uid, @RequestParam(value="name") String uname, String address, HttpServletRequest req) {
  req.setAttribute("id", uid);
  req.setAttribute("name", uname);
  req.setAttribute("address", address);
  return "hello2";
 }
 
}

五、代码下载:

链接:http://pan.baidu.com/s/1eQ3vAOq 密码:wihz

springMVC&hibernate

标签:

原文地址:http://my.oschina.net/sniperLi/blog/412535

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!