s2sh案例一
0、 开发jar包:
antlr-2.7.6.jar
aspectjrt.jar
aspectjweaver.jar
backport-util-concurrent.jar
c3p0-0.9.1.jar
cglib-nodep-2.1_3.jar
commons-collections-3.1.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.0.4.jar
commons-logging-1.1.1.jar
commons-logging.jar
dom4j-1.6.1.jar
ehcache-1.5.0.jar
freemarker-2.3.15.jar
hibernate3.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
log4j.jar
mysql-connector-java-5.1.10-bin.jar
ognl-2.7.3.jar
slf4j-api-1.5.8.jar
slf4j-log4j12.jar
spring.jar
struts2-core-2.1.8.1.jar
struts2-spring-plugin-2.1.8.1.jar
xwork-core-2.1.6.jar
1、 类
**********************************************************************************************
bean类:
public class Person implements Serializable {
private Long pid;
private String pname;
private String psex;
// set/get属性
}
dao接口:
public interface PersonDao {
public void savePerson(Person person);
public Person getPersonById(Serializable id);
}
dao实现类:
public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {
public void savePerson(Person person) {
this.getHibernateTemplate().save(person);
}
public Person getPersonById(Serializable id) {
return (Person) this.getHibernateTemplate().load(Person.class, id);
}
}
service接口:
public interface PersonService {
public void savePerson(Person person);
public Person getPersonById(Serializable id);
}
service实现类:
public class PersonServiceImpl implements PersonService {
private PersonDao personDao;
// set/get属性
public void savePerson(Person person) {
this.personDao.savePerson(person);
}
public Person getPersonById(Serializable id) {
return this.personDao.getPersonById(id);
}
}
Action动作类:
public class PersonAction extends ActionSupport {
private PersonService personService;
// set/get属性
public String savePerson(){
Person person = new Person();
person.setPname("小李222");
person.setPsex("男");
this.personService.savePerson(person);
System.out.println("aaa222");
return "index";
}
public String showPerson(){
Person person = this.personService.getPersonById(1L);
ServletActionContext.getRequest().setAttribute("person", person);
return "index";
}
}
工具类:
public class SpringInit {
public static ApplicationContext context;
static{
context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}
}
显示层index.jsp:
<body>
This is my JSP page. <br>
${requestScope.person.pname}
</body>
**********************************************************************************************
2、配置文件:
**********************************************************************************************
-----------------------------------------------------------------------------------------
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>
<!-- 自动加载配置文件 -->
<constant name="struts.devMode" value="true"></constant>
<include file="struts2/struts-person.xml"></include>
</struts>
-----------------------------------------------------------------------------------------
struts-person.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="person" namespace="/" extends="struts-default">
<!-- 注: class的值与spring配置中的action类的id值对应 -->
<action name="personAction_*" method="{1}" class="personAction">
<result name="index">index.jsp</result>
</action>
</package>
</struts>
-----------------------------------------------------------------------------------------
hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库的用户名 -->
<property name="connection.username">root</property>
<!-- 密码 -->
<property name="connection.password">123456</property>
<!-- url -->
<property name="connection.url">
jdbc:mysql://localhost:3306/s2shDemo
</property>
<!-- 方言 告诉hibernate链接的mysql数据库 -->
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 作用:根据持久化类和映射文件生成表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 显示hibernate内部生成的sql语句 -->
<property name="show_sql">true</property>
<!-- hibernate中使用currentSession需要配置该内容 -->
<property name="current_session_context_class">thread</property>
<!-- 加载映射文件 -->
<mapping resource="cn/itcast/domain/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
-----------------------------------------------------------------------------------------
Person.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itcast.domain.Person">
<id name="pid" length="10">
<generator class="increment"></generator>
</id>
<property name="pname" length="20"></property>
<property name="psex" length="25"></property>
</class>
</hibernate-mapping>
-----------------------------------------------------------------------------------------
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:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<import resource="applicationContext-db.xml"/>
<import resource="applicationContext-person.xml"/>
</beans>
-----------------------------------------------------------------------------------------
applicationContext-db.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:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- spring结合hibernate配置sessionFactory的方式 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate/hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" read-only="false"/>
<tx:method name="update*" read-only="false"/>
<tx:method name="delete*" read-only="false"/>
<!-- *代表了除了上述三种情况的以外情况 -->
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* cn.itcast.service.impl.*.*(..))" id="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
</aop:config>
</beans>
-----------------------------------------------------------------------------------------
applicationContext-person.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:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="personDao" class="cn.itcast.dao.impl.PersonDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="personService" class="cn.itcast.service.impl.PersonServiceImpl">
<property name="personDao">
<ref bean="personDao"/>
</property>
</bean>
<!-- scope="prototype 多例 -->
<bean id="personAction" class="cn.itcast.action.PersonAction" scope="prototype">
<property name="personService">
<ref bean="personService"/>
</property>
</bean>
</beans>
-----------------------------------------------------------------------------------------
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 整合spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!-- OpenSessionInViewFilter的过滤器 注:必须放在strust2的过滤器前面(因为此时要打开session) -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- struts2的过滤器 -->
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
-----------------------------------------------------------------------------------------
******************************************************************************************
3、测试类
-----------------------------------------------------------------------------------------
public class ConfigTest extends SpringInit{
@Test
public void test(){
context.getBean("sessionFactory");
// 还要测试各个bean对象
}
}
public class PersonTest extends SpringInit {
@Test
public void test(){
// 注: 启事务是在service层启动的,所以用personService来操作 目标类
// 此处要打断点,验证为jdk动态代理$proxy4
PersonService personService = (PersonService) context.getBean("personService");
Person person = new Person();
person.setPname("小1");
person.setPsex("女");
personService.savePerson(person);
}
}
-----------------------------------------------------------------------------------------