spring2.5+jpa(hibernate)+struts2

本文介绍如何搭建Java持久化API (JPA) 环境,并整合Spring框架,以及如何配置Struts2框架以实现MVC架构。文中详细展示了配置文件的编写过程,包括persistence.xml和beans.xml等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.搭建jpa环境

   1)加入jar包

   2)在src下面建立文件夹META-INF,在文件下面建立persistence.xml(固定用法)

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
 version="1.0">
 <persistence-unit name="vissul" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  <properties>
   <property name="hibernate.dialect"
    value="org.hibernate.dialect.SQLServerDialect" />
   <property name="hibernate.max_fetch_depth" value="3" />
   <property name="hibernate.hbm2ddl.auto" value="update" />
  </properties>
 </persistence-unit>
</persistence>
2.继承spring

  1)加入jar包

  2)在src下面建立beans.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:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 <context:annotation-config />
 <context:component-scan base-package="com.mybaoku"/>
 <context:property-placeholder location="classpath:jdbc.properties" />

 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="${driverClassName}"></property>
  <property name="url" value="${url}"></property>
  <property name="username" value="${username}"></property>
  <property name="password" value="${password}"></property>
  <property name="initialSize" value="${initialSize}"></property>
  <property name="maxActive" value="${maxActive}"></property>
  <property name="maxIdle" value="${maxIdle}"></property>
  <property name="minIdle" value="${minIdle}"></property>
 </bean>

 <bean id="entityManagerFactory"
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  <property name="persistenceXmlLocation"
   value="classpath:META-INF/persistence.xml">
  </property>
  <!-- 运行植入 -->
  <property name="loadTimeWeaver">
   <bean 
    class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver">
   </bean>
  </property>
 </bean>

 <bean id="txManager"
  class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory"
   ref="entityManagerFactory" />
 </bean>

 <tx:annotation-driven transaction-manager="txManager" />
</beans>

 3)jdbc 配置文件jdbc.properties

driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
username=sa
password=
url=jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=baoku
initialSize=1
maxActive=100
maxIdle=8
minIdle=1\t


3.加入struts2

 1)在src下面建立struts。xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
    如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
    <constant name="struts.action.extension" value="do"/>
    <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
    <constant name="struts.serve.static.browserCache" value="false"/>
    <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
    <constant name="struts.configuration.xml.reload" value="true"/>
    <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
    <constant name="struts.devMode" value="true" />
     <!-- 默认的视图主题 -->
    <constant name="struts.ui.theme" value="simple" />
    <!-- struts2 的action 由spring创建 -->
    <constant name="struts.objectFactory" value="spring" />
   
    <package name="account"  extends="struts-default" namespace="/control">
     <global-results>
      <result name="error">/WEB-INF/jsp/util/error.jsp</result>
     </global-results>
     <action name="account_*" class="loginAction" method="{1}">
      <result name="loginSuccess">/WEB-INF/jsp/account/accountList.jsp</result>
      <result name="login">/login.jsp</result>
     </action>
    </package>
</struts>
2)整合spring和struts,配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.xml</param-value>
 </context-param>
 <!-- 对Spring容器进行实例化 -->
 <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
   </filter-mapping>
  <!--
  <filter>
         <filter-name>OpenSessionInViewFilter</filter-name>
         <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
 </filter>
 <filter-mapping>
         <filter-name>OpenSessionInViewFilter</filter-name>
         <url-pattern>/*</url-pattern>
 </filter-mapping>
   -->
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>
4。用junit测试框架

 public static ApplicationContext context;
 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
  try {
    context = new ClassPathXmlApplicationContext("beans.xml");
   
  } catch (RuntimeException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 @Test public void initial(){
 }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值