搭建spring、struts、jpa(hibernate)、ibatis框架详解

搭建spring、struts、jpa(hibernate)、ibatis框架详解

1.本文选用spring3.06发行版,首先下载spring3.0.6版的jar包;
2.下载struts2.3.24版本,struts2的jar可以在struts2官网下载完整的source demo,里面包含了struts2整合spring的demo;
3.下载ibatis2.3.4的jar;
4.spring的版本不一样,支持的hibernate和ibatis版本也不一样。

一、首先新建一个web工程,在eclipse下新建web工程并命名为di。
二、复制相关jar到WEB-INF的lib下,这一步可以选择引入jar的引用而不复制。复制到lib下方便web工程的迁移。
三、配置spring的配置文件,在WEB-INF下新建applicationContext.xml文件,内容如下
这里拆分了spring的配置文件,同时开启了spring的资源读取和引入注释配置。

<?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"
    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-3.0.xsd"
>
    <context:property-placeholder location="classpath:messages_zh_CN.properties" ignore-unresolvable="true"/>
    <context:property-placeholder location="classpath:messages.properties" ignore-unresolvable="true"/>
    <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
    <context:annotation-config/>
    <import resource="spring-beans.xml" />
    <import resource="spring-ibatis.xml" />
    <import resource="spring-jpa.xml" />
    <import resource="spring-service.xml" />
    <import resource="spring-struts.xml" />
    <!--<import resource="spring-ibatis.xml" />-->
</beans>

四、配置struts2配置文件,在src下新建struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!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.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.objectFactory" value="spring" />
    <constant name="struts.action.excludePattern" value="/resources/.*?" />
    <constant name="struts.custom.i18n.resources" value="messages" />
    <package name="default" extends="struts-default">
        <global-results>
            <result name="error">/WEB-INF/jsp/error.jsp</result>
        </global-results>   
        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception"
                result="error" />
        </global-exception-mappings>    
        <action name="helloAction" class="helloAction">
            <!-- <interceptor-ref name="mydefault" /> -->
        </action>
    </package>
    <!-- Add packages here -->
</struts>

五、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
    <display-name>di</display-name>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <filter>
        <filter-name>messages</filter-name>
        <filter-class>com.di.util.MessagesFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>messages</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <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>
    <welcome-file-list>
        <welcome-file>login.html</welcome-file>
    </welcome-file-list>
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
</web-app>

六、配置spring-jpa,在WEB-INF下新建spring-jpa.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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            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-3.0.xsd"
>
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    <!-- 利用Spring的实体管理器工厂来创建JPA实体管理器 -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    >
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="MYSQL" />
                <property name="showSql" value="true" />
                <!-- <property name="generateDdl" value="true" /> -->
            </bean>
        </property>
    </bean>
    <!-- 声明一个Spring提供的JPA事务管理器,传入的参数是Spring中的实体管理器工厂 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <bean id="entityManager"
        class="org.springframework.orm.jpa.support.SharedEntityManagerBean"
    >
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <bean id="dao" class="com.di.jpa.dao.Dao" autowire="byName" />
</beans>

七、配置ibatis,在src下新建包com.ibatis,新建一个pojo,Student,student.xml文件,配置spring-ibatis.xml文件
student.xml配置

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE sqlMap
   PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
   "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Student">
    <select id="getStudentById" resultClass="com.ibatis.Student"
        parameterClass="int"
    > select id,firstname,lastname from student where id=#value#
    </select>
    <insert id="insertStudent" parameterClass="com.ibatis.Student"> insert into
        student(firstname,lastname) values(#firstname#,#lastname#)
    </insert>
</sqlMap>

SalMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
    <settings useStatementNamespaces="true" />
    <sqlMap resource="com/ibatis/student.xml" />
</sqlMapConfig>

spring-ibatis.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    >
        <property name="locations" value="classpath:jdbc.properties" />
    </bean>
    <!--根据dataSource和configLocation创建一个SqlMapClient-->
  <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation">
      <value>classpath:sqlMapConfig.xml</value>
    </property>
    <property name="dataSource">
      <ref bean="dataSource" />
    </property>
  </bean>

  <!--根据sqlMapClien创建一个SqlMapClient模版类-->
  <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
    <property name="sqlMapClient">
      <ref bean="sqlMapClient" />
    </property>
  </bean>

  <!--将上面的模版类织入到我们的DAO对象中-->
  <bean id="ibatisDao" class="com.di.spring.ibatis.dao.IbatisDao" autowire="byName"/>
</beans>

八、配置资源全球化文件、log4j、及jdbc连接配置。略


参考链接:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值