首页 > 代码库 > springmvc+spring+hibernate配置过程

springmvc+spring+hibernate配置过程

开发工具:eclipse

项目目录:技术分享


jar包:

技术分享


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" metadata-complete="true">
  <display-name>SMSH</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:hanxuanyuan/config/spring-*.xml</param-value>
  </context-param>
  
  
   <!-- 配置Spring的用于初始化容器对象的监听器 -->
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 中央控制器  -->
  <servlet>
  <servlet-name>springMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:hanxuanyuan/config/springmvc-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-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>  
  
</web-app>

springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 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.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
       
    <!-- 启用注解 -->
    <mvc:annotation-driven/>
    
   <!--  静态资源访问 --> 
   <!--  <mvc:resources location="/js/" mapping="/js/**" /> -->
    
    <!-- 扫描包 -->
    <context:component-scan base-package="hanxuanyuan.controller" />
    
  <!-- 视图配置 -->
  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="http://www.mamicode.com/"></property>
  <property name="suffix" value="http://www.mamicode.com/.jsp"></property>
  </bean>
  
</beans>


spring-hibernate.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--  自动扫描与装配bean -->
<context:component-scan base-package="hanxuanyuan"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="http://www.mamicode.com/oracle.jdbc.driver.OracleDriver"></property>
<property name="url"
value="http://www.mamicode.com/jdbc:oracle:thin:@localhost:1521:demo">
</property>
<property name="username" value="http://www.mamicode.com/test"></property>
<property name="password" value="http://www.mamicode.com/test"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
 org.hibernate.dialect.Oracle10gDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="configLocations">
<list>
<value>
classpath*:hanxuanyuan/config/hibernate.cfg.xml
</value>
</list>
</property>  
</bean>
  <!--   配置声明式事务管理(采用注解的方式)-->
 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
 <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 
 <bean id="txBase" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
 <property name="transactionManager" ref="transactionManager"></property>
 <property name="transactionAttributes">
 <props>
 <prop key="add*">PROPAGATION_REQUIRED,-Exception </prop>
 <prop key="update*">PROPAGATION_REQUIRED,-Exception </prop>
 <prop key="insert*">PROPAGATION_REQUIRED,-Exception </prop>
 <prop key="modify*">PROPAGATION_REQUIRED,-Exception </prop>
 <prop key="get*">PROPAGATION_NEVER</prop>
 </props>
 </property>
 </bean>
 
</beans>

spring-bean.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  <bean id="userDao" class="hanxuanyuan.Dao.UserDaoImpl">
   <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>
 
    <bean id="userServiceBase" class="hanxuanyuan.service.UserServiceImpl">
   <property name="userDao" ref="userDao"></property>
  </bean>
  
  <bean id="userService" parent="txBase">
  <property name="target" ref="userServiceBase"></property>
  </bean>
 
</beans>


hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <mapping class="hanxuanyuan.entity.GYh"/>
    </session-factory>
</hibernate-configuration>


UserController.java

package hanxuanyuan.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import hanxuanyuan.entity.GYh;
import hanxuanyuan.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource(name="userService")
private UserService userService ;
@RequestMapping("/list")
public String list(){
return "userlist" ;
}
@RequestMapping("/add")
public String add(GYh gyh,Model model ,HttpServletRequest request){
userService.addUser(gyh);
List<GYh> list = userService.queryList() ;
model.addAttribute("list", list);
return "userlist" ;
}
}


后台向前台传值方式:放入model.addAtribute()中


entity: GYh.java

package hanxuanyuan.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
/**
 * GYh entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name="G_YH")
public class GYh implements java.io.Serializable {
// Fields
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator="payablemoney_seq")  
@SequenceGenerator(name="payablemoney_seq", sequenceName="SEQ_ID")  
private Long id;
private String yhlx01;
private String yhlx02;
private String xm;
private String dm;
private String dw;
private String sfz;
private String jgz;
private String yjgz;
private String grdh;
private String mm;
private String sm;
private String cjsj;
private String zt;
private Long ryid;
private String dwid;
private String zzmc;
// Constructors
/** default constructor */
public GYh() {
}
/** full constructor */
public GYh(String yhlx01, String yhlx02, String xm, String dm, String dw,
String sfz, String jgz, String yjgz, String grdh, String mm,
String sm, String cjsj, String zt,Long ryid, String dwid, String zzmc) {
this.yhlx01 = yhlx01;
this.yhlx02 = yhlx02;
this.xm = xm;
this.dm = dm;
this.dw = dw;
this.sfz = sfz;
this.jgz = jgz;
this.yjgz = yjgz;
this.grdh = grdh;
this.mm = mm;
this.sm = sm;
this.cjsj = cjsj;
this.zt = zt;
this.ryid = ryid;
this.dwid = dwid;
this.zzmc = zzmc;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getYhlx01() {
return this.yhlx01;
}
public void setYhlx01(String yhlx01) {
this.yhlx01 = yhlx01;
}
public String getYhlx02() {
return this.yhlx02;
}
public void setYhlx02(String yhlx02) {
this.yhlx02 = yhlx02;
}
public String getXm() {
return this.xm;
}
public void setXm(String xm) {
this.xm = xm;
}
public String getDm() {
return this.dm;
}
public void setDm(String dm) {
this.dm = dm;
}
public String getDw() {
return this.dw;
}
public void setDw(String dw) {
this.dw = dw;
}
public String getSfz() {
return this.sfz;
}
public void setSfz(String sfz) {
this.sfz = sfz;
}
public String getJgz() {
return this.jgz;
}
public void setJgz(String jgz) {
this.jgz = jgz;
}
public String getYjgz() {
return this.yjgz;
}
public void setYjgz(String yjgz) {
this.yjgz = yjgz;
}
public String getGrdh() {
return this.grdh;
}
public void setGrdh(String grdh) {
this.grdh = grdh;
}
public String getMm() {
return this.mm;
}
public void setMm(String mm) {
this.mm = mm;
}
public String getSm() {
return this.sm;
}
public void setSm(String sm) {
this.sm = sm;
}
public String getCjsj() {
return this.cjsj;
}
public void setCjsj(String cjsj) {
this.cjsj = cjsj;
}
public String getZt() {
return this.zt;
}
public void setZt(String zt) {
this.zt = zt;
}
public Long getRyid() {
return ryid;
}
public void setRyid(Long ryid) {
this.ryid = ryid;
}
@Override
public String toString() {
return "GYh [id=" + id + ", yhlx01=" + yhlx01 + ", yhlx02=" + yhlx02 + ", xm=" + xm + ", dm=" + dm + ", dw="
+ dw + ", sfz=" + sfz + ", jgz=" + jgz + ", yjgz=" + yjgz + ", grdh=" + grdh + ", mm=" + mm + ", sm="
+ sm + ", cjsj=" + cjsj + ", zt=" + zt + ", ryid=" + ryid + "]";
}
public String getDwid() {
return dwid;
}
public void setDwid(String dwid) {
this.dwid = dwid;
}
public String getZzmc() {
return zzmc;
}
public void setZzmc(String zzmc) {
this.zzmc = zzmc;
}
}


附件是项目源码

本文出自 “linux菜鸟” 博客,请务必保留此出处http://asura1992.blog.51cto.com/8159058/1918666

springmvc+spring+hibernate配置过程