ssh-ssh整合(Struts2+Sprinig+hibernate)
在之前呢我们已经讲解过ssm以及ssm2的整合开发,今天我们进行ssh的整合,在之前已经有一篇整合ssh的文章,那是基于注解开发的,今天讲解的为基于配置文件注入方式进行开发。
思路:spring管理hibernate相关会话工厂的创建以及负责管理hibernate的事务,同时spring容器管理service层的实现以及struts2的action,话不多说,我们进入正题。
同样的,我们以一个用户登录的案例进行讲解。
开发软件:
Eclipse neon
Tomcat7.0
Jdk1.7
Struts2.3
Spring2.5
Hiberbate3.0
项目结构如下所示:
项目源码下载:点击下载
在线演示:点击观看
ssh的开发我们同样的分为标准的三层进行开发,首先我们进行Model层的开发。
Model
1、首先我们编写po对象-User.Java
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
package com.sw.domain;
/*
*@Author swxctx
*@time 2017年4月27日
*@Explain:用户表po对象
*id:编号
*username:用户名
*password:密码
*/
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2、编写对应的hibernate映射文件-User.hbm.xml
[html] view plain copy print?在CODE上查看代码片派生到我的代码片
<?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>
<class name="com.sw.domain.User" table="user">
<!-- 配置主键 -->
<id name="id" type="int">
<!-- 列定义 -->
<column name="id" not-null="true" sql-type="int"></column>
<!-- 生成策略 -->
<generator class="uuid"></generator>
</id>
<!-- 字段配置 -->
<property name="username" type="string">
<column name="username" sql-type="varchar(50)"></column>
</property>
<property name="password" type="string">
<column name="password" sql-type="varchar(50)"></column>
</property>
</class>
</hibernate-mapping>
3、接下来我们需要编写Hibernate配置文件-hibernate.cfg.xml
[html] view plain copy print?在CODE上查看代码片派生到我的代码片
<?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="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">0707</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bs</property>
<!-- 事务自动提交,使用sessionFactory需要进行此项配置 -->
<property name="hibernate.connection.autocommit">true</property>
<!-- 配置方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 操作数据库的形式 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 映射文件 -->
<mapping resource="com/sw/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3、现在基本配置已经完成了,我们可以进行dao层的开发了,编写UserDao.java文件
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
package com.sw.dao;
import com.sw.domain.User;
/*
*@Author swxctx
*@time 2017年5月10日
*@Explain:Dao层接口
*/
public interface UserDao {
public final static String SERVICE_NAME = "UserDaoImpl";
//登录
public User findUserByUsername(String username);
}
4、接下来需要编写实现类-UserDaoImpl.java
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
package com.sw.dao.impl;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.sw.dao.UserDao;
import com.sw.domain.User;
/*
*@Author swxctx
*@time 2017年5月10日
*@Explain:Dao层实现
*/
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Override
public User findUserByUsername(String username) {
Session session=null;
User user=new User();
try {
session=this.getHibernateTemplate(http://www.yiqianou.cn/).getSessionFactory().openSession();
Criteria criteria=(Criteria) session.createCriteria(User.class);
//加入条件查询
criteria.add(Restrictions.eq("username",www.22yigouyule.cn/ username));
user=(User) criteria.uniqueResult();
} finally {
if(session!=null){
session.close();
}
}
return user;
}
}
5、接下来我们需要通过Spring来管理SsqlsessionFactoty,同时需要通过spring管理dao层的方法,编写applicatiobContext.xml文件
[html] view plain copy print?在CODE上查看代码片派生到我的代码片
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 配置注解自动扫描范围 -->
<context:component-scan base-package="com.sw"></context:component-scan>
<!-- 创建SessionFactory工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>
classpath:/hibernate/hibernate.cfg.xml
</value>
</property>
</bean>
<!-- 配置dao层(注入sessionFactory) -->
<bean id="UserDaoImpl" class="com.sw.dao.impl.UserDaoImpl">
<!-- 注入sessionFactory --www.xyseo.net>
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
6、接下来我们需要编写spring配置文件加载的工具类,在之前的ssm以及ssm2中已经讲到,这里不再重复。
加载配置文件类:
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
package com.sw.container;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
*@Author swxctx
*@time 2017年5月15日
*@Explain:服务类,用用户加载applicationContext.xml文件
*/
public class ServiceProvideCord {
protected static ApplicationContext applicationContext;
public static void load(String[www.feifanyule.cn] fileName){
applicationContext = new ClassPathXmlApplicationContext(fileName);
}
}
获取bean服务的类:
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
package com.sw.container;
import org.apache.commons.lang.StringUtils;
/*
*@Author swxctx
*@time 2017年5月15日
*@Explain:Service层服务类
*/
@SuppressWarnings("static-access")
public class SwServiceProvider {
private static ServiceProvideCord serviceProvideCord;
//静态加载
static{
serviceProvideCord = new ServiceProvideCord();
serviceProvideCord.load(new String[]{"classpath:/spring/applicationContext-service.xml",
"classpath:/spring/applicationContext-dao.xml",
"classpath:/spring/applicationContext-transaction.xml",
"classpath:/spring/applicationContext-action.xml"});
}
public static Object getService(String serviceName){
//服务名称为空
if(StringUtils.isBlank(serviceName)){
throw new RuntimeException("当前服务名称不存在...");
}
Object object = null;
if(serviceProvideCord.applicationContext.containsBean(serviceName)){
//获取bean
object = serviceProvideCord.applicationContext.getBean(serviceName);
}
if(object==null){
throw new RuntimeException("服务名称为【"+serviceName+"】下的服务节点不存在...");
}
return object;
}
}
7、如上,我们已经完成了编写,可以进行测试了。
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
package com.sw.test;
import org.junit.Test;
import com.sw.container.SwServiceProvider;
import com.sw.dao.UserDao;
import com.sw.domain.User;
/*
*@Author swxctx
*@time 2017年5月18日
*@Explain:hibernate(027yeshenghuowang.com dao)测试
*/
public class DaoTest {
@Test
public void testFindUserByName(www.yigouyule2019.cn) {
UserDao userDao= (UserDao) SwServiceProvider.getService(UserDao.SERVICE_NAME);
User user=(User) userDao.findUserByUsername("bs");
if(user!=null){
String string=user.getPassword();
System.out.println(string);
}else{
System.out.println("www.yuheng119.com/");
}
}
}
测试结果如下:
如上所示,表明model层的开发已经无误;记下来进行service层的开发。
ssh-ssh整合(Struts2+Sprinig+hibernate)的更多相关文章
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:SSH框架(Struts2+Spring+Hibernate)搭建整合详细步骤
在实际项目的开发中,为了充分利用各个框架的优点,通常都会把 Spring 与其他框架整合在一起使用. 整合就是将不同的框架放在一个项目中,共同使用它们的技术,发挥它们的优点,并形成互补.一般而言,在进 ...
- 整合struts2+spring+hibernate
一.准备struts2+spring+hibernate所须要的jar包: 新建web项目并将jar包引入到project项目中. 二.搭建struts2环境 a.在 ...
- spring整合struts2和hibernate
1.spring 1.1 jar包 1.2 spring.xml <?xml version="1.0" encoding="UTF-8"?> &l ...
- SSH之IDEA2017整合Struts2+Spring+Hibernate
转自:https://blog.csdn.net/sysushui/article/details/68937005
- 搭建SSH框架整合Struts2和Spring时,使用@Autowired注解无法自动注入
© 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: 搭建SSH框架,在进行Struts2和Spring整合时,使用Spring的@Autowired自动注入失败,运行报错java.lan ...
- 整合Struts2、Hibernate、Spring
将项目中的对象和对象之间的管理,纳入spring容器,由spring管理 1 实现spring+hibernate集成 1.1 新建web项目 建立项目的包结构(package) 1.2加入jar包 ...
- SSH的整合
SSH的整合 struts2和hibernate的配置我这里就不多说了,先把两个有关的东西说下.一个是所有的包.struts2+hibernate3+spring2.5我包准备放上去给大家下载. ht ...
- struts2+spring+hibernate(SSH)框架的搭建和总结
SSH框架:struts2+spring+hibernate,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. struts2+spring+hibernat ...
- Spring、Struts2+Spring+Hibernate整合步骤
所使用的Jar包: Hibernate: Spring(使用MyEclipse自动导入框架功能) Struts2: 注解包和MySql驱动包: 1.配置Hibernate和Spring: <be ...
随机推荐
- hbase_学习_01_HBase环境搭建(单机)
一.前言 本文承接上一篇:hadoop_学习_02_Hadoop环境搭建(单机) ,主要是搭建HBase的单机环境 二.环境准备 1.说明 hbase 的下载来源有: 官方版本:http://arc ...
- python模块inspect.py
inspect模块用来检查对象的类型(函数,属性,类,抽象基类,方法,模块等等) 是一个封装好的非常有用的模块. ]) ]: cls = :]: content = ] = lines[].lstri ...
- Havel-Hakimi定理(握手定理)
Havel-Hakimi定理(握手定理) 由非负整数组成的非增序列s(度序列):d1,d2,…,dn(n>=2,d1>=1)是可图的,当且仅当序列: s1:d2 – 1,d3 – 1,…, ...
- stl_hashtable.h
stl_hashtable.h // Filename: stl_hashtable.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog ...
- 如何查看myeclipse是否激活
myEclipse---->Subscription information--->Subscription expiration date 看这个日期到什么时候!另外建议别用太高版本的M ...
- rtmp与hls流媒体服务器搭建:ubuntu下Nginx搭建初探与rtmp-module的添加
关键词:Nignx(http服务器):rtmp,hls(流媒体服务) 前言:感谢开源,感谢战斗民族.现在在做流媒体服务的一些工作,流媒体服务器搭建的网上教程多入牛毛,但是细细查看,发现很多同志贴上来的 ...
- 使用WindowsAPI实现播放PCM音频的方法
这篇文章主要介绍了使用WindowsAPI实现播放PCM音频的方法,很实用的一个功能,需要的朋友可以参考下 本文介绍了使用WindowsAPI实现播放PCM音频的方法,同前面一篇使用WindowsAP ...
- [BZOJ2962][清华集训]序列操作
bzoj luogu 题意 有一个长度为\(n\) 的序列,有三个操作: \(I \ \ a\ b\ c\ :\)表示将\([a,b]\)这一段区间的元素集体增加\(c\): \(R \ \ a\ b ...
- 浏览器全屏的JS代码实现
方法一:该方法是从一个网上的效果看到不错,然后自己就拿来下来实验了一下,还是比较满意度,下面直接给出代码 <!DOCTYPE HTML> <html lang="en-US ...
- 计算MySQL的内存峰值公式 (转)
-- 计算MySQL的内存峰值公式,计算所有的连接满了的情况下: select (@@key_buffer_size + @@query_cache_size + @@tmp_table_size ...