提示:
本文是SSH整合中的笔记,所以在记录时,配置内容有所省略,所以建议SSH零基础者慎之,以防因某一步错误而不得完成此demo,尽量不误人子弟!
SSH整合,整合Struts2、Hibernate和Spring(只是简单整合的demo,故没有涉及事务部分的内容,Struts拦截器等),测试内容为简单的用户登录和查询两个功能
有完整源码,测试请自行建立Oracle表(字段参考show.jsp页面)
源码bdy链接:https://pan.baidu.com/s/1erU0F4RtSywx9TfhSDuY3Q 密码:76i8
链接失效请联系我更新。
整合思路
整合概述: Struts对MVC封装、Hibernate完成持久层,两者都应交给Spring管理,先整合Hibernate和Spring
整合流程:
1、将原来Hibernate的cfg文件(配置文件)交给Spring管理(Spring配置dataSource):得到数据源
2、Spring核心为sessionFactory:得到sessionFactory
3、建立DAO、Action、Entity等
4、建立Test测试
Hibernate内容包含了两个xml文件:
1、cfg文件(引用hbm,包含数据库配置url,driver、user、pwd、etc……)
2、hbm文件(实体映射)
Struts内容包含1个xml文件:struts.xml
整合完毕包含四个xml文件(四类文件:entity每个都会对应hbm文件):web.xml、cfg、hbm、struts
整合准备:
1、建立web项目
2、导入Strust2、Hibernate和Spring包(注意导入reference包)(jar存在多个版本,选择较高版本)
3、建立xml(共4类)文件:src下建立applicationContext.xml 配置
<bean>
4、参考jar包(37个)
注意:applicationContext.xml按照流程分开书写,分为得到数据源、得到sessionFactory、整合action、dao配置等
完整配置、源码参考链接。
1、得到数据源
applicationContext.xml(第一部分)
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean>
2、得到sessionFactory
applicationContext.xml(第二部分)
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/it/ssh/entity/Person.hbm.xml</value>
</list>
</property>
</bean>
<!--整合完毕后,DAO,SERVICE要做一下配置(即第三部分)-->
<bean id="personDao" class="com.it.ssh.dao.impl.PersonDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="personService" class="com.it.ssh.service.impl.PersonServiceImpl">
<property name="personDao" ref="personDao"></property>
</bean>
<!--整合了Struts之后,不能直接找action,没有personService会有空指针异常。应该先找到Spring配置文件中的内容-->
<bean name="personAction" class="com.it.ssh.action.PersonAction">
<property name="personService" ref="personService"></property>
</bean>
3、Struts.xml
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<action name="personAction" class="personAction">
<result>show.jsp</result>
<result name="input" type="redirect">login.jsp</result>
</action>
</package>
<include file="example.xml"/>
<!-- Add packages here -->
</struts>
4、person.hbm.xml
<hibernate-mapping package="com.it.ssh.entity">
<class name="Person">
<id name="pid" type="int">
<generator class="sequence">
<param name="sequence">se_per</param>
</generator>
</id>
<property name="pname" type="string" />
<property name="pwd" type="string"></property>
</class>
</hibernate-mapping>
5、web.xml
<display-name>ssh</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--注意:服务器启动时就应该先加载applicationContext.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--配置application文件位置时注意:应该为:WEB-INFO/classes/applicationContext.xml或classpath:applicationContext.xml-->
<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>
<!--error-page可设置error页,
Struts中global result的配置最终执行到这里,后面覆盖前面的设置内容-->
<!-- <error-page>
<error-code>404</error-code>
<location>……</location>
</error-page> -->
</web-app>
6、entity
public class Person {
private int pid;
private String pname;
private String pwd;
public Person() {
// TODO Auto-generated constructor stub
}
public Person(int pid, String pname, String pwd) {
super();
this.pid = pid;
this.pname = pname;
this.pwd = pwd;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
7、DAO
7.1、com.dao
public interface PersonDao {
public boolean login(Person person);
public boolean add(Person person);
public boolean update(Person person);
public boolean delete(int pid);
public Person queryById(int pid);
public List<Person> queryAll();
}
7.2、com.dao.impl
public class PersonDaoImpl implements PersonDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public boolean login(Person person) {
String hql="from Person where pid=? and pwd=?";
Session session=sessionFactory.openSession();
Query q=session.createQuery(hql);
q.setInteger(0, person.getPid());
q.setString(1, person.getPwd());
Object obj=q.uniqueResult();
boolean flag=false;
if(obj!=null){
flag=true;
}
session.close();
return flag;
}
@Override
public boolean add(Person person) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean update(Person person) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean delete(int pid) {
// TODO Auto-generated method stub
return false;
}
@Override
public Person queryById(int pid) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Person> queryAll() {
String hql="from Person";
Session session=sessionFactory.openSession();
Query q=session.createQuery(hql);
List<Person> list=q.list();
session.close();
return list;
}
}
8、action
public class PersonAction extends ActionSupport implements ModelDriven<Person> {
Person person=new Person();
private PersonService personService;
public void setPersonService(PersonService personService) {
this.personService = personService;
}
private List<Person> list;
public List<Person> getList() {
return list;
}
public void setList(List<Person> list) {
this.list = list;
}
@Override
public Person getModel() {
// TODO Auto-generated method stub
return person;
}
public String login() throws Exception {
boolean flag=personService.login(person);
String path=INPUT;
if(flag){
path=SUCCESS;
queryAll();
}
return path;
}
public String queryAll() throws Exception {
list=personService.queryAll();
return "success";
}
}
9、service
9.1、com.service
public interface PersonService {
public boolean login(Person person);
public boolean add(Person person);
public boolean update(Person person);
public boolean delete(int pid);
public Person queryById(int pid);
public List<Person> queryAll();
}
9.2、com.service.impl
public class PersonServiceImpl implements PersonService {
private PersonDao personDao;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
@Override
public boolean login(Person person) {
// TODO Auto-generated method stub
return personDao.login(person);
}
@Override
public boolean add(Person person) {
// TODO Auto-generated method stub
return personDao.add(person);
}
@Override
public boolean update(Person person) {
// TODO Auto-generated method stub
return personDao.update(person);
}
@Override
public boolean delete(int pid) {
// TODO Auto-generated method stub
return personDao.delete(pid);
}
@Override
public Person queryById(int pid) {
// TODO Auto-generated method stub
return personDao.queryById(pid);
}
@Override
public List<Person> queryAll() {
// TODO Auto-generated method stub
return personDao.queryAll();
}
}
10、Test
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
PersonService personService=(PersonService)ac.getBean("personService");
Person person=new Person(10025, "tom", "123456");
boolean flag=personService.login(person);
System.out.println(flag);
}
}
11、login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
<s:form action="personAction!login" method="post">
<s:textfield name="pid" label="帐号"></s:textfield>
<s:password name="pwd" label="密码"></s:password>
<tr>
<td colspan="2">
<s:submit value="提交" theme="simple"></s:submit>
<s:reset value="重置" theme="simple"></s:reset>
</td>
</tr>
</s:form>
</body>
</html>
12.show.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
<table border="1" width="300px">
<tr>
<th>编号</th>
<th>姓名</th>
<th>密码</th>
</tr>
<s:iterator value="list" >
<tr>
<td><s:property value="pid"/></td>
<td><s:property value="pname"/></td>
<td><s:property value="pwd"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>
后记:SSH简单整合、包含连接Oracle登录用户、queryall两个功能。有不足之处还望海涵,欢迎指正错误!
可联系[email protected]作以学习交流。