ssh增加测试将spring配置文件分开使整个页面更清晰

接口dao

public interface StudentDao {//将这个dao层作为一个接口

	//创建一个添加的方法
	public void add(Student student);
}

实现dao

public class StudentDaoImpl implements StudentDao {

	//实现studentdao

	//引用一个sessionFactory
	private SessionFactory factory;
	//重写里面的方法
	public void add(Student student) {
		//获取当前session保存对象
		factory.getCurrentSession().save(student);
		
	} 
	public void setFactory(SessionFactory factory) {
		this.factory = factory;
	}
}

业务逻辑层biz接口

public interface StudentBiz {//业务逻辑层接口

	public void add(Student student);
}

业务逻辑层实现

public class StudentBizImpl implements StudentBiz {//实现业务逻辑层的接口

//引用studentDao
	private StudentDao stud;
	//重写接口中的方法
	public void add(Student student) {
          stud.add(student);
		
	}
	public void setStud(StudentDao stud) {
		this.stud = stud;
	}
}

Action类

public class AllAction extends ActionSupport {

	//引用biz
	private StudentBiz stub;
	public String  test() {
		stub.add(new Student("hhah"));
		return NONE;
	}
	public void setStub(StudentBiz stub) {
		this.stub = stub;
	}
}

spring的文件先把它整合在一起基于上一篇不再重写然后再把它分开成几个
可分为
applicationContext-dao
applicationContext-action
applicationContext-biz
applicationContext-common 主

applicationContext-common 主

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"  
    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"
	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">


<!-- spring产生的连接池数据源DataSource c3p0连接池 -->
<context:property-placeholder location="classpath:hibernate.properties"/>
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${url}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="maxPoolSize" value="${max}"></property>
<property name="initialPoolSize" value="${min}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>

<!-- spring代理hibernate产生sessionFactory -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 配置连接池 -->
<property name="dataSource" ref="datasource"></property>
<!-- 方言, 显示SQL语句,SQL语句格式化 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>

<!-- 配置实体类的映射文件 -->
<property name="mappingLocations" value="classpath:com/hlp/entity/*.hbm.xml"></property>
</bean>

<!-- 配置事务管理器transactionManager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!-- 事务管理器的范围内的sessionFactory的任意对象 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 配置切面 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 事务传播特性 -->
<tx:attributes>
      <tx:method name="add*" propagation="REQUIRED"/>
      <tx:method name="delete*" propagation="REQUIRED"/>
      <tx:method name="update*" propagation="REQUIRED"/>
      <tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- 切入点 -->
<aop:config >
<aop:pointcut expression="execution(* com.hlp.biz.impl.*.*(..))" id="pointCut"/>
<aop:advisor advice-ref="txAdvice"  pointcut-ref="pointCut"/>
</aop:config>

</beans>

applicationContext-dao

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 用spring创建studentDao的实现类对象 -->
<bean id="stud" class="com.hlp.dao.impl.StudentDaoImpl">
<property name="factory" ref="sessionFactory"></property>
</bean>

</beans>

applicationContext-action

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 将action文件的产生交给spring -->
<bean id="allAction" class="com.hlp.action.AllAction">
<property name="stub" ref="stub"></property>
</bean>
</beans>

applicationContext-biz

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 用spring创建studentBiz的实现类对象 -->
<bean id="stub" class="com.hlp.biz.impl.StudentBizImpl">
<property name="stud" ref="stud"></property>
</bean>
</beans>

strus文件Struts.xml

<?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="default" namespace="/" extends="struts-default">
      <!-- 将action的配置交给spring之后class便只要写spring的配置文件中的ID名 -->
      <action name="allAction_*" class="allAction" method="{1}"></action>
      </package>
    </struts>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!-- 找到spring配置文件 -->
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <!-- 注意在当所有配置都在一个spring文件当中时要把-*去掉 -->
  <param-value>classpath:applicationContext-*.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>
  <!-- spring加载上下文配置文件的核心加载监听器 -->
  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

hibernate.properties用于在spring文件当中连接池的加载

url=jdbc:mysql://localhost:3306/hibernate4?characterEncoding=utf-8
driverClass=com.mysql.jdbc.Driver
max=10
min=3
user=root
password=123456
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值