SSH框架
2016-06-10 20:11
337 查看
综述
SSH(struts + spring + hibernate)是一个集成框架,是目前较流行的一种Web应用程序开源框架。集成SSH框架的系统从职责上分为四层:表示层、业务逻辑层、数据持久层和域模块层,以帮助开发人员在短期内搭建结构清晰、可复用性好、维护方便的Web应用程序。
其中使用Struts作为系统的整体基础架构,负责MVC的分离,在Struts框架的模型部分,控制业务跳转,利用Hibernate框架对持久层提供支持,Spring做管理,管理struts和hibernate。具体做法是:用面向对象的分析方法根据需求提出一些模型,将这些模型实现为基本的Java对象,然后编写基本的DAO(Data Access Objects,数据库访问对象)接口,并给出Hibernate的DAO实现,采用Hibernate架构实现的DAO类来实现Java类与数据库之间的转换和访问,最后由Spring做管理,管理struts和hibernate。
图解
今天做了一个(SSH)的小作业,下面的图是自己的一些理解注意
过程中遇到了一些问题,下面这些值得了解一下openSession和getCurrentSession的区别
Configuration cfg = new Configuration(); // 获得配置信息对象 SessionFactory sf = cfg.configure().buildSessionFactory(); //解析并建立Session工厂 1. Session session = sf.getCurrentSession(); // 获得Session 2. Session session = sf.openSession(); // 打开Session
对于上述的两个方法,有以下区别:
openSession 从字面上可以看得出来,是打开一个新的session对象,而且每次使用都是打开一个新的session,假如连续使用多次,则获得的session不是同一个对象,并且使用完需要调用close方法关闭session。
getCurrentSession ,从字面上可以看得出来,是获取当前上下文一个session对象,当第一次使用此方法时,会自动产生一个session对象,并且连续使用多次时,得到的session都是同一个对象,这就是与openSession的区别之一,简单而言,getCurrentSession
就是:如果有已经使用的,用旧的,如果没有,建新的。
注意:在实际开发中,往往使用getCurrentSession多,因为一般是处理同一个事务(即是使用一个数据库的情况),所以在一般情况下比较少使用openSession或者说openSession是比较老旧的一套接口了;
对于getCurrentSession 来说,有以下一些特点:
1.用途,界定事务边界
2.事务提交会自动close,不需要像openSession一样自己调用close方法关闭session
3.上下文配置(即在hibernate.cfg.xml)中,需要配置:
<property name="current_session_context_class">thread</property>
(需要注意,这里的current_session_context_class属性有几个属性值:jta 、 thread 常用 , custom、managed 少用 )
a).thread使用connection 单数据库连接管理事务
b).jta (java transaction api) Java 分布式事务管理 (多数据库访问),jta 由中间件提供(JBoss WebLogic 等, 但是tomcat 不支持)
源码
下面把实例的源码贴出来UserInfo.java
package domain; public class UserInfo { private int _id = 0;//用户ID private String name = null;//用户姓名 public int get_id() { return _id; } public void set_id(int _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
UserInfo.hbm.xml
<hibernate-mapping> <class name="domain.UserInfo" table="USERINFO"> <id name="_id" type="int"> <column name="_ID" /> <generator class="assigned" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> </class> </hibernate-mapping>
inputUserInfo.jsp
<body> <s:form action="inputuserinfo" name="inputForm"> ID :<input type="text" name="userInfo._id"/><br> NAME:<input type="text" name="userInfo.name" /><br> <input type="submit" value="注册"/> </s:form> </body>
struts.xml
<struts> <package name="default" extends="struts-default" namespace="/"> <action name="inputuserinfo" class="action.UserInfoAction" method="execute"> <result name="success">/success.jsp</result> <result name="input">/inputUserInfo.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
UserInfoAction.class
package action; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import d4b0 service.UserInfoService; import domain.UserInfo; public class UserInfoAction { private UserInfo userInfo; public UserInfo getUserInfo() { return userInfo; } public void setUserInfo(UserInfo userInfo) { this.userInfo = userInfo; } private void save (UserInfo user) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); UserInfoService service = (UserInfoService) context.getBean("userinfoservice"); service.saveUserInfo(user); } public String execute() { String ret = "error"; if (userInfo.get_id() != 0 && !userInfo.getName().equals("")) { ret = "success"; // SaveUserInfo.saveUserInfo(getUserInfo());//没有使用spring 框架, 使用的是只是hibernate save(getUserInfo()); } else if (userInfo.get_id() == 0 || userInfo.getName().equals("")) { ret = "input"; } else { ret = "error"; } return ret; } }
beans.xml
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="userinfoservice" class="service.UserInfoServiceImpl"></bean> <bean id="userinfodao" class="dao.UserInfoDaoImpl"></bean> </beans>
UserInfoService.java
package service; import domain.UserInfo; public interface UserInfoService { public boolean saveUserInfo(UserInfo user); }
UserInfoServiceImpl.java
package service; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import dao.UserInfoDao; import domain.UserInfo; public class UserInfoServiceImpl implements UserInfoService { @Override public boolean saveUserInfo(UserInfo user) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "beans.xml"); UserInfoDao dao = (UserInfoDao) applicationContext.getBean("userinfodao"); return dao.saveUserInfoInDB(user); } }
UserInfoDao.java
package dao; import domain.UserInfo; public interface UserInfoDao { public boolean saveUserInfoInDB(UserInfo user); }
UserInfoDaoImpl.java
package dao; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import domain.UserInfo; public class UserInfoDaoImpl implements UserInfoDao { @Override public boolean saveUserInfoInDB(UserInfo user) { try { Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() .applySettings(configuration.getProperties()) .buildServiceRegistry(); SessionFactory factory = configuration.buildSessionFactory(serviceRegistry); Session session = factory.getCurrentSession(); Transaction transaction = session.beginTransaction(); session.persist(user); transaction.commit(); return true; } catch (HibernateException e) { e.printStackTrace(); return false; } } }
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.username">root</property> <property name="connection.password"></property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=utf-8</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!--设置Hibernate自动管理上下文的策略--> <property name="current_session_context_class">thread</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="domain/UserInfo.hbm.xml"/> </session-factory> </hibernate-configuration>
总结
要深入理解SSH框架各自的作用,以及理解三者之间的关联关系相关文章推荐
- Linux Generating SSH Keys
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- JQuery+Strusts1.x无刷新登录
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 插件管理框架 for Delphi(一)
- 使用CSS框架布局的缺点和优点小结
- 一起动手编写Android图片加载框架
- 基于.NET平台常用的框架和开源程序整理
- 列举PHP的Yii 2框架的开发优势
- perl脚本实现限制ssh最大登录次数(支持白名单)
- Windows窗体的.Net框架绘图技术实现方法
- 浅谈JavaScript 框架分类