使用xml方式整合三大框架
首先,新建一个web工程项目,整合的项目基本结构如下:(没有加入view层)
第二步,导入ss2h整合所需要的jar包,如下如:
第三步,编写持久层
1.定义好po类
package com.jjyy.ss2h.po;
/**
* @author Administrator--jiangyu--2014-9-12下午8:00:45
* po类
*/
publicclass User {
private Integer id;
private String name;
private String phone;
public User() {
}
public User(Integer id, String name,String phone) {
super();
this.id = id;
this.name = name;
this.phone = phone;
}
public Integer getId() {
returnid;
}
publicvoid setId(Integer id) {
this.id = id;
}
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
public String getPhone() {
returnphone;
}
publicvoid setPhone(String phone) {
this.phone = phone;
}
}
2.编写对应的*.hbm.xml文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/HibernateMapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.jjyy.ss2h.po" >
<class name="User" table="t_user3">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<property name="phone" column="phone"></property>
</class>
</hibernate-mapping>
第四步,编写服务层
1.定义服务层的接口(由于是简单的整合介绍,就只加入一个业务方法)
package com.jjyy.ss2h.service;
import com.jjyy.ss2h.po.User;
publicinterface UserService {
publicvoid save(User user);
}
2.编写实现服务层的接口类
package com.jjyy.ss2h.service.impl;
importorg.springframework.orm.hibernate3.HibernateTemplate;
importorg.springframework.transaction.annotation.Transactional;
import com.jjyy.ss2h.po.User;
importcom.jjyy.ss2h.service.UserService;
/**
* @author Administrator--jiangyu--2014-9-12下午8:04:33
* 接口的实现类
*/
publicclass UserServiceImpl implements UserService{
/**
* 根据配置文件被动的等待注入
*/
private HibernateTemplate hibernateTemplate;
public HibernateTemplategetHibernateTemplate() {
returnhibernateTemplate;
}
publicvoid setHibernateTemplate(HibernateTemplatehibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
/**
* 开启事务
*/
@Transactional
publicvoid save(User user) {
hibernateTemplate.save(user);
}
}
第五步,编写表现层
package com.jjyy.ss2h.action;
import com.jjyy.ss2h.po.User;
importcom.jjyy.ss2h.service.UserService;
publicclass UserAction {
private UserService userService;
public UserService getUserService() {
returnuserService;
}
publicvoid setUserService(UserService userService) {
this.userService = userService;
}
private User user;
public User getUser() {
returnuser;
}
publicvoid setUser(User user) {
this.user = user;
}
public String save(){
userService.save(user);
returnnull;
}
}
第六步,xml文件的配置
1.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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name></display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
2.applicationContext-action.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<bean id="userAction" class="com.jjyy.ss2h.action.UserAction" scope="prototype">
<property name="userService" ref="userService" ></property>
</bean>
</beans>
3.applicationContext-service.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<bean id="userService" class="com.jjyy.ss2h.service.impl.UserServiceImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/test</prop>
<prop key="hibernate.connection.username">root</prop>
<prop key="hibernate.connection.password">1234</prop>
<prop key="format_sql">true</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/jjyy/ss2h/po/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- 事务的配置 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
4.struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//ApacheSoftware Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/"extends="struts-default">
<action name="userAction" class="userAction"></action>
</package>
</struts>
第六步,测试
启动tomcat服务器,到数据库查看是否生成了t_user3表,在tomcat服务器启动的时候,自动创建了表,这是框架内部帮我们完成的,然后测试一下业务方法save,在浏览器地址栏输入http://localhost:8080/ss2h_xml/userAction!save?user.name=jjyy,回车,在去数据库的t_user3表中查看是否插入了一条name为jjyy的记录。