ssh框架整合完整版

1、导入jar包

  可以在src下添加一个log4j.properties文件来记录日志

2、加入实体类+映射文件

映射:从类入手class+属性

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

  b、type=“java.lang.String"根据属性来更改+column(加长度的时候要独立出来)

注意:如果有遇到entity not found   就要想到路径错误,映射文件中有那个package=”包名“还有就是在hibernate.cfg.xml的mappling那路径有没有正确

<?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.dtd

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文件中

先配置dataSource找BasicDataSource

然后在注入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>

 

 

3、创建dao、impl、biz包

4、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>

就可以开始配置部署了。

 

posted @ 2015-11-09 12:18  夏末、初秋  阅读(273)  评论(0编辑  收藏  举报