整合开发Struts2,Hibernate,Spring简单框架的搭建
第一步:整合开发Struts2,Hibernate,Spring需要的JAR 如图所示:
第二步:在spring中的applicationContext.xml配置数据库连接池,sessionFactory工厂,hibernate事务管理器,事务的通知给事务配置一个切面,导入其他的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" 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/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="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8" />
<property name="user" value="root" />
<property name="password" value="123" />
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="1" />
<!--连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="1" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="300" />
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60" />
</bean>
<!-- sessionFactory工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>cn/csdn/hr/s2sh/domain/Admin.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<!-- hiberante事务管理器 -->
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 事务的通知 -->
<tx:advice id="txadvice" transaction-manager="hibernateTransactionManager">
<tx:attributes>
<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="insert*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 给事务配置一个切面 -->
<aop:config>
<!-- advisor 切入点和通知组合体 -->
<aop:pointcut expression="execution(* cn.csdn.hr.hiberante.service.*.*(..))"
id="txPointcut" />
<aop:advisor advice-ref="txadvice" pointcut-ref="txPointcut" />
</aop:config>
<!-- 导入其它的配置文件 -->
<import resource="beans-dao.xml"/>
<import resource="beans-service.xml"/>
<import resource="beans-action.xml"/>
</beans>
第三步:新建cn.csdn.hr.s2sh.damain包,在此包下新建Admin.java类和对应的Admin.hbm.xml文件。
package cn.csdn.hr.s2sh.domain;
import java.io.Serializable;
//创建了一个实体bean
publicclass Admin implements Serializable{
/**
*
*/
privatestaticfinallongserialVersionUID = 1L;
private Integer id;
private String name;
private String pass;
public Admin() {
super();
// TODO Auto-generated constructor stub
}
public Admin(String name, String pass) {
super();
this.name = name;
this.pass = pass;
}
public Integer getId() {
returnid;
}
publicvoid setId(Integer id) {
this.id = id;
}
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
public String getPass() {
returnpass;
}
publicvoid setPass(String pass) {
this.pass = pass;
}
@Override
public String toString() {
return"Admin [id=" + id + ", name=" + name + ", pass=" + pass + "]";
}
}
Xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.csdn.hr.s2sh.domain">
<class name="Admin" table="admin">
<id column="id" name="id" type="integer">
<generator class="native"/>
</id>
<property name="name" column="name" type="string" length="40" unique="true"/>
<property name="pass" column="pass" type="string" length="40"/>
</class>
</hibernate-mapping>
第四步:新建cn.csdn.hr.s2sh.dao包,在此包下新建接口AdminDao.java和实现此接口的类AdminDaoImpl.java
package cn.csdn.hr.s2sh.dao;
import java.util.List;
import cn.csdn.hr.s2sh.domain.Admin;
public interface AdminDao {
public List<Admin> findAll();
public void delete(Admin entity);
public void update(Admin entity);
public Admin findById(Integer id);
public Admin findByName(String name);
public void insert(Admin entity);
public Admin login(final String name,final String pass);
}
package cn.csdn.hr.s2sh.dao;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.csdn.hr.s2sh.domain.Admin;
public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {
public List<Admin> findAll() {
List<Admin> admins = this.getHibernateTemplate().find("from Admin");
return admins;
}
public void delete(Admin entity) {
this.getHibernateTemplate().delete(entity);
}
public void update(Admin entity) {
this.getHibernateTemplate().update(entity);
}
public Admin findById(Integer id) {
Admin entity = (Admin) this.getHibernateTemplate().get(Admin.class, id);
return entity;
}
public Admin findByName(final String name) {
return (Admin) this.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException,
SQLException {
Object obj = session.createQuery("from Admin where name=:name").setString("name", name).uniqueResult();
return obj;
}
});
}
public void insert(Admin entity) {
this.getHibernateTemplate().save(entity);
}
public Admin login(final String name,final String pass) {
return (Admin) this.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException,
SQLException {
Object obj = session.createQuery("from Admin where name=:name and pass=:pass").setString("name", name).setString("pass", pass).uniqueResult();
return obj;
}
});
}
}
第五步:新建cn.csdn.hr.s2sh.service包,在此包下新建接口AdminService.java和实现此接口的类AdminServiceImpl.java
package cn.csdn.hr.s2sh.service;
import java.util.List;
import cn.csdn.hr.s2sh.domain.Admin;
public interface AdminService {
public List<Admin> findAll();
public void delete(Admin entity);
public void update(Admin entity);
public Admin findById(Integer id);
public Admin findByName(String name);
public void insert(Admin entity);
public Admin login(final String name,final String pass);
}
package cn.csdn.hr.s2sh.service;
import java.util.List;
import cn.csdn.hr.s2sh.dao.AdminDao;
import cn.csdn.hr.s2sh.domain.Admin;
public class AdminServiceImpl implements AdminService {
// Dao对象声明
private AdminDao adminDao;
// 注入的Dao对象
public void setAdminDao(AdminDao adminDao) {
this.adminDao = adminDao;
}
public List<Admin> findAll() {
// TODO Auto-generated method stub
return adminDao.findAll();
}
public void delete(final Admin entity) {
adminDao.delete(entity);
}
public void update(Admin entity) {
adminDao.update(entity);
}
public Admin findById(Integer id) {
return adminDao.findById(id);
}
public Admin findByName(String name) {
return adminDao.findByName(name);
}
public void insert(Admin entity) {
adminDao.insert(entity);
}
public Admin login(String name, String pass) {
return adminDao.login(name, pass);
}
}
第六步:编写登陆界面index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<div>
<form action="/s2sh/csdn/loginAdmin.action" method="post">
用户名:<input type="text" name="admin.name"><br/>
密 码:<input type="password" name="admin.pass"><br />
<input type="submit" value="登陆"><input type="reset"
value="重置">
</form>
</div>
<div>登陆成功的用户名${admin.name}</div>
</body>
</html>
第七步:新建包cn.csdn.hr.s2sh.action在此包下新建类AdminActiobn.java实现简单的登陆的功能。
package cn.csdn.hr.s2sh.action;
import cn.csdn.hr.s2sh.domain.Admin;
import cn.csdn.hr.s2sh.service.AdminService;
import com.opensymphony.xwork2.ActionSupport;
public class AdminAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private AdminService adminService;
private Admin admin;
public Admin getAdmin() {
return admin;
}
public void setAdmin(Admin admin) {
this.admin = admin;
}
public void setAdminService(AdminService adminService) {
this.adminService = adminService;
}
public String login(){
System.out.println("==========================================");
admin = adminService.login(admin.getName(), admin.getPass());
if (admin != null) {
return SUCCESS;
} else {
return INPUT;
}
}
}
第八步:编写struts.xml代码:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--常量 -->
<!-- 是用spring创建和管理struts2 的action操作 -->
<constant name="struts.objectFactory" value="spring"></constant>
<!-- 包含其它的配置文件 -->
<include file="struts-admin.xml"></include>
</struts>
第九步:编写struts-admin.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="" extends="struts-default" namespace="/csdn">
<!-- class的名称等于spring中action配置文件中的 id名称 -->
<action name="loginAdmin" class="adminAction" method="login">
<result name="success">../index.jsp</result>
</action>
</package>
</struts>
第十步:配置admin的action的beans-action.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" 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/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">
<!-- 配置admin的action -->
<bean id="adminAction" class="cn.csdn.hr.s2sh.action.AdminAction" scope="prototype">
<!-- 引入业务bean的操作 -->
<property name="adminService" ref="adminServiceImpl"/>
</bean>
</beans>
第十一步:admin的业务层beans-service.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" 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/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">
<!-- admin的业务操作 -->
<bean id="adminServiceImpl" class="cn.csdn.hr.s2sh.service.AdminServiceImpl">
<property name="adminDao" ref="adminDaoImpl"/>
</bean>
</beans>
第十二步:admin的dao对象
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" 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/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">
<!-- HibernateDaoSupport -->
<bean id="hibernateDaoSupport" class="org.springframework.orm.hibernate3.support.HibernateDaoSupport" abstract="true">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- admin的Dao对象 -->
<bean id="adminDaoImpl" class="cn.csdn.hr.s2sh.dao.AdminDaoImpl" parent="hibernateDaoSupport"/>
</beans>