Spring、Struts2+Spring+Hibernate整合步骤
所使用的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整合步骤的更多相关文章
- Java Web开发之Spring | SpringMvc | Mybatis | Hibernate整合、配置、使用
1.Spring与Mybatis整合 web.xml: <?xml version="1.0" encoding="UTF-8"?> <web ...
- spring+struts2+ibatis 框架整合以及解析
一. spring+struts2+ibatis 框架 搭建教程 参考:http://biancheng.dnbcw.net/linux/394565.html 二.分层 1.dao: 数据访问层(增 ...
- struts2和hibernate整合的小Demo
jar包下载地址 创建一个web项目. 导入jar包 配置web.xml <?xml version="1.0" encoding="UTF-8"?> ...
- Struts2与Hibernate整合
时间:2017-1-26 02:00 1.创建一个Web项目2.导入jar包 3.引入配置文件 struts.xml hibernate.cfg.xml log4j.prope ...
- Spring第九篇【Spring与Hibernate整合】
前言 前面已经学习了如何使用Spring与Struts2进行整合,本博文主要讲解如何使用Spring对Hibernate进行整合 Spring和Hibernate整合的关键点: SessionFact ...
- 最新版ssh hibernate spring struts2环境搭建
最新版ssh hibernate spring struts2环境搭建 最新版spring Framework下载地址:spring4.0.0RELEASE环境搭建 http://repo.sprin ...
- Struts2,Spring, Hibernate三大框架SSH的整合步骤
整合步骤 创建web工程 引入相应的jar包 整合spring和hibernate框架 编写实体类pojo和hbm.xml文件 编写bean-base.xml文件 <!-- 1) 连接池实例 - ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:SSH框架(Struts2+Spring+Hibernate)搭建整合详细步骤
在实际项目的开发中,为了充分利用各个框架的优点,通常都会把 Spring 与其他框架整合在一起使用. 整合就是将不同的框架放在一个项目中,共同使用它们的技术,发挥它们的优点,并形成互补.一般而言,在进 ...
- SSH框架之Spring+Struts2+Hibernate整合篇
回顾 -Hibernate框架 ORM: 对象关系映射.把数据库表和JavaBean通过映射的配置文件映射起来, 操作JavaBean对象,通过映射的配置文件生成SQL语句,自动执行.操作数据库. 1 ...
随机推荐
- python操作redis--string
#!/usr/bin/python #!coding:utf-8 """ 完成用redis模块操作string类型的数据 """ impor ...
- JS如何设置计算几天前的时间?
计算多少天前的具体时间.比如今天是9月5日,那7天前正常就是8月29了. 之前曾经直接用时间进行加减,吃了大亏,后来脑残到直接写了一个很复杂的计算闰年,闰月,30.31.28的月份 现在分享一下. f ...
- Delphi调用安装驱动sys的单元
unit SysDriver; interface uses windows, winsvc; // jwawinsvc; Type TSysDriver = class(TObject) priva ...
- (八)boost库之异常处理
(八)boost库之异常处理 当你面对上千万行的项目时,当看到系统输出了异常信息时,你是否想过,如果它能将文件名.行号等信息输出,该多好啊,曾经为此绞尽脑汁. 今天使用boost库,将轻松的解决这个问 ...
- hibernate 非xml实体类配置方法!
hibernate 非xml实体类配置方法! 这个是hibernate.cfg.xml配置文件 <?xml version='1.0' encoding='UTF-8'?> <!DO ...
- Fast RCNN 学习
因为项目需要,之前没有接触过深度学习的东西,现在需要学习Fast RCNN这个方法. 一步步来,先跟着做,然后再学习理论 Fast RCNN 训练自己数据集 (1编译配置) Fast RCNN 训练自 ...
- Dima and Salad(完全背包)
Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 菜鸟必须知道的linux的文件目录结构
Linux文件目录结 / 根目录,所有的目录.文件.设备都在/之下,/就是Linux文件系统的组织者,也是最上级的领导者. /bin bin就是二进制(binary)英文缩写.在一般的系统当中,你都可 ...
- js 推断 当页面无法回退时(history.go(-1)),关闭网页
在做一个Web项目时遇到一个需求,当页面没有前驱历史记录时(就是当前为新弹出的页面,没法做goback操作即history.go(-1)),点击返回button时直接关闭页面,否则就退回到前一页. 遇 ...
- C++ STL源代码学习(map,set内部heap篇)
stl_heap.h ///STL中使用的是大顶堆 /// Heap-manipulation functions: push_heap, pop_heap, make_heap, sort_heap ...