1、思路
Myeclipse环境,工程环境,Tomcat环境的jdk版本保持一致
1)新建一个项目,把编码格式改为 utf-8
2)把 jsp 的编码形式改为utf-8
3)在 lib 文件夹中建立小包,区分不同的 jar 包
4)建项目 s2sh_sh,再建src、config、test 三个 source folder
在 config中建 hibernate、spring 文件夹存放配置文件。struts2配置文件放入src中
5)在src下建立包
cn.google.s2sh.domain 存放持久化类&映射文件
6)建dao层和 service,谁先测试先写谁(dao)
在 cn.google.s2sh.dao
public interface PersonDao{
public void savePerson(Person person);
}
在 cn.google.s2sh.dao.impl
public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao{
@Overrride
public void savePerson(Person person){
this.getHibernateTemplate().save(person);
}
}
在 cn.google.s2sh.service
public interface PersonService{
public void savePerson(Person person);
}
在 cn.google.s2sh.service.impl
public class PersonServiceImpl implememts PersonService{
public PersonDao personDao;
封装……
@Override
public void savePerson(Person person){
personDao.savePerson(person);
}
}
7)写spring配置文件
<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-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="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate/hibernate.cfg.xml</value>
</property>
</bean>
</beans>
8)测试建 src/cn.google.s2sh.utils
public class SpringInit {
public static ApplicationContext context;
static{
context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}
}
在test/cn.google.s2sh.test public class SessionFactoryTest extends SpringInit{
@Test
public void testSessionFactory(){
context.getBean("sessionFactory");
}
}
9)根据spring配置文件中有的部分不发生变化,而有的发生变化,来把spring文件中的配置信息分为两部分:·建 applicationContext-db.xml,用来存放“不变的东西”:
<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-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="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate/hibernate.cfg.xml</value>
</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 id="perform" expression="execution(* cn.google.s2sh.sh.service.impl.*.*(..))"/>
<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
</aop:config>
</beans><pre name="code" class="html"> <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-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="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate/hibernate.cfg.xml</value>
</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 id="perform" expression="execution(* cn.google.s2sh.sh.service.impl.*.*(..))"/>
<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
</aop:config>
</beans>
·建 applicationContext-person.xml,用来存放“变化的内容”
<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-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.google.s2sh.sh.dao.impl.PersonDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="personService" class="cn.google.s2sh.sh.service.impl.PersonServiceImpl">
<property name="personDao">
<ref bean="personDao"/>
</property>
</bean>
<bean id="personAction" class="cn.google.s2sh.struts2.action.sh.PersonAction" scope="prototype">
<property name="personService">
<ref bean="personService"/>
</property>
</bean>
</beans>
·修改 applicationContext.xml,存入两个 spring 配置文件:
<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-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>
10)测试,在test/cn.itcast.s2sh.test中
public class PersonTest extends SpringInit{
@Test
public void testSavePerson(){
PersonService personService = (PersonService)context.getBean("personService");
Person person = new Person();
person.setPname("aa11");
personService.savePerson(person);
}
}
结果:person表插入一行数据 aa11