创建web工程:sshProject。
设置工程编码集:右键项目名称,选择properties,选择resource,改成utf-8。
在lib下导入jar包:
数据库驱动包
struts包
spring包
hibernate包
创建三个src folder
src :存放源代码
config:存放配置文件
test: 存放测试类
在src下
创建一个包放持久化类和映射文件 类:com.xgss.ssh.domain。
创建其他包:com.xgss.ssh.dao,
com.xgss.ssh.dao.impl,
com.xgss.ssh.service,
com.xgss.ssh.service.impl,
com.xgss.ssh.sturts.action.
在com.xgss.ssh.domain包下建立
持久化类Person.java
public Class Person implements serializable{
private String name;
private Long pid;
private Integer age;
set和get方法
}
映射文件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="com.itheima09.s2sh.domain.Person">
<id name="pid" length="5" type="java.lang.Long" column="pid">
<generator class="increment"></generator>
</id>
<property name="name" column="name" type="java.lang.String" length="20">
</property>
<property name="description" column="description" type="java.lang.String" length="50">
</property>
</class>
</hibernate-mapping>
在config下
创建包:hibernate,spring,struts
在hibernate包下建立
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">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/itheima09_hibernate
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/xgss/ssh/domain/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
在spring包下建立
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-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"/>
</beans>
applicationContext-db.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-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">
//引入sessionFactory,用于获得当前session进行crud操作
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate/hibernate.cfg.xml</value>
</property>
</bean>
</beans>
在test包下建立包和测试类
public class SpringUtils {
public static ApplicationContext context;
static{
//启动spring容器
context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}}
public class SessionFactoryTest extends SpringUtils{
@Test
public void testCreateTable(){
}}
执行测试,数据库中会创建成对应的person表。
创建dao层和service层接口
public interface PersonDao {
public void savePerson(Person person);
public Person getPersonById(Serializable id);
}
public interface PersonService {
public void savePerson(Person person);
public Person getPersonById(Serializable id);
}
编写实现类
public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao{
public void savePerson(Person person) {
this.getHibernateTemplate().save(person);
}
public Person getPersonById(Serializable id) {
return this.getHibernateTemplate().load(Person.class, id);
}
}
public class PersonServiceImpl implements PersonService{
private PersonDao personDao;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
@Override
public void savePerson(Person person) {
this.personDao.savePerson(person);
}
@Override
public Person getPersonById(Serializable id) {
return this.personDao.getPersonById(id);
}
}
配置声明式事务处理
applicationContext-db.xml
<!--
事务管理器
-->
<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:attributes>
</tx:advice>
<aop:config>
<aop:pointcut
expression="execution(* com.itheima09.s2sh.service.impl.*.*(..))" //注意第一个星号后面的空格
id="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
</aop:config>
建立applicationContext-person.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-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="com.itheima09.s2sh.dao.impl.PersonDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="personService" class="com.itheima09.s2sh.service.impl.PersonServiceImpl">
<property name="personDao">
<ref bean="personDao"/>
</property>
</bean>
</beans>
在applicationConfig.xml文件中
<import resource="applicationContext-person.xml"/>
对声明式事务处理进行测试
public class PersonTest extends SpringUtils{
@Test
public void testSavePerson(){
PersonService personService = (PersonService)context.getBean("personService");
Person person = new Person();
person.setName("aa");
personService.savePerson(person);
}
@Test
public void testPersonDao(){
}
}
这样数据库中就会填入对应的数据
编写action
public class PersonAction extends ActionSupport{
private PersonService personService;
public void setPersonService(PersonService personService) {
this.personService = personService;
}
public String savePerson(){
Person person = new Person();
person.setName("asdf");
this.personService.savePerson(person);
return "index";
}
public String showPerson(){
Person person = this.personService.getPersonById(1L);
ServletActionContext.getRequest().setAttribute("person", person);
return "index";
}
}
对action中的service完成注入
在applicationContext-person.xml中添加
<bean id="personAction"
class="com.itheima09.s2sh.action.PersonAction" scope="prototype">
<property name="personService">
<ref bean="personService"/>
</property>
</bean>
在config下建立struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<include file="struts/struts-person.xml"></include>
</struts>
在struts包下建立struts-person.xml
?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="person" namespace="/" extends="struts-default">
<action name="personAction_*" method="{1}" class="personAction">
<result name="index">index.jsp</result>
</action>
</package>
</struts>
编写web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>com-xgss-ssh</display-name>
<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>
<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>
访问http://localhost:8080/sshProject/personAction_savePerson.action
访问到index.jsp,数据库中添加了action中写入的数据。