SSH整合代码
SSH整合
一、 jar包整合
struts:2.3.15.3
hibernate: 3.6.10
spring:3.2.0
一共40个jar包 需要删除一个:一共39个
二、spring整合hibernate:有hibernate.cfg.xm
1.创建表
CREATE DATABASE ssh;
USE ssh;
CREATE TABLE t_user(
idINT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50),
PASSWORD VARCHAR(32),
age INT
);
-----------------------------------------------------------------------------------
2.创建PO类(javaBean+映射文件)
package com.cjw.domain;
/**
* 实体类 与数据库表结构对应
* @authorDreamWF
*
*/
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
public Integer getId() {
return id;
}
-----------------------------------------------------------------------------------
User.hbm.xml
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.cjw.domain.User" table="t_user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="username"></property>
<property name="password"></property>
<property name="age"></property>
</class>
</hibernate-mapping>
-----------------------------------------------------------------------------------
3.Dao层
package com.cjw.dao.impl;
importorg.springframework.orm.hibernate3.HibernateTemplate;
import com.cjw.dao.UserDao;
import com.cjw.domain.User;
public class UserDaoImpl implements UserDao {
//需要Spring注入模板,底层使用session,session由sessionFactory获得
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public void save(User user) {
this.hibernateTemplate.save(user);
}
}
-----------------------------------------------------------------------------------
4.Service层
package com.cjw.service.impl;
import com.cjw.dao.UserDao;
import com.cjw.domain.User;
import com.cjw.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void register(User user) {
userDao.save(user);
}
}
-----------------------------------------------------------------------------------
5.hibernate.cfg.xml
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<!-- 基本四项 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///ssh</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 配置数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 打印sql语句 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 自动生成表结构(一般没有用) -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 本地线程绑定 -->
<property name="current_session_context_class">thread</property>
<!-- 引入映射文件 -->
<mapping resource="com/cjw/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
-----------------------------------------------------------------------------------
6.allicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="constructor">
<!-- 1加载hibernate.cfg.xml配置文件 获得SessionFactory
configLocation:确定配置文件位置
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 2 创建底层模板
底层使用session,session由sessionFactory获得
-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 3 dao -->
<bean id="userDao" class="com.cjw.dao.impl.UserDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- 4 service -->
<bean id="userService" class="com.cjw.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 5 事务管理 -->
<!-- 5.1 事务管理器:HibernateTransactionManager -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 5.2 事务详情,给ABCD进行具体事务设置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="register"/>
</tx:attributes>
</tx:advice>
<!-- 5.3 AOP编程,ABCD筛选ABC -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(*com.cjw.service..*.*(..))"/>
</aop:config>
</beans>
-----------------------------------------------------------------------------------
7.测试类
package com.cjw.test;
import org.junit.Test;
import org.junit.runner.RunWith;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.test.context.ContextConfiguration;
importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.cjw.domain.User;
import com.cjw.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class TestApp {
@Autowired
private UserService userService;
@Test
public void demo01(){
Useruser = new User();
user.setUsername("汪峰");
user.setPassword("1234");
user.setAge(18);
userService.register(user);
}
}
-----------------------------------------------------------------------------------
三、spring整合hibernate:没有hibernate.cfg.xm
1.创建表
2.创建PO类(javaBean+映射文件)
3.Dao层
package com.cjw.dao.impl;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.cjw.dao.UserDao;
import com.cjw.domain.User;
//底层需要SessionFactory,自动创建HibernateTemplate模板
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Override
public void save(User user) {
this.getHibernateTemplate().save(user);
}
}
-----------------------------------------------------------------------------------
4.Service层
6.allicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1.1加载properties文件 -->
<!-- 1.2 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///ssh"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 1.3配置 LocalSessionFactoryBean,获得SessionFactory
* configLocation确定配置文件位置
<propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property>
1)dataSource 数据源
2)hibernateProperties hibernate其他配置项
3) 导入映射文件
mappingLocations ,确定映射文件位置,需要“classpath:”,支持通配符【】
<propertyname="mappingLocations" value="classpath:com/itheima/domain/User.hbm.xml"></property>
<propertyname="mappingLocations" value="classpath:com/itheima/domain/*.hbm.xml"></property>
mappingResources ,加载执行映射文件,从src下开始。不支持通配符*
<propertyname="mappingResources" value="com/itheima/domain/User.hbm.xml"></property>
mappingDirectoryLocations ,加载指定目录下的,所有配置文件
<propertyname="mappingDirectoryLocations" value="classpath:com/itheima/domain/"></property>
mappingJarLocations ,从jar包中获得映射文件
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<property name="mappingLocations" value="classpath:com/cjw/domain/*.hbm.xml"></property>
</bean>
<!-- 3 dao -->
<bean id="userDao" class="com.cjw.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 4 service -->
<bean id="userService" class="com.cjw.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 5 事务管理 -->
<!-- 5.1 事务管理器:HibernateTransactionManager -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 5.2 事务详情,给ABC进行具体事务设置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="register"/>
</tx:attributes>
</tx:advice>
<!-- 5.3 AOP编程,ABCD 筛选ABC -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(*com.cjw.service..*.*(..))"/>
</aop:config>
</beans>
-----------------------------------------------------------------------------------
7.测试类
四、Struts整合Spring,Spring创建Action
1.创建表
2.创建PO类(javaBean+映射文件)
3.Dao层
package com.cjw.dao.impl;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.cjw.dao.UserDao;
import com.cjw.domain.User;
//底层需要SessionFactory,自动创建HibernateTemplate模板
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Override
public void save(User user) {
this.getHibernateTemplate().save(user);
}
}
-----------------------------------------------------------------------------------
4.Service层
5.UserAction
public class UserAction extends ActionSupport implementsModelDriven<User> {
//1.封装数据
private User user=new User();
@Override
public User getModel() {
return user;
}
//2.service
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
/**
* 注册
* @return
*/
public String register() {
userService.register(user);
return "success";
}
}
6.struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 开发模式 -->
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<!--底层自动从spring容器中通过名称获得内容,getBean("userAction") -->
<action name="userAction_*" class="userAction" method="{1}">
<result name="success">/message.jsp</result>
</action>
</package>
</struts>
7.allicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1.1加载properties文件 -->
<!-- 1.2 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///ssh"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 1.3配置 LocalSessionFactoryBean,获得SessionFactory
* configLocation确定配置文件位置
<propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property>
1)dataSource 数据源
2)hibernateProperties hibernate其他配置项
3) 导入映射文件
mappingLocations ,确定映射文件位置,需要“classpath:”,支持通配符【】
<propertyname="mappingLocations" value="classpath:com/itheima/domain/User.hbm.xml"></property>
<propertyname="mappingLocations" value="classpath:com/itheima/domain/*.hbm.xml"></property>
mappingResources ,加载执行映射文件,从src下开始。不支持通配符*
<propertyname="mappingResources" value="com/itheima/domain/User.hbm.xml"></property>
mappingDirectoryLocations ,加载指定目录下的,所有配置文件
<propertyname="mappingDirectoryLocations" value="classpath:com/itheima/domain/"></property>
mappingJarLocations ,从jar包中获得映射文件
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<property name="mappingLocations" value="classpath:com/cjw/domain/*.hbm.xml"></property>
</bean>
<!-- 3 dao -->
<bean id="userDao" class="com.cjw.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 4 service -->
<bean id="userService" class="com.cjw.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 5 事务管理 -->
<!-- 5.1 事务管理器:HibernateTransactionManager -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 5.2 事务详情,给ABC进行具体事务设置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="register"/>
</tx:attributes>
</tx:advice>
<!-- 5.3 AOP编程,ABCD 筛选ABC -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(*com.cjw.service..*.*(..))"/>
</aop:config>
<!--配置action-->
<bean id="userAction" class="com.cjw.web.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
</beans>
8.web.xml
<?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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>ssh</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 1 确定spring xml位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 2 spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 3 struts 前端控制器 -->
<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>
</web-app>
9.编写jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html;charset=UTF-8">
<title>主页</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/userAction_register" method="post">
用户名:<input type="text" name="username"/> <br/>
密码:<input type="password" name="password"/> <br/>
年龄:<input type="text" name="age"/> <br/>
<input type="submit" />
</form>
</body>
</html>
五、Struts整合Spring,Struts创建Action
1.只需要把Spring里面的action配置删除即可
2.action里的class,该怎么写就怎么写