加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Spring和Hibernate的整合操作示例

发布时间:2020-05-24 13:49:16 所属栏目:Java 来源:互联网
导读:Spring和Hibernate的整合操作示例 本文实例讲述了Spring和Hibernate的整合操作.分享给大家供大家参考,具体如下: 一 web配置 ?xml version=1.0 encoding=GBK? web-app xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns=http://xmlns.jcp.org/xml/ns

本文实例讲述了Spring和Hibernate的整合操作。分享给大家供大家参考,具体如下:

一 web配置

<?xml version="1.0" encoding="GBK"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1">
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

二 applicationContext.xml

<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
  <!-- 定义数据源Bean,使用C3P0数据源实现,并注入数据源的必要信息 -->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close"
    p:driverClass="com.mysql.jdbc.Driver"
    p:jdbcUrl="jdbc:mysql://localhost/spring"
    p:user="root"
    p:password="32147"
    p:maxPoolSize="40"
    p:minPoolSize="2"
    p:initialPoolSize="2"
    p:maxIdleTime="30"/>
  <!-- 定义Hibernate的SessionFactory,SessionFactory需要依赖数据源,注入dataSource -->
  <bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
    p:dataSource-ref="dataSource">
    <!-- mappingResources用来列出全部映射文件 -->
    <property name="annotatedClasses">
      <list>
        <!-- 以下用来列出所有的PO类-->
        <value>org.crazyit.booksys.domain.Book</value>
      </list>
    </property>
    <!-- 定义Hibernate SessionFactory的属性 -->
    <property name="hibernateProperties">
      <props>
        <!-- 指定Hibernate的连接方言 -->
        <prop key="hibernate.dialect">
          org.hibernate.dialect.MySQL5InnoDBDialect</prop>
        <!--是否根据Hiberante映射创建数据表 -->
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.format_sql">true</prop>
      </props>
    </property>
  </bean>
  <!-- 定义Service组件,并将DAO组件注入Service组件 -->
  <bean id="bookService" class="org.crazyit.booksys.service.impl.BookServiceImpl"
    p:bookDao-ref="bookDao"/>
  <!-- 定义DAO组件,并将SessionFactory注入DAO组件 -->
  <bean id="bookDao" class="org.crazyit.booksys.dao.impl.BookDaoHibernate4"
    p:sessionFactory-ref="sessionFactory"/>
  <!-- 配置Hibernate的局部事务管理器,使用HibernateTransactionManager类 -->
  <!-- 该类是PlatformTransactionManager接口针对采用Hibernate的特定实现类 -->
  <!-- 配置HibernateTransactionManager需依赖注入SessionFactory -->
  <bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"
    p:sessionFactory-ref="sessionFactory"/>
<!-- 配置事务增强处理Bean,指定事务管理器 -->
<tx:advice id="txAdvice"
  transaction-manager="transactionManager">
  <!-- 用于配置详细的事务定义 -->
  <tx:attributes>
    <!-- 所有以'get'开头的方法是read-only的 -->
    <tx:method name="get*" read-only="true"/>
    <!-- 其他方法使用默认的事务设置,指定超时时长为5秒 -->
    <tx:method name="*" isolation="DEFAULT"
      propagation="REQUIRED" timeout="5"/>
  </tx:attributes>
</tx:advice>
<!-- AOP配置的元素 -->
<aop:config>
  <!-- 配置一个切入点 -->
  <aop:pointcut id="myPointcut" expression="bean(bookService)"/>
  <!-- 指定在myPointcut切入点应用txAdvice事务增强处理 -->
  <aop:advisor advice-ref="txAdvice"
    pointcut-ref="myPointcut"/>
</aop:config>
</beans>

三 Struts配置

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
  <!-- 配置了系列常量 -->
  <constant name="struts.i18n.encoding" value="GBK"/>
  <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  <constant name="struts.devMode" value="true"/>
  <package name="lee" extends="struts-default">
    <action name="addBook" class="org.crazyit.booksys.action.BookAction"
      method="add">
      <!-- 添加图书成功,列出所有图书 -->
      <result type="chain">listBooks</result>
      <!-- 添加图书失败,跳转到添加图书的表单页 -->
      <result name="error">/WEB-INF/content/bookForm.jsp</result>
    </action>
    <action name="listBooks" class="org.crazyit.booksys.action.BookAction"
      method="list">
      <result>/WEB-INF/content/listBooks.jsp</result>
    </action>
    <action name="deleteBook" class="org.crazyit.booksys.action.BookAction"
      method="delete">
      <result type="chain">listBooks</result>
    </action>
    <!-- 让用户直接访问该应用时列出所有视图页面 -->
    <action name="*">
      <result>/WEB-INF/content/{1}.jsp</result>
    </action>
  </package>
</struts>

四 视图

1 bookForm.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>添加图书</title>
</head>
<body>
<h3>添加图书</h3>
<s:form action="addBook">
  <s:textfield name="book.name" label="书名"/>
  <s:textfield name="book.price" label="价格"/>
  <s:textfield name="book.author" label="作者"/>
  <tr align="center">
    <td colspan="2">
    <s:submit value="添加" theme="simple"/>
    <s:reset value="重设" theme="simple"/>
    </td>
  </tr>
</s:form>
</body>
</html>

2 listBooks.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>全部图书</title>
</head>
<body>
<h3>全部图书</h3>
<table width="640" border="1">
  <s:iterator value="books" var="b">
    <tr>
      <td><s:property value="name"/></td>
      <td><s:property value="price"/></td>
      <td><s:property value="author"/></td>
      <td><a href="${pageContext.request.contextPath}/deleteBook?id=${b.id}" rel="external nofollow" >删除</a></td>
    </tr>
  </s:iterator>
</table>
</body>
</html>

五 domain

package org.crazyit.booksys.domain;
import javax.persistence.*;
@Entity
@Table(name="book_inf")
public class Book
{
  @Id @Column(name="book_id")
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private Integer id;
  @Column(name="book_name")
  private String name;
  private double price;
  private String author;
  public Integer getId()
  {
    return id;
  }
  public void setId(Integer id)
  {
    this.id = id;
  }
  public String getName()
  {
    return name;
  }
  public void setName(String name)
  {
    this.name = name;
  }
  public double getPrice()
  {
    return price;
  }
  public void setPrice(double price)
  {
    this.price = price;
  }
  public String getAuthor()
  {
    return author;
  }
  public void setAuthor(String author)
  {
    this.author = author;
  }
}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读