ssh框架整合完整版

一、導入jar包java

  能夠在src下添加一個log4j.properties文件來記錄日誌web

二、加入實體類+映射文件spring

映射:從類入手class+屬性sql

  a、映射的頭文件在:hibernate3.jar-->org.hibernate-->hibernate-mapping-3.0.dtdexpress

  b、type=「java.lang.String"根據屬性來更改+column(加長度的時候要獨立出來)apache

注意:若是有遇到entity not found   就要想到路徑錯誤,映射文件中有那個package=」包名「還有就是在hibernate.cfg.xml的mappling那路徑有沒有正確tomcat

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="po">
    <class name="CheckResult" table="biz_check_result">
        <id name="id" type="long">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="sheetType" type="string">
            <column name="sheet_type" length="20" not-null="true">
                <comment>單據類型</comment>
            </column>
        </property>
        <property name="sheetId" type="long">
            <column name="sheet_id" not-null="true">
                <comment>單據編號</comment>
            </column>
        </property>
        <property name="checkTime" type="date">
            <column name="check_time" length="19" not-null="true">
                <comment>審覈時間</comment>
            </column>
        </property>
        <property name="type" type="string">
            <column name="type" length="20" not-null="true">
                <comment>審覈類型</comment>
            </column>
        </property>
        <many-to-one name="checker" class="Employee" fetch="select">
            <column name="checker_sn" length="20" not-null="true">
                <comment>審覈人</comment>
            </column>
        </many-to-one>
        <property name="result" type="string">
            <column name="result" length="20" not-null="true">
                <comment>審覈結果</comment>
            </column>
        </property>
        <property name="comment" type="string">
            <column name="comment">
                <comment>審覈意見</comment>
            </column>
        </property>
    </class>
</hibernate-mapping>
映射文件

hibernate.cfg.xml文件。頭文件在:hibernate3.jar-->org.hibernate-->hibernate-configuration-3.0.dtdsession

hibernate.cfg.xml文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

<session-factory>
    <property name="connection.url">
        jdbc:oracle:thin:@localhost:1521:jbit
    </property>
    <property name="connection.username">rong</property>
    <property name="connection.password">rong</property>
    <property name="connection.driver_class">
        oracle.jdbc.driver.OracleDriver
    </property>
    <property name="dialect">
        org.hibernate.dialect.OracleDialect
    </property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    
    <mapping resource="po/Employee.hbm.xml" />
    <mapping resource="po/Position.hbm.xml" />
</session-factory>

</hibernate-configuration>

用spring的把hibernate.cfg.xml整合到applicationContext.xml文件中oracle

先配置dataSource找BasicDataSourceapp

而後在注入sessionFactory

applicationContext.xml文件剛開始寫入下面的代碼
<!--
dataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="jdbc:oracle:thin:@localhost:1521:jbit"></property> <property name="username" value="rong"></property> <property name="password" value="rong"></property> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> <!-- 最大鏈接數是100 --> <property name="maxActive" value="100"></property> <!-- 最大空閒數是10 --> <property name="maxIdle" value="10"></property> <!-- 最大等待時間毫秒爲單位 --> <property name="maxWait" value="10000"></property> </bean> <!-- SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- hibernateProperties 輔助參數 --> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.dialecg">org.hibernate.dialect.Oracle10gDialect</prop> </props> </property> <!-- 文件太多的話,不適合用 --> <!-- <property name="mappingResources"> <list> <value>cn/bdqn/jboa/entity/Department.hbm.xml</value> <value>cn/bdqn/jboa/entity/Dictionary.hbm.xml</value> <!-- <value>cn/bdqn/jboa/entity/Employee.hbm.xml</value> --> <value>cn/bdqn/jboa/entity/Position.hbm.xml</value> </list> </property> --> <!-- 指定的包的路徑 、找包下全部的映射文件--> <property name="mappingDirectoryLocations"> <list> <value>classpath:cn/bdqn/jboa/entity/</value> </list> </property> </bean>

 

 

三、建立dao、impl、biz包

四、a、建立applicationContext.xml-->Next-->CREATE SCHEMA FILE-->Next-->select xml catalog entry

  找beans-->next-->root element 改beans   其餘不打勾。而後把prefix的前綴p刪了

  add -->  aop文件和tx文件

 

  再添加p命名+context

  b、spring接管hibernate的建立工廠(使用hibernate.cfg.xml文件的方法,添加一個bean sessionFactory  找localSessionFactoryBean)

  注入一個屬性 configLocation( 當前配置)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    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.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!--用dbcp的話,這段就不須要了,換成dataSource -->
<!-- sessionFatory工廠 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

注入dao:

dao要工做的話,必需要一個sessionFactory(會話工廠),因此引用sessionFactory

dao繼承hibernateDaoSupport就能夠獲得模板的屬性,就可使用getHibernateTemplate()方法

<!-- dao -->
<bean id="employeeDao" class="dao.impl.EmployeeDaoImpl">
    <property name="sessionFatory" ref="sessionFactory"></property>
</bean>

注入biz:biz調用dao因此屬性那是dao

<!-- 注入biz -->
<bean id="employeeBiz" class="biz.impl.EmployeeBizImpl">
    <property name="employeeDao" ref="employeeDao"></property>
</bean>

事務:

首先聲明事務(多加註意,很容易忘了)+事務管理器transaction-manager(事務管理器的要求增刪改)

tx:advice事務屬性的配置增刪改  織入切面時候要引用id

<!-- 事務tx -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
 p:sessionFactory-ref="sessionFactory"></bean>
 <!-- 引用事務管理器 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
     <tx:attributes>
         <tx:method name="find*"/>//查詢用read-only=「true"會比較快點
         <tx:method name="get*"/>
         <tx:method name="save*"/>
     </tx:attributes>
 </tx:advice>

織入切面:

<!-- 切面織入 -->
 <aop:config>
     <aop:pointcut expression="execution(public * biz.*.*(..))" id="serviceMethod"/>
     <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
 </aop:config>

接下去就是把業務層biz給action了

建立action和jsp界面

public class EmployeeAction {
    //業務bean交給action
    private EmployeeBiz employeeBiz;
    //登陸的話須要驗證
    private Employee employee;
    //接收錯誤
    private String message;
     //set和get忽略
    //登陸
    public String login(){
        
        Employee result = employeeBiz.checkLogin(employee);
        if(result == null){
            this.message="用戶名或密碼錯誤";
            return "input";
        }else{
            ActionContext.getContext().getSession().put("employee", result);
            //職位
            ActionContext.getContext().getSession()
            .put("employee_position", result.getPosition().getNameEn());
            
            return "success";
        }
    }

新建個struts.xml  (頭文件去struts2-spring-plugin-2.3.16.3.jar找struts-plugin.xml)

struts和spring合做的結果(EmployeeAction類裏找到employeeBiz會去applicationContext.xml中去找同樣名字的biz,就這樣注入進去)

因此命名得規範,由於自動裝配時靠名字對應的。不然就改爲類型

<?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>
<package name="default" extends="struts-default" namespace="/">
        <action name="login" class="action.EmployeeAction" method="login">
            <result>/index.jsp</result>
            <result name="input">/login.jsp</result>
        </action>
    </package>
</struts>

讀取配置文件web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name></display-name>
  <!-- web.xml就是讀取配置文件 -->
  <!-- 首先找配置文件在哪裏 -->
  <!-- (不是必需要的,若是文件放在而且名字也是applicationContext就不用配下面的文件路徑在哪了/WEB-INF/applicationContext.xml) -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- 監聽器  -->
  <!-- 負責tomcat啓動後建立ContextLoaderListener讀取applicationContext文件-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 過濾器(爲了控制會話) -->
  <filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        </filter-class>
  </filter>
  <filter-mapping>//這個必定要放在struts2的映射前面,由於取決於誰在前,誰先執行
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>*.action</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>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

就能夠開始配置部署了。

相關文章
相關標籤/搜索