所使用的Jar包:

Hibernate:

Spring(使用MyEclipse自动导入框架功能)

Struts2:

注解包和MySql驱动包:

1、配置Hibernate和Spring:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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:annotation-config />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="" />
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialSize" value="1" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxActive" value="500" />
<!--最大空闲值,当经过一段峰值以后,连接会被释放掉一部分,直到释放到maxidle -->
<property name="maxIdle" value="2" />
<!--最小空闲值 当空闲的连接数小于阀值时,连接池会主去的去申请一些连接 -->
<property name="minIdle" value="1" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>cn/raffaello/hbm/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=false
</value>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="personIDao" class="cn.raffaello.dao.impl.PersonDaoImpl" />
<bean id="personIService" class="cn.raffaello.service.impl.PersonServiceImpl">
<property name="personIDao" ref="personIDao" />
</bean>
</beans>

2、新建实体和映射文件:

public class Person {
public Person(){}
public Person(String name) {
super();
this.name = name;
}
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<hibernate-mapping package="cn.raffaello.model">
<class name="Person" table="PERSON">
<id name="id" column="ID">
<generator class="native" />
</id>
<property name="name" column="NAME" />
</class>
</hibernate-mapping>

3、新建DaoBean,并在Bean中实现部分方法,使用@Resource给SessionFactory注入:

public interface PersonIDao {
void add(Person person);
void update(Person person);
Person getById(Integer id);
List<Person> getPersons();
}
public class PersonDaoImpl implements PersonIDao {

	@Resource(name="sessionFactory")
private SessionFactory sessionFactory;
@Override
public void add(Person person) {
sessionFactory.getCurrentSession().persist(person);
} @Override
public void update(Person person) {
sessionFactory.getCurrentSession().merge(person);
} @Override
public Person getById(Integer id) {
return (Person)sessionFactory.getCurrentSession().get(Person.class, id);
} @SuppressWarnings("unchecked")
@Override
public List<Person> getPersons() {
return sessionFactory.getCurrentSession().createQuery("from Person").list();
} }

5、新建ServiceBean,并实现部分方法;在ServiceBean的头部添加@Transactional 注解,使用该ServiceBean纳入Spring事务管理;

public interface PersonIService {

	void add(Person person);
void update(Person person);
Person getById(Integer id);
List<Person> getPersons();
}
@Transactional
public class PersonServiceImpl implements PersonIService {
private PersonIDao personIDao; @Override
public void add(Person person) {
personIDao.add(person);
} @Override
public void update(Person person) {
personIDao.update(person);
} @Transactional(readOnly=true)
@Override
public Person getById(Integer id) {
return personIDao.getById(id);
} @Transactional(readOnly=true)
@Override
public List<Person> getPersons() {
return personIDao.getPersons();
} public PersonIDao getPersonIDao() {
return personIDao;
} public void setPersonIDao(PersonIDao personIDao) {
this.personIDao = personIDao;
} }

6、测试Spring和Hibernate的代码:

public class test {

	private static PersonIService personIService;
@BeforeClass
public static void init(){
ApplicationContext cxt=new ClassPathXmlApplicationContext("beans.xml");
personIService=(PersonIService)cxt.getBean("personIService");
}
@Test
public void test() {
personIService.add(new Person("克露丝"));
personIService.add(new Person("Json"));
personIService.add(new Person("布尔曼"));
personIService.add(new Person("HHH"));
personIService.add(new Person("汤姆"));
} @Test
public void testUpdate(){
Person person=personIService.getById(1);
person.setName("汉克丝 hankesi");
personIService.update(person);
} @Test
public void testList(){
List<Person> list=personIService.getPersons();
for(Person per : list){
System.out.println(per.getName());
}
}
}

7、整合Struts2+Spring+Hibernate,将下面代码添加到Web.xml中:

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<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>

8、在Struts.xml中 进行如下配置:

	<constant name="struts.118n.encoding" value="UTF-8" />
<!-- Action 后缀 -->
<constant name="struts.action.extension" value="do,action" />
<!-- 设置浏览器是否缓存静态内容,默认值为true,开发环境下建议设置为false -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 当Struts 修改文件以后,是否重新加载该文件,默认为false,推荐开发环境下设置为true -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 开发模式下可以打印更详细的错误信息 -->
<constant name="struts.devMode" value="false" />
<!-- 默认视图主题 -->
<constant name="struts.ui.theme" value="simple" />
<!-- 与Spring集成是,指定有Spring创建Action对象 -->
<constant name="struts.objectFactory" value="spring" />
<package name="raffaello" namespace="/raffaello" extends="struts-default">
<action name="main_*" class="cn.raffaello.action.MainAction" method="{1}">
<result name="message">/WEB-INF/pages/message.jsp</result>
<result name="list">/WEB-INF/pages/list.jsp</result>
</action>
</package>

9、新建Action:MainAction:

public class MainAction {
private String message;
private Person person;
private PersonIService personIService;
public String hello(){
setMessage("struts2 测试消息!");
return "message";
} public String save(){
if(person!=null && !person.getName().equals("")){
personIService.add(person);
setMessage("添加成功");
}else{
setMessage("对象为空");
}
return "message";
}
public String list(){
List<Person> list=personIService.getPersons();
HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("PersonList", list);
return "list";
}
public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public Person getPerson() {
return person;
} public void setPerson(Person person) {
this.person = person;
} public PersonIService getPersonIService() {
return personIService;
} public void setPersonIService(PersonIService personIService) {
this.personIService = personIService;
} }

10、添加测试代码和获取的测试代码:

	<form action="/SSH2/raffaello/main_save.do" method="post">
添加人员    <a target="_blank" href="/SSH2/raffaello/main_list.do">人员列表</a>
<hr />
姓名:<input type="text" name="person.name" />
<input type="submit" value="提交">
</form>
<span style="white-space:pre">	</span><c:forEach items="${PersonList}" var="per">
${per.name}
</c:forEach>

Spring、Struts2+Spring+Hibernate整合步骤的更多相关文章

  1. Java Web开发之Spring | SpringMvc | Mybatis | Hibernate整合、配置、使用

    1.Spring与Mybatis整合 web.xml: <?xml version="1.0" encoding="UTF-8"?> <web ...

  2. spring+struts2+ibatis 框架整合以及解析

    一. spring+struts2+ibatis 框架 搭建教程 参考:http://biancheng.dnbcw.net/linux/394565.html 二.分层 1.dao: 数据访问层(增 ...

  3. struts2和hibernate整合的小Demo

    jar包下载地址 创建一个web项目. 导入jar包 配置web.xml <?xml version="1.0" encoding="UTF-8"?> ...

  4. Struts2与Hibernate整合

    时间:2017-1-26 02:00 1.创建一个Web项目2.导入jar包    3.引入配置文件    struts.xml    hibernate.cfg.xml    log4j.prope ...

  5. Spring第九篇【Spring与Hibernate整合】

    前言 前面已经学习了如何使用Spring与Struts2进行整合,本博文主要讲解如何使用Spring对Hibernate进行整合 Spring和Hibernate整合的关键点: SessionFact ...

  6. 最新版ssh hibernate spring struts2环境搭建

    最新版ssh hibernate spring struts2环境搭建 最新版spring Framework下载地址:spring4.0.0RELEASE环境搭建 http://repo.sprin ...

  7. Struts2,Spring, Hibernate三大框架SSH的整合步骤

    整合步骤 创建web工程 引入相应的jar包 整合spring和hibernate框架 编写实体类pojo和hbm.xml文件 编写bean-base.xml文件 <!-- 1) 连接池实例 - ...

  8. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:SSH框架(Struts2+Spring+Hibernate)搭建整合详细步骤

    在实际项目的开发中,为了充分利用各个框架的优点,通常都会把 Spring 与其他框架整合在一起使用. 整合就是将不同的框架放在一个项目中,共同使用它们的技术,发挥它们的优点,并形成互补.一般而言,在进 ...

  9. SSH框架之Spring+Struts2+Hibernate整合篇

    回顾 -Hibernate框架 ORM: 对象关系映射.把数据库表和JavaBean通过映射的配置文件映射起来, 操作JavaBean对象,通过映射的配置文件生成SQL语句,自动执行.操作数据库. 1 ...

随机推荐

  1. JSON基础学习

    定义 JSON时轻量级的文本数据交换格式,独立于语言,比xml更小更快更易解析 JSON解析器和JSON库支持不同的编程语言 4个基本规则 1. 并列数据间用 逗号, 2. 映射用冒号表示 3. 并列 ...

  2. SSAS父子层次结构的增强-UnaryOperatorColumn属性

    上次我有讲到自定义汇总,这次的内容跟上次的差不多也算是自定义汇总,实现的方式不同而已!使用的是UnaryOperatorColumn属性. 这个属性说明: 一元运算符用于将成员自定义汇总到父级,汇总运 ...

  3. (一)boost库之日期、时间

    (一)boost库之日期.时间 一.计时器  计时器,通常在一个项目中统计一个函数的执行时间是非常实用的.   #include <boost/timer.hpp> void PrintU ...

  4. bzoj3293 [Cqoi2011]分金币&&bzoj1045 [HAOI2008]糖果传递

    Description 圆桌上坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值. Inpu ...

  5. Linux内核中常见内存分配函数(二)

    常用内存分配函数 __get_free_pages unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order) __get_f ...

  6. CoreData多表操作.

    这次给大家带来的是CoreData多表操作的使用. 首先我们要对CoreData来进行多表操作我们先要创建至少两个实体在工程中. 在创建完成这两个对应的工程实体文件和工程中的类文件后我们现在需要创建一 ...

  7. ios ViewController present不同的方向

    First ViewController CATransition *transition = [CATransition animation]; transition.duration = 0.3; ...

  8. js添加、删除Cookie

    //cookie function addCookie(objName, objValue, objHours) { //添加cookie var str = objName + "=&qu ...

  9. Stopwatch 和TimeSpan介绍【转】

    1.使用 Stopwatch 类 (System.Diagnostics.Stopwatch) Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.在典型的 S ...

  10. DB2数据库常用基本操作命令

    点击开始菜单-->所有程序-->IBM-->DB2-->DB2COPY1-->命令行工具-->命令窗口一.DB2实例操作1.查看DB2数据库的版本及安装目录 E:\ ...