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
<?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框架整合完整版的更多相关文章
- ssh框架整合之登录以及增删改查
1.首先阐述一下我用得开发工具,myeclipse2017+oracle,所以我的基本配置步骤可能不一样,下面我用几张图来详解我的开发步骤. ---1先配置structs (Target 选择apac ...
- SSH框架整合
SSH框架整合 一.原理图 action:(struts2) 1.获取表单的数据 2.表单的验证,例如非空验证,email验证等 3.调用service,并把数据传递给service Service: ...
- dwr与ssh框架整合教程
(1)dwr与ssh框架整合教程dwr框架介绍. DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开 发人员开发包含AJ ...
- Spring+Hibernate+Struts(SSH)框架整合
SSH框架整合 前言:有人说,现在还是流行主流框架,SSM都出来很久了,更不要说SSH.我不以为然.现在许多公司所用的老项目还是ssh,如果改成流行框架,需要成本.比如金融IT这一块,数据库dao层还 ...
- J2EE进阶(十)SSH框架整合常见问题汇总(一)
SSH框架整合常见问题汇总(一) 前言 以下所列问题具有针对性,但是遇到同类型问题时均可按照此思路进行解决. HTTP Status 404 - No result defined for actio ...
- 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 ...
- SSH框架整合的其它方式
--------------------siwuxie095 SSH 框架整合的其它方式 1.主要是整合 Spring 框架和 Hibernate 框架时,可以不写 Hibernate 核心配置文件: ...
- SSH框架整合过程总结
---------------------siwuxie095 SSH 框架整合过程总结 (一)导入相关 jar 包(共 ...
- SSH框架整合思想
--------------------siwuxie095 SSH 框架整合思想 1.SSH 框架,即 Struts2 ...
随机推荐
- ace布置小作业: 制作一个简单的电话号码归属地查询软件:JSON解析和Volly发送get请求
大概就这个样子 用到JSON解析和Volly发送Get请求两个知识点 关于Volly的用法请看我的这篇: http://www.cnblogs.com/AceIsSunshineRain/p/5177 ...
- wpf键盘记录器
很简单的一个wpf键盘记录器 这个程序我一样用了全局勾子,之前用的都是winform上运行了,前一段时间 在国外的论坛上逛看到了一个wpf能用的就做了一个小程序记录一下,为了方便大家直关的看我在页面上 ...
- PM2实用入门指南
简介 PM2是node进程管理工具,可以利用它来简化很多node应用管理的繁琐任务,如性能监控.自动重启.负载均衡等,而且使用非常简单. 下面就对PM2进行入门性的介绍,基本涵盖了PM2的常用的功能和 ...
- [poj1742]coin
题意:多重背包的w=v特殊情况 分析:此题如果用单调队列O(nv)会被无耻卡常数…… 然后鉴于此题W=V,所以只存在背包恰好为i的是否存在,即bool型的f[i]是否为1 即往背包染色上面考虑 如果是 ...
- 百度地图 api 功能封装类 (ZMap.js) 本地搜索,范围查找实例 [源码下载]
相关说明 1. 界面查看: 吐槽贴:百度地图 api 封装 的实用功能 [源码下载] 2. 功能说明: 百度地图整合功能分享修正版[ZMap.js] 实例源码! ZMap.js 本类方法功能大多使用 ...
- jQuery基础之(一)jQuery概述
1.jQuery的简介 就像上节所将到的Ajax框架一样,简单的说,jQuery是一个优秀的javascript框架,它能够让用户方便的处理html,events(冒泡)事件,动画效果,ajax交互等 ...
- jQuery基础之(三)jQuery功能函数前缀及与window.onload冲突
1.jQuery功能函数前缀 在javascript中,开发者通常会编写一些小函数来处理各种操作细节,例如在用户提交表单时,要将文本框最前端和最末端的空格内容清理掉.而javascript中没有类似t ...
- angular的$scope,这东西满重要的
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Graphics samples
绘制二次曲线: public void paint(Graphics g) { // TODO 自动生成的方法存根 super.paint(g); Graphics2D g2=(Graphics2D) ...
- java设计优化--观察者模式
观察者模式介绍 观察者模式是一种非常有用的设计模式,在软件系统中,当一个对象的行为依赖于另一个对象的状态时,观察者模式就非常有用.如果不适用观察者模式,而实现类似的功能,可能就需要另外启动一个线程不停 ...