说明:涉及毕业设计,正好在学这方面的内容,打算用SSH2来做,但是所学尚浅,一步一个雷区的趟过;
这是一个小小的练习不能再练习的小案例,整合SSH2(Struts2.3.7+Spring3.1.0+Hibernate4.0)
tomcat 6.0 jar1.6 win7 eclipse 9.0 editplus
以下配置文件为完整配置,没有按案例进展配置,基本上应该能够看懂先后顺序,尤其是后面dao service action编写好后 主要就是对applicationContext.xml和struts.xml的编写
基本顺序:
1添加struts2--->在web.xml添加struts2的filter-->添加spring3-->在web.xml添listener--->添加hibernate-->在web.xml添加hibernate的filter
2编写Spring核心配置文件applicationContext.xml--->在里面配置数据库连接-->配置sessionFactory
3 Spring3.1.0接管hibernate.cfg.xml所以就不用在hibernate.cfg.xml里配了。在applicationContext.xml配置sessionFactory时将domain与表的映射关系配好。
4 生成domain(pojo)类-->编写dao TeacherDao(数据库访问对象)接口-->编写dao.impl TeacherDaoImpl -->编写service业务层接口-->编写service.impl实现类
TeacherService TeacherServiceImpl-->编写调用业务Action类TeacherAction
5 在applicationContext.xml中配置事务 并且 关联 上面编写的dao service 和 action
6 在struts.xml中配置好action
6 设计页面
7 测试
项目目录树(里面webroot中的页面是以后要毕业设计扩展的):
(1)准备好相应jar包
(2)在eclipse9 中建立web project 命名为随意了 我的为dsdemon j2ee开发环境选择6.0的
(3)把jar包扔到lib里
建议大家在项目属性里建立自己的jar包 User Library 把所需的jar都add进去
添加struts spring hibernate能力时,不选择eclipse提供的jar库,用刚才自己建立的,避免jar包因版本问题冲突
(4)配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name></display-name>
<!-- 配置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>
<!-- 配置Spring上下文监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置hibernate -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
(5)配置Spring
采用C3P0数据池配置
采用Spring管理事务
采用的是XML注入,Spring将dao service action 互相关联。因为刚接触所以尝试注解方式 注入的时候有困难……
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:annotation-config/>
<!-- 使用c3p0配置数据库连接池 -->
<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/newdsweb"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
<property name="maxPoolSize" value="15"/>
<property name="minPoolSize" value="1"/>
<property name="initialPoolSize" value="3"/>
<property name="idleConnectionTestPeriod" value="60"/>
<property name="maxIdleTime" value="60"/>
</bean>
<!--配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/ds/domain/Teacher.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>
<!-- 事务配置 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 事务通知配置,使用的事务管理器引用到 transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 指定哪些方法需要加入事务,使用通配符来加入需要的方法 -->
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" read-only="true" />
<tx:method name="login*" propagation="REQUIRED" read-only="true" />
<tx:method name="update*" propagation="REQUIRED" read-only="true" />
<tx:method name="delete*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 引入aop的命名空间 -->
<aop:config>
<!-- 切入点指明在执行service所有方法时的事务拦截操作 -->
<aop:pointcut id="daoMethods"
expression="execution(* com.ds.dao.impl.*.*(..))" />
<!-- 定义采用何种拦截操作,引用到 txAdvice -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" />
</aop:config>
<bean id="teacherDao" class="com.ds.dao.impl.TeacherDaoImpl">
<property name="sessionFactory" ref="sessionFactory">
</property>
</bean>
<bean id="teacherService" class="com.ds.service.impl.TeacherServiceImpl">
<property name="teDao" ref="teacherDao"></property>
</bean>
<bean id="teacherAction" class="com.ds.action.TeacherAction">
<property name="teacherService" ref="teacherService"></property>
</bean>
</beans>
(6)利用hibernate反向工程生成domain
这里是com.ds.domain.Teacher,自动生成映射文件Teacher.hbm.xml
(7)编写dao 接口和dao实现类
这里对数据库的操作用sessionFactory和Query 并不用hibernate的模板
代码如下
TeacherDao
package com.ds.dao;
import java.util.List;
import com.ds.domain.Teacher;
public interface TeacherDao {
public Teacher login(Teacher teacher);
public List findByExample(Teacher teacher);
public void add(Teacher teacher);
public void update(Teacher teacher);
public void delete(int id);
List<Teacher> getTeacher();
}
TeacherDaoImpl(我这里掺杂了注解内容,是因为刚开始直接尝试用注解,把注解取消掉即可 不影响)
package com.ds.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.ds.dao.TeacherDao;
import com.ds.domain.Teacher;
@Repository
@Transactional
public class TeacherDaoImpl implements TeacherDao {
@Resource
private SessionFactory sessionFactory;
private Session session;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public Session getSession() {
session = this.sessionFactory.getCurrentSession();
return session;
}
@SuppressWarnings("unchecked")
@Override
public Teacher login(Teacher teacher) {
String hql = "from Teacher where teid='"+teacher.getTeid()+"' and pwd='"+teacher.getPwd()+"'";
String hql2 = "from Teacher where teid='40701' and pwd='123456'";
session = this.getSession();
List<Teacher> list = session.createQuery(hql).list();
if(list.size()==1){
teacher = list.get(0);
}else{
teacher = null;
}
return teacher;
}
@Override
public void add(Teacher teacher) {
// TODO Auto-generated method stub
}
@Override
public void update(Teacher teacher) {
// TODO Auto-generated method stub
}
@Override
public void delete(int id) {
// TODO Auto-generated method stub
}
@Override
public List<Teacher> getTeacher() {
// TODO Auto-generated method stub
return null;
}
@Override
public List findByExample(Teacher teacher) {
return sessionFactory.getCurrentSession().createQuery("from Teacher").list();
}
}
(8)编写service和serviceImpl
package com.ds.service;
import com.ds.domain.Teacher;
public interface TeacherService {
public boolean findlogin(Teacher teacher);
}
package com.ds.service.impl;
import com.ds.dao.TeacherDao;
import com.ds.domain.Teacher;
import com.ds.service.TeacherService;
public class TeacherServiceImpl implements TeacherService {
private TeacherDao teDao;
public void setTeDao(TeacherDao teDao) {
this.teDao = teDao;
}
public TeacherDao getTeDao() {
return teDao;
}
@Override
public boolean findlogin(Teacher teacher) {
if(teDao.login(teacher)!=null){
return true;
}
return false;
}
}
(9)编写action并配置struts.xml
package com.ds.action;
import com.ds.domain.Teacher;
import com.ds.service.TeacherService;
import com.opensymphony.xwork2.ActionSupport;
public class TeacherAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private TeacherService teacherService;
private Teacher teacher;
public String login(){
if(teacherService.findlogin(teacher)){
return "success";
}else{
return "error";
}
}
public void setTeacherService(TeacherService teacherService) {
this.teacherService = teacherService;
}
public TeacherService getTeacherService() {
return teacherService;
}
public Teacher getTeacher(){
return teacher;
}
public void setTeacher(Teacher teacher){
this.teacher = teacher;
}
}
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.action.extension" value="do,action" />
<!-- 启用动态调用方法 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!--启用错误信息调试模式。显示更详细的错误信息 -->
<constant name="struts.devMode" value="false" />
<package name="teacherLogin" namespace="/" extends="struts-default">
<action name="TeacherAction" class="teacherAction" method="login">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
(10)页面,使用struts标签<%@ taglib prefix="s“ uri="/struts-tag" %>
<s:form action="TeacherAction" namespace="/">
<s:textfield name="teacher.teid" label="用户名"></s:textfield>
<s:password name="teacher.pwd" label="密码"></s:password>
<s:submit value="确定"/>
<s:reset value="重置"/>
</s:form>
service方法中对dao接口对象的getter 和 setter方法 不要丢
action中domain对象的名称使用一致Teacher teacher,使用Teacher te则出错
action的命名空间要通页面的提交给哪个action的命名空间一致
sessionFactory 的setter getter方法 缺失会导致获取session null值的情况