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>

就可以开始配置部署了。

ssh框架整合完整版的更多相关文章

  1. ssh框架整合之登录以及增删改查

    1.首先阐述一下我用得开发工具,myeclipse2017+oracle,所以我的基本配置步骤可能不一样,下面我用几张图来详解我的开发步骤. ---1先配置structs (Target 选择apac ...

  2. SSH框架整合

    SSH框架整合 一.原理图 action:(struts2) 1.获取表单的数据 2.表单的验证,例如非空验证,email验证等 3.调用service,并把数据传递给service Service: ...

  3. dwr与ssh框架整合教程

    (1)dwr与ssh框架整合教程dwr框架介绍. DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开 发人员开发包含AJ ...

  4. Spring+Hibernate+Struts(SSH)框架整合

    SSH框架整合 前言:有人说,现在还是流行主流框架,SSM都出来很久了,更不要说SSH.我不以为然.现在许多公司所用的老项目还是ssh,如果改成流行框架,需要成本.比如金融IT这一块,数据库dao层还 ...

  5. J2EE进阶(十)SSH框架整合常见问题汇总(一)

    SSH框架整合常见问题汇总(一) 前言 以下所列问题具有针对性,但是遇到同类型问题时均可按照此思路进行解决. HTTP Status 404 - No result defined for actio ...

  6. MVC+Spring.NET+NHibernate .NET SSH框架整合 C# 委托异步 和 async /await 两种实现的异步 如何消除点击按钮时周围出现的白线? Linq中 AsQueryable(), AsEnumerable()和ToList()的区别和用法

    MVC+Spring.NET+NHibernate .NET SSH框架整合   在JAVA中,SSH框架可谓是无人不晓,就和.NET中的MVC框架一样普及.作为一个初学者,可以感受到.NET出了MV ...

  7. SSH框架整合的其它方式

    --------------------siwuxie095 SSH 框架整合的其它方式 1.主要是整合 Spring 框架和 Hibernate 框架时,可以不写 Hibernate 核心配置文件: ...

  8. SSH框架整合过程总结

    ---------------------siwuxie095                                 SSH 框架整合过程总结         (一)导入相关 jar 包(共 ...

  9. SSH框架整合思想

    --------------------siwuxie095                                 SSH 框架整合思想         1.SSH 框架,即 Struts2 ...

随机推荐

  1. 神奇的main方法详解

    main函数的详解:    public : 公共的. 权限是最大,在任何情况下都可以访问.        原因: 为了保证让jvm在任何情况下都可以访问到main方法.    static:  静态 ...

  2. 我们为什么需要DTO?

    看了几套源码,其中都有用到DTO,这篇文章主要来谈论一下DTO使用的场合及其带来的好处. 在传统的编程中,我们一般都是前台请求数据,发送到Webservice,然后WebService向数据库发出请求 ...

  3. OpenShift

    一步一脚印 停停走走,回头看看 博客园 首页 新随笔 联系 订阅 管理 随笔 - 24  文章 - 8  评论 - 2 调戏OpenShift:一个免费能干的云平台   一.前因后果 以前为了搞微信的 ...

  4. 执行mount命令时找不到介质或者mount:no medium found的解决办法

    使用vmware时,在虚拟机设置里,设置CD/DVD为系统镜像,挂载时,有时会有找不到介质或者no medium found之类的提示. 根本原因是iso镜像并没有加载到虚拟机系统内. 解决办法: 首 ...

  5. [CareerCup] 5.4 Explain Expression ((n & (n-1)) == 0) 解释表达式

    5.4 Explain what the following code does: ((n & (n-1)) == 0). 这道题让我们解释一个表达式((n & (n-1)) == 0 ...

  6. [CareerCup] 12.5 Test a Pen 测试一支笔

    12.5 How would you testa pen? 这道题让我们测试一支笔,我们需要问面试官许多问题来理解"who,what,where,when,how and why" ...

  7. 数据挖掘系列(5)使用mahout做海量数据关联规则挖掘

    上一篇介绍了用开源数据挖掘软件weka做关联规则挖掘,weka方便实用,但不能处理大数据集,因为内存放不下,给它再多的时间也是无用,因此需要进行分布式计算,mahout是一个基于hadoop的分布式数 ...

  8. C#基础之内存分配

    1.创建一个对象 一个对象的创建过程主要分为内存分配和初始化两个环节.在.NET中CLR管理的内存区域主要有三部分:栈.GC堆.LOH堆,栈主要用来分配值类型数据.它的管理是有系统控制的,而不是像GC ...

  9. [软件测试]Linux环境中简单清爽的Google Test (GTest)测试环境搭建(初级使用)

    本文将介绍单元测试工具google test(GTEST)在linux操作系统中测试环境的搭建方法.本文属于google test使用的基础教程.在linux中使用google test之前,需要对如 ...

  10. JavaScript实例-----反选

    <!DOCTYPE HTML> <html> <head> <script> function myFunction() { var oTab = do ...