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="/"></property>
  <property name="suffix" value=".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="oracle.jdbc.driver.OracleDriver"></property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:demo">
</property>
<property name="username" value="test"></property>
<property name="password" value="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;
}
}

附件是项目源码

时间: 2024-08-07 23:04:04

springmvc+spring+hibernate配置过程的相关文章

SSM(springmvc+spring+mybatis)整合过程

问题?SSM(springmvc+spring+mybatis)整合过程 一.SSM框架比较S2SH框架的优缺点比较 站在个人使用这两个框架的观点儿上来说的话,我觉得这个不能定死的说,常用的S2SH,SpringMVC等,还可以考虑jFinal. 首先说S2SH,这个是用的比较多的,特别是在中小型项目中,针对大型项目就不行了,是Struts和hibernate过于庞大,过于重量级,项目大了之后配置文件多的也是麻烦事,配置文件多了之后一方面不好操作,另外比较蛋疼的是没法断点调试,特别是对底层数据库

Maven搭建springMVC+spring+hibernate环境

这次不再使用struts2做控制器,采用spring自己的springMVC框架实现. 首先,改写pom.xml文件,不需要struts2的相关jar了. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apa

SpringMVC+Spring+Hibernate的小样例

Strusts2+Spring+Hibernate尽管是主流的WEB开发框架,可是SpringMVC有越来越多的人使用了.确实也很好用.用得爽! 这里实现了一个SpringMVC+Spring+Hibernate的小样例.凝视都在代码里面. 项目各包的结构例如以下图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWlhbnR1amF2YQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve

用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能(二)

前一篇写的有些多,大家先看前一篇,传送门 具体的资源已将上传到资源了. 附地址:MySQL.zip启动 用Maven整合SpringMVC+Spring+Hibernate 框架 上文我们直接搭建前的准备和资源配置都写好了,下面进入具体代码编写.承接上文的小3 3.我习惯建立接口,这样对整个项目感觉更合理. (1.)建立IBaseService(业务逻辑层,有的习惯写成BaseServiceI)都可以,都是标注接口的,我只是简单的贴下代码 package com.jesus.sshframewo

用Maven整合SpringMVC+Spring+Hibernate 框架

用Maven整合SpringMVC+Spring+Hibernate 框架, 实现简单的插入数据库数据 一.搭建开始前的准备 1.打开MyEclipse新建Maven项目.File>New>Other(或Ctrl+N)>Maven Project:然后我们用default Workspace就行了(注意Location的路径,区分目录名和项目名):然后再Filter中输入webapp,我们选择org.apache.maven.archetypes 下的maven-archetype-we

SpringMVC+Spring+Hibernate的小例子

Strusts2+Spring+Hibernate虽然是主流的WEB开发框架,但是SpringMVC有越来越多的人使用了,确实也非常好用,用得爽! 这里实现了一个SpringMVC+Spring+Hibernate的小例子.注释都在代码里面. 项目各包的结构如下图: 1, 首先是pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLS

Spring hibernate配置中mappingLocations、mappingDirecto

mappingLocations.mappingDirectoryLocations与mappingJarLocations 区别 由于spring对hibernate配置文件hibernate.cfg.xml的集成相当好, 所以,在项目中我一直使用spring的org.springframework.orm.hibernate.LocalSessionFactoryBean来取代hibernate.cfg.xml文件的功能 LocalSessionFactoryBean有好几个属性用来查找hi

Springmvc+Spring+Hibernate搭建方法及实例

Springmvc+Spring+Hibernate搭建方法及实例

spring(一)--spring/springmvc/spring+hibernate(mybatis)配置文件

这篇文章用来总结一下spring,springmvc,spring+mybatis,spring+hibernate的配置文件 1.web.xml 要使用spring,必须在web.xml中定义分发器等信息,基本的配置信息如下: <?xml version="1.0" encoding= "UTF-8"?> <web-app version= "3.0" xmlns="http://java.sun.com/xml/n