SSH三大框架整合(Hibernate,Spring,struts2)

刚学完SSH三大框架整合,对初学者来说略显复杂,总结一下(经典永远不会过时)

测试项目结构:

UserDao(用户数据接口)--->UserDaoImpl(实现类)     

UserService(用户业务逻辑接口)-->UserServiceImpl(实现类)

UserAction

-------视图页面----

showUsers.jsp//显示用户数据

index.jsp//请求入口

------------------

ApplicationContext.xml

Hibernate.cfg.xml

struts.xml

------------------------------UserDao接口----------------------

package dao;
import java.util.List;
import entity.User;
public interface UserDao {
    public void save(User user);
    public void saveOrUpdate(User user);
    public void update(User user);
    public void delete(User user);
    public List<User> findAll();
    public User findById(int id);
    public List<User> find(String queryString,Object...values);
    public User load(String username);
    public boolean exists(User user);
    public String getPasswordMD5(User user);
}
------------------UserDaoImpl实现类(以save和find为例)-----------------

public class UserDaoImpl extends DAOSupport  implements UserDao {

	public UserDaoImpl(HibernateTemplate template) {//HibernateTemplate继承自DAOSupport
		super(template);
	}
	
	@Override
	public List<User> findAll() {
		return this.template.find("from User");//User为持久化类
	}

	@Override
	public void save(User user) {//事务管理交给spring实现
		this.template.save(user);
	}<span style="font-family: Arial, Helvetica, sans-serif;">}</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">----------------------------DAOSupport类---------------------</span>

public class DAOSupport
{
    protected HibernateTemplate template;//template通过Spring注入
    
    public DAOSupport(HibernateTemplate template)
    {
    	this.template = template;
    }
}
--------------------------UserService接口--------------

public interface UserService {
	public void addNewUser(User user);
	public List<User> findAll();
}

-----------------------------------UserServiceImpl实现类----------------------------

public class UserServiceImpl implements UserService {
	private UserDao userDao;//通过Spring注入
	
	public UserServiceImpl(UserDao userDao) {
		super();
		this.userDao = userDao;
	}

	@Override
	public void addNewUser(User user) {
		userDao.save(user);	
	}

	@Override
	public List<User> findAll() {
		return this.userDao.findAll();
	}
}
-----------------------------UserAction类----------------------
public class UserAction extends ActionSupport {
	private UserService userService;//通过spring注入,对应setter方法为必须
	private List<User> users;
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public String findAllUsers(){
		this.users = userService.findAll();
		return SUCCESS;
	}
}
------------------------------hibernate.cfg.xml配置-------------------------------
<hibernate-configuration>
	<session-factory> <span style="font-family: Arial, Helvetica, sans-serif;"><property name="dialect"></span><span style="font-family: Arial, Helvetica, sans-serif;">org.hibernate.dialect.MySQLDialect</span><span style="font-family: Arial, Helvetica, sans-serif;"></property></span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><property name="connection.url">jdbc:mysql://localhost:3306/cityrose</property><property name="connection.username">root</property><property name="connection.password">123456</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="show_sql">true</property><mapping resource="entity/User.hbm.xml" /></session-factory></hibernate-configuration></span>
 ------------------------------------ApplicationContext.xml中的配置,实现spring与hibernate的整合----------------------------------- 
<span style="white-space:pre">	<?xml version="1.0" encoding="UTF-8"?>
	<beans xmlns="http://www.springframework.org/schema/beans"
<span style="white-space:pre">	</span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<span style="white-space:pre">	</span>xmlns:p="http://www.springframework.org/schema/p"
<span style="white-space:pre">	</span>xmlns:tx="http://www.springframework.org/schema/tx" 
<span style="white-space:pre">	</span>xmlns:aop="http://www.springframework.org/schema/aop"
<span style="white-space:pre">	</span>xsi:schemaLocation="http://www.springframework.org/schema/beans
<span style="white-space:pre">	</span> http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
<span style="white-space:pre">	</span> http://www.springframework.org/schema/tx 
<span style="white-space:pre">	</span> http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
<span style="white-space:pre">	</span>  http://www.springframework.org/schema/aop
<span style="white-space:pre">	</span> http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"></span>
<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml">
//通过hibernate.cfg.xml装配sessionFactory(还可以通过数据源装配SessionFactory,待续。。。。。)
		</property>
	</bean>

	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
//装配hibernateTemplate,注入到userDao中简化DAO的实现
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean><span style="font-family: Arial, Helvetica, sans-serif;">	</span><span style="font-family: Arial, Helvetica, sans-serif;">	</span>
<span style="white-space:pre"></span><pre name="code" class="html"><!-- aop事务管理 hibernateTemplate.save hibernateTemplate.update等方法的事务交由spring Aop管理,注意tx和aop命名空间的添加-->
 
<span style="white-space:pre"></span><pre name="code" class="html"><span style="white-space:pre">	</span><bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="search*" read-only="true" propagation="REQUIRED"
				isolation="DEFAULT" timeout="-1" />
			<tx:method name="add*" />
			<tx:method name="remove*" />
			<tx:method name="modify*" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut expression="execution(public * service.impl..*(..))"
			id="service" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="service" />
	</aop:config>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><bean id="userDao" class="dao.impl.UserDaoImpl"><constructor-arg><ref bean="hibernateTemplate" /></constructor-arg></bean><bean id="userService" class="service.impl.UserServiceImpl"><constructor-arg><ref bean="userDao" /></constructor-arg></bean></span>
 
</beans>
----------------------Spring和Struts2整合(方法一:spring中显示配置UserAction实例,并为其注入UserService的bean实例)------------------------

前提:在web.xml中配置org.springframework.web.context.ContextLoaderListener监听器启动spring容器,方法如下,并导入struts2-spring-plugin-2.2.1.jar在struts2的jar包中可以找到

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>


spring容器中的相关配置

<bean id="userAction" class="action.UserAction" scope="prototype">
		<property name="userService" ref="userService"></property>
	</bean>

struts.xml中的相关配置

<action name="findAllUsers" class="userAction" method="findAllUsers">
//class属性要配置为spring容器中Action的对象id,不能配置为Action的全类名action.UserAction
	<result name="success">
	/showUsers.jsp
	</result>
</action>

----------------------Spring和Struts2整合(方法二:通过struts2-spring-plugin-2.2.1.jar中struts-plugin.xml中的strutsSpringObjectFactory常量实现spring对action的管理,通过该文件中的name为autowiring的拦截器实现Action的自动装配)拦截器在struts.xml中常量的配置如下-------------

<constant name="struts.objectFactory.spring.autoWire" value="name"></constant>//value="name",根据属性名称自动查找spring容器中的同名bean对action进行属性注入,如上例中UserAction中的userService属性,用容器中id="userService"的bean自动装配
<pre name="code" class="html"><action name="findAllUsers" class="action.UserAction" method="findAllUsers">//class用全类名action.UserAction
			<result name="success">
				/showUsers.jsp
			</result>
		</action>


 前提:在web.xml中配置org.springframework.web.context.ContextLoaderListener监听器启动spring容器,并导入struts2-spring-plugin-2.2.1.jar在struts2的jar包中可以找到(同上) 

index.jsp


showUsers.jsp


至此,SSH三大框架的整合已完成,希望能帮到大家!




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值