1、所需jar包
2、配置Spring
(1)导入需要的jar包
(2)配置文件bean.xml 下面给出的xml文件是整合完毕之后的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 配置DataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root" />
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />
<property name="password" value="1234" />
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<!-- 配置TransactionManager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置Advice -->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop -->
<aop:config>
<aop:pointcut expression="execution(* com.cqb.service.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config>
<!-- 配置Service -->
<bean id="categoryService" class="com.cqb.service.CategoryServiceImp">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置Action -->
<bean id="categoryAction" class="com.cqb.action.CategoryAction" scope="prototype">
<property name="categoryService" ref="categoryService" />
</bean>
</beans>
(3)测试Spring是否配置完成
3、配置整合hibernate
(1)导入jar包
(2)创建配置文件hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping class="com.cqb.bean.Category"/>
</session-factory>
</hibernate-configuration>
注释掉的部分在bean.xml中配置
(3)在bean.xml中配置dataSource
(4)在bean.xml中配置sessionFactory
(5)使用Hibernate Tools逆向生成永久类
package com.cqb.bean;
// Generated 2017-4-12 19:15:33 by Hibernate Tools 4.3.5.Final
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Category generated by hbm2java
*/
@Entity
@Table(name = "category", catalog = "test")
public class Category implements java.io.Serializable {
private Integer id;
private String type;
private Boolean hot;
public Category() {};
public Category(int i, String string, boolean b) {
}
public Category(String type, Boolean hot) {
this.type = type;
this.hot = hot;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "type", length = 20)
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
@Column(name = "hot")
public Boolean getHot() {
return this.hot;
}
public void setHot(Boolean hot) {
this.hot = hot;
}
}
(6)创建Service用于测试
package com.cqb.service;
import com.cqb.bean.Category;
public interface CategoryService {
public void save(Category category) ;
public void update(Category category);
public Category findById(int id);
}
package com.cqb.service;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.cqb.bean.Category;
import sun.print.resources.serviceui;
public class CategoryServiceImp implements CategoryService {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Override
public void save(Category category) {
sessionFactory.getCurrentSession().save(category);
}
@Override
public void update(Category category) {
sessionFactory.getCurrentSession().update(category);
}
@Override
public Category findById(int id) {
// TODO Auto-generated method stub
return (Category) sessionFactory.getCurrentSession().get(Category.class, id);
}
}
(7)在bean.xml中配置service
(8)在bean.xml中配置transactionManager
(9)在bean.xml中配置advice和aop
(10)测试
4、配置和整合struts2
(1)导入所需jar包
(2)创建web.xml和struts.xml
(3)在web.xml中写入Spring的Listener
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>E_shop</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>*.action</url-pattern>
</filter-mapping>
<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>
</web-app>
(4)创建action 并在bean.xml和struts.xml中配置
package com.cqb.action;
import com.cqb.bean.Category;
import com.cqb.service.CategoryService;
import com.opensymphony.xwork2.ActionSupport;
public class CategoryAction extends ActionSupport {
private CategoryService categoryService;
private Category category;
public void setCategory(Category category) {
this.category = category;
}
public Category getCategory() {
return category;
}
public void setCategoryService(CategoryService categoryService) {
this.categoryService = categoryService;
}
public String save() {
categoryService.save(category);
return "index";
}
public String update() {
categoryService.update(category);
return "index";
}
}
<?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="shop" extends="struts-default" namespace="/">
<!-- category_update.actiocan: 访问update方法 -->
<action name="category_*" class="categoryAction" method="{1}">
<result name="index">/index.jsp</result>
</action>
</package>
</struts>
(5)编写jsp测试
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="category_save.action?category.type=gga&category.hot=false">访问save</a>
<a href="category_update.action?category.id=2&category.type=g&category.hot=true">访问update</a>
</body>
</html>
以上 就完成了对ssh的整合
hibernate和spring的测试使用Junit4
import java.util.Date;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.cqb.bean.Category;
import com.cqb.service.CategoryService;
import com.cqb.service.CategoryServiceImp;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:beans.xml")
public class SSHTest {
@Resource
private CategoryService categoryService;
@Resource
private Date date;
public void setCategoryService(CategoryService categoryService) {
this.categoryService = categoryService;
}
public CategoryService getCategoryService() {
return categoryService;
}
/*
@Test //测试Spring IOC的开发环境
public void springIoc() {
System.out.println(date);
}
*/
@Test //测试Hibernate的开发环境,因为没有整合,可以直接new
public void hihernate() {
Category category = categoryService.findById(1);
category.setType("chenqingb");
categoryService.update(category);
System.out.println("hahahahaha");
}
}