zoukankan      html  css  js  c++  java
  • SSH项目整合

    其实框架的整合无非就是jar包和配置文件:

    struts2、spring、Hibernate这三个框架,分清楚什么作用就好配置了。

    jar包我们就不说了,这里看下配置文件吧:

    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.devMode" value="true" />
        <!-- 把扩展名配置为action -->
        <constant name="struts.action.extension" value="action" />
        <!-- 把主题配置为simple -->
        <constant name="struts.ui.theme" value="simple" />
    	
       
        <package name="default" namespace="/" extends="struts-default">
          
    		<!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 -->
    		<!-- 当Struts2与Spring整合后,class属性可以写bean的名称 -->
    		<action name="test" class="testAction">
    			<result name="success">/test.jsp</result>
    		</action>
    
        </package>
    
        <!-- Add packages here -->
    
    </struts>
    

      配置这个后,需要在web.xml中配置struts的过滤器,至于为什么呢?因为它是与前台页面进行交互的工具,需要对用户输入的地址链接进行一些过滤功能。一个web项目读取配置文件的顺序是先从web.xml中读取的,也就是说我们需要将struts与spring都在web.xm中设置好,让他们可以访问到这两个的配置文件。

    WEB.XML的内容:

    <?xml version="1.0" encoding="UTF-8"?>
    <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" id="WebApp_ID" version="3.1">
      <display-name>itcastoa</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
    	<!-- 配置Spring的用于初始化容器对象的监听器 -->
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:applicationContext*.xml</param-value>
    	</context-param>
    
    
    	<!-- 配置Struts2的核心的过滤器 -->
    	<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>
    

      Spring的配置文件:applicationContext.xml或者是beans.xml都可以:这里将hibernate与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"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	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
    				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    	<!-- 自动扫描与装配bean -->
    	<context:component-scan base-package="cn.itcast.oa"></context:component-scan>
    	<!-- 导入外部的properties文件 -->
    	<context:property-placeholder location="classpath:jdbc.properties"/>
    
    
    	<!-- 配置SessionFactory -->
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<!-- 指定hibernate的配置文件位置 -->
    		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    		<!-- 配置c3p0数据库连接池 -->
    		<property name="dataSource">
    			<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
    				<!-- 数据连接信息 -->
    				<property name="jdbcUrl" value="${jdbcUrl}"></property>
    				<property name="driverClass" value="${driverClass}"></property>
    				<property name="user" value="${user}"></property>
    				<property name="password" value="${password}"></property>
    				<!-- 其他配置 -->
    				<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
    				<property name="initialPoolSize" value="3"></property>
    				<!--连接池中保留的最小连接数。Default: 3 -->
    				<property name="minPoolSize" value="3"></property>
    				<!--连接池中保留的最大连接数。Default: 15 -->
    				<property name="maxPoolSize" value="5"></property>
    				<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
    				<property name="acquireIncrement" value="3"></property>
    				<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
    				<property name="maxStatements" value="8"></property>
    				<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
    				<property name="maxStatementsPerConnection" value="5"></property>
    				<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
    				<property name="maxIdleTime" value="1800"></property>
    			</bean>
    		</property>
    	</bean>
    
    
    	<!-- 配置声明式事务管理(采用注解的方式) -->
    	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory"></property>
    	</bean>
    	<tx:annotation-driven transaction-manager="txManager"/>
    
    
    
    	
    	</beans>
    

      hibernate主要与数据库打交道:

    hhibernate.cfg.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!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>
    
    	<!-- 1,数据库连接信息 -->
    	<property name="dialect">
    		org.hibernate.dialect.MySQL5InnoDBDialect
    	</property>
    	<!-- 
    		<property name="connection.url">jdbc:mysql:///itcastoa0720</property>
    		<property name="connection.driver_class">com.jdbc.mysql.Driver</property>
    		<property name="connection.username">root</property>
    		<property name="connection.password">root</property>
    	-->
    
    	<!-- 2,其他配置 -->
    	<property name="show_sql">true</property>
    	<property name="hbm2ddl.auto">update</property>
    
    	<!-- 3,导入映射文件 -->
        <mapping resource="cn/itcast/oa/domain/Item.hbm.xml" />
    
    
    </session-factory>
    
    </hibernate-configuration>
    

      hibernate管理po类和数据库打交道还需要实体类的hibernate配置文件:

    通常是**.hbm.xml:这些都可以在帮助文档找到。

    另外数据库的url和用户名密码这里也要用db.properties来命名:

    jdbcUrl = jdbc:mysql:///itcastoa0720
    driverClass = com.mysql.jdbc.Driver
    user = root
    password = root

     。这样就相当于将三大框架整合完成。

  • 相关阅读:
    linux-文件
    字符串函数
    函数
    内存管理
    静态库、动态库文件制作
    Makefile 待完善
    指针
    开发板GEC6816环境搭建,使用VS code
    C语言数组
    连接开发板下载程序
  • 原文地址:https://www.cnblogs.com/fengli9998/p/6497170.html
Copyright © 2011-2022 走看看