温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springmvc+spring+hibernate配置过程

发布时间:2020-08-14 14:32:04 来源:网络 阅读:828 作者:o凤舞九天o 栏目:开发技术

开发工具:eclipse

项目目录:springmvc+spring+hibernate配置过程


jar包:

springmvc+spring+hibernate配置过程


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;
}
}


项目源码链接:http://down.51cto.com/data/2302295

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI