Spring总结:(4)SSH框架的配置文件

13 篇文章 0 订阅
6 篇文章 0 订阅

目录

1 Spring和Hibernate

1.1 实体类

1.2 映射 

1.3 编写DAO及其实现类

1.4 编写Service及其实现类

1.5 加载db.properties

1.6 在src下新建spring的配置文件applicationContext.xml

2 Struts和Spring整合

2.1 配置类

2.2 业务逻辑类

2.3 在spring配置文件中配置业务逻辑Action,为业务逻辑Action注入service

2.4 在src下添加struts的配置文件并配置

2.5 编写页面


1 Spring和Hibernate

1.1 实体类

public class Userinfo {
	private int userId;
	private String userName;
	private String userPass;
	省去getset方法
}

1.2 映射 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="cn.ssh.entity.Userinfo">
		<id name="userId">
			<generator class="native"></generator>
		</id>
		<property name="userName"></property>
		<property name="userPass"></property>
	</class>
</hibernate-mapping>   

1.3 编写DAO及其实现类

public interface UserinfoDao {
	public List<Userinfo> findAll();
	public Userinfo findById(int userId);
	public int add(Userinfo user);
	public int update(Userinfo user);
	public int delete(int userId);
	
}
public class UserinfoDaoImpl implements UserinfoDao {
	private HibernateTemplate  hibernateTemplate;
	
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	@Override
	public List<Userinfo> findAll() {
//		 List<Userinfo> list = (List<Userinfo>)hibernateTemplate.find("from Userinfo");
		return  hibernateTemplate.loadAll(Userinfo.class);
	}
	@Override
	public Userinfo findById(int userId) {
		return hibernateTemplate.get(Userinfo.class, userId);
	}
	@Override
	public int add(Userinfo user) {
		int count=0;
		try {
			hibernateTemplate.save(user);
			count=1;
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return count;
	}
	@Override
	public int update(Userinfo user) {
		int count=0;
		try {
			hibernateTemplate.update(user);
			count=1;
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return count;
	}
	@Override
	public int delete(int userId) {
		int count=0;
		try {
			Userinfo user = hibernateTemplate.get(Userinfo.class, userId);
			hibernateTemplate.delete(user);
			count=1;
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return count;
	}
}

1.4 编写Service及其实现类

public interface UserinfoService {
	public List<Userinfo> findAll();
	public Userinfo findById(int userId);
	public int add(Userinfo user);
	public int update(Userinfo user);
	public int delete(int userId);
}
public class UserinfoServiceImpl implements UserinfoService {
	private UserinfoDao userinfoDao;
	
	public void setUserinfoDao(UserinfoDao userinfoDao) {
		this.userinfoDao = userinfoDao;
	}
	@Override
	public List<Userinfo> findAll() {
		return userinfoDao.findAll();
	}
	@Override
	public Userinfo findById(int userId) {
		return userinfoDao.findById(userId);
	}
	@Override
	public int add(Userinfo user) {
		return userinfoDao.add(user);
	}
	@Override
	public int update(Userinfo user) {
		return userinfoDao.update(user);
	}
	@Override
	public int delete(int userId) {
		return userinfoDao.delete(userId);
	}
}

1.5 加载db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/hibernate
username=root
password=root

1.6 在src下新建spring的配置文件applicationContext.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"
    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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
       <!--1.加载db.properties(可选) --> 
       <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       		<property name="location" value="classpath:db.properties"></property>
       </bean>
       <!--2.配置数据源 DataSource -->
       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       	   <property name="driverClass" value="${driver}"></property>
       	   <property name="jdbcUrl" value="${url}"></property>
       	   <property name="user" value="${username}"></property>
       	   <property name="password" value="${password}"></property>
       </bean>
       <!--3.配置SessionFactory,注入dataSource -->
       <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
       		<property name="dataSource" ref="dataSource"></property>
       		<!-- 配置hibernate相关属性 -->
       		<property name="hibernateProperties">
       			<props>
       				<prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
       				<prop key="show_sql">true</prop>
       				<prop key="format_sql">true</prop>
       				<prop key="hbm2ddl.auto">update</prop>
       			</props>
       		</property>
       		<!-- 配置映射文件位置 -->
       		<property name="mappingResources">
       			<list>
       				<value>cn/ssh/entity/Userinfo.hbm.xml</value>
       			</list>
       		</property>
       </bean>	
       <!-- 4.配置HibernateTemplate,并注入sessionFactory -->
       <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
       	  <property name="sessionFactory" ref="sessionFactory"></property>
       </bean>
       <!-- 5.配置UserinfoDao,并注入HibernateTemplate -->
       <bean id="userinfoDao" class="cn.ssh.dao.impl.UserinfoDaoImpl">
       	  <property name="hibernateTemplate" ref="hibernateTemplate"></property>
       </bean>
       <!-- 6.配置UserinfoService,并注入UserinfoDao -->
       <bean id="userinfoService" class="cn.ssh.service.impl.UserinfoServiceImpl">
       	   <property name="userinfoDao" ref="userinfoDao"></property>
       </bean>
       <!-- 配置spring声明式事务 -->
       <!-- 配置hibernteTransactionManager,注入sessionFactory -->
       <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
       		<property name="sessionFactory" ref="sessionFactory"></property>
       </bean>
       <!-- 配置事务的通知 -->
       <tx:advice id="txAdivce" transaction-manager="transactionManager">
       		<!-- 事务的传播行为 -->
       		<tx:attributes>
       			<tx:method name="*" propagation="REQUIRED"></tx:method>
       		</tx:attributes>
       </tx:advice>
      <aop:config>
      		<!-- 定义切面 -->
      		<aop:pointcut expression="execution(* cn.ssh.service.*.*(..))" id="serviceMethods"></aop:pointcut>
      		<!-- 将事务通知和切面告知通知者 -->
      		<aop:advisor advice-ref="txAdivce" pointcut-ref="serviceMethods"></aop:advisor>
      </aop:config>	
</beans> 

2 Struts和Spring整合

  1. 添加struts2依赖的jar文件和struts2-spring-plugin.jar
  2. 在web.xml中配置监听器ContextLoaderListener(解析spring配置文件)和struts2的核心过滤器

2.1 配置类

<!-- 配置监听器ContextLoaderListener,解析spring配置文件 --> 	
   <listener>
   	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
   <!-- 通过contextConfigLocation参数指定配置文件的位置 -->
   <context-param>
   	<param-name>contextConfigLocation</param-name>
   	<param-value>classpath:applicationContext*.xml</param-value>
   </context-param>
   <!-- 配置struts2的核心过滤器 -->
   <filter>
   	<filter-name>struts2</filter-name>
   	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
   </filter>
   <filter-mapping>
   	<filter-name>struts2</filter-name>
   	<url-pattern>/*</url-pattern>
   </filter-mapping>

2.2 业务逻辑类

public class UserinfoAction extends ActionSupport  implements ServletRequestAware{
	private UserinfoService userinfoService;
	private HttpServletRequest request;
	private Userinfo user;
	
	public Userinfo getUser() {
		return user;
	}
	public void setUser(Userinfo user) {
		this.user = user;
	}
	public void setUserinfoService(UserinfoService userinfoService) {
		this.userinfoService = userinfoService;
	}
	
	public String list() throws Exception {
		List<Userinfo> list = userinfoService.findAll();
		request.setAttribute("list", list);
		return "list";
	}
	
	public String add() throws Exception {
		int count = userinfoService.add(user);
		if(count>0){
			return this.SUCCESS;
		}
		return this.ERROR;
	}
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request=arg0;
	}
}

2.3 在spring配置文件中配置业务逻辑Action,为业务逻辑Action注入service

<bean id="userinfoAction" class="cn.ssh.action.UserinfoAction" scope="prototype">
       	   <property name="userinfoService" ref="userinfoService"></property>
   </bean>

2.4 在src下添加struts的配置文件并配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="sshDemo" extends="struts-default">
		<global-allowed-methods>regex:.*</global-allowed-methods>
		<!-- class="spring容器中action的id" -->
		<action name="user-*" class="userinfoAction" method="{1}">
			<result name="list">/list.jsp</result>
			<result name="success" type="redirectAction">user-list</result>
		</action>
	</package>
</struts>

2.5 编写页面

List.jsp

<body>
  	<table align="center" border="1" width="800">
  		<tr>
  			<td>用户编号</td>
  			<td>用户名称</td>
  			<td>用户密码</td>
  			<td>操作</td>
  		</tr>
  		<s:if test="#request.list.size>0">
  			<s:iterator value="#request.list" var="user">
  				<tr>
  					<td>
  						<s:property value="#user.userId"/>
  					</td>
  					<td>
  						<s:property value="#user.userName"/>
  					</td>
  					<td>
  						<s:property value="#user.userPass"/>
  					</td>
  					<td>
  						修改  删除
  					</td>
  				</tr>
  			</s:iterator>
  		</s:if>
  		<s:else>
  			<tr>
  				<td colspan="4">暂无用户信息!</td>
  			</tr>
  		</s:else>
  	</table>
  </body>

Add.jsp

<body>
  	<form action="user-add.action" method="post">
  		用户名:<input type="text" name="user.userName"/><br>
  		密码:<input type="text" name="user.userPass"/><br>
  		<input type="submit" value="提交"/>
  	</form>
  </body>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值