1.导入相关必须的jar包
spring-4.3.10,struts2-2.3.4,hibernate-4.1.7
antlr-2.7.7.jar
aopalliance-1.0.jar
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
cglib-nodep-2.2.jar
commons-collections-3.2.jar
commons-dbcp-1.4.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
commons-logging-1.1.1.jar
commons-pool-1.5.3.jar
dom4j-1.6.1.jar
freemarker-2.3.22.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.1.7.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
jstl-1.2.jar
log4j-1.2.16.jar
mysql-connector-java-5.1.38-bin.jar
ognl-3.0.6.jar
spring-aop-4.3.10.RELEASE.jar
spring-aspects-4.3.10.RELEASE.jar
spring-beans-4.3.10.RELEASE.jar
spring-context-4.3.10.RELEASE.jar
spring-context-support-4.3.10.RELEASE.jar
spring-core-4.3.10.RELEASE.jar
spring-expression-4.3.10.RELEASE.jar
spring-instrument-4.3.10.RELEASE.jar
spring-instrument-tomcat-4.3.10.RELEASE.jar
spring-jdbc-4.3.10.RELEASE.jar
spring-orm-4.3.10.RELEASE.jar
spring-tx-4.3.10.RELEASE.jar
spring-web-4.3.10.RELEASE.jar
standard-1.1.2.jar
struts2-core-2.3.24.1.jar
struts2-spring-plugin-2.3.24.1.jar
xwork-core-2.3.24.1.jar
2. 配置文件的加入:
Hibernate配置文件:方言,连接,基本配置,映射文件。
Struts2配置文件:package->action->class
Spring配置文件:扫描包,配置数据源,配置SessionFactory
3. 项目结构
4. spring的配置文件applicationContext.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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
<!-- 启用注解 -->
<context:annotation-config/>
<!-- 扫描包,把对象加载到IOC容器中, 需要加载的对象有:Service层中的对象,dao层中的对象,Action层中的对象 -->
<context:component-scan base-package="com.zhq.service"/>
<context:component-scan base-package="com.zhq.dao.impl"/>
<!-- 扫描所有的Action -->
<context:component-scan base-package="com.zhq.action"/>
<!-- 读取properties数据库连接的属性文件 -->
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/jdbc.properties"/>
</bean>
<!-- 配置数据源DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<!--连接池配置 -->
<property name="initialSize" value="${initialSize}"></property>
<property name="maxActive" value="${maxActive}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
<property name="minIdle" value="${minIdle}"></property>
</bean>
<!-- 由Spring来创建SessionFactory对象, 配置SessionFactory对象,用Hibernate操作数据 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 加载Hibernate主配置文件、映射文件、依赖的数据源 -->
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:config/hibernate.cfg.xml"/>
<property name="mappingLocations" value="classpath:com/zhq/mapping/*.hbm.xml"/>
</bean>
<!-- 事务的配置 -->
<bean id="resourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 启用注解配置事务 -->
<tx:annotation-driven transaction-manager="resourceTransactionManager"/>
</beans>
5.struts2配置文件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.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<!-- *表示通配符{1}{2}是占位符,自动取第几个通配符:*的值/student/Student_add -->
<action name="*_*" class="{1}Action" method="{2}">
<result name="success">/{1}_{2}.jsp</result>
</action>
</package>
</struts>
6. hibernate配置文件hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 把name="foo"属性去掉 -->
<session-factory>
<!--这里是Hibernate框架的基本配置:数据库连接,框架参数的配置,映射文件的配置 -->
<!-- 方言,表示Hibernate框架在与某个数据库进行映射,才能自动生成某个数据库特有的SQL语句 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/myschool?characterEncoding=utf-8</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property> -->
<!--Hibernate框架的基本配置 -->
<!-- 打印sql语句,一般开发时运用 ,跟踪自动生成的Sql语句-->
<property name="show_sql">true</property>
<!-- 格式化打印的sql语句,一般开发时运用,便于区分关键字 -->
<property name="format_sql">true</property>
<!--此属性表示根据当前线程来操作数据,可以提交或者回滚 -->
<property name="current_session_context_class">thread</property>
<!--数据库中的表结构可以随映射文件的配置发生改变 -->
<!-- <property name="hbm2ddl.auto">create</property> -->
<!--扫描实体类 -->
<!-- <mapping class="com.zhq.entity.Grade"/>
<mapping class="com.zhq.entity.Subject"/>
<mapping class="com.zhq.entity.Students"/>
<mapping class="com.zhq.entity.StudentsInfo"/>
<mapping class="com.zhq.entity.Teachers"/> -->
</session-factory>
</hibernate-configuration>
7. 主配置文件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" 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>SSH_01</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>
<!--Spring入口 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--Struts2入口 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!-- 当struts2的配置文件不在默认路径下,则需要配置路径,还要额外配置struts2必须的两个文件 -->
<init-param>
<param-name>config</param-name>
<param-value>
struts-default.xml,
struts-plugin.xml,
config/struts.xml
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
8. 数据库配置文件:jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/myschool?characterEncoding\=utf-8
username=root
password=root
initialSize=5
maxActive=50
maxIdle=10
minIdle=5
9. 实体类
package com.zhq.pojo;
import java.util.Date;
public class Student {
private int studentNo;
private String name;
private String sex;
private int age;
private String phone;
private String address;
private String email;
private Date birthday;
public int getStudentNo() {
return studentNo;
}
public void setStudentNo(int studentNo) {
this.studentNo = studentNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
10. dao接口
package com.zhq.dao;
import java.util.List;
import com.zhq.pojo.Student;
public interface StudentDao {
public List<Student> list();
}
11.dao接口实现类
package com.zhq.dao.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.zhq.dao.StudentDao;
import com.zhq.pojo.Student;
@Repository
public class StduentDaoImpl implements StudentDao {
@Autowired //自动注入
SessionFactory factory;
@Override
public List<Student> list() {
//SessionFactory对象操作数据
Session session = factory.openSession();
session.beginTransaction();
String hql ="from Student";
Query query = session.createQuery(hql);
query.setFirstResult(0);
query.setMaxResults(10);
@SuppressWarnings("unchecked")
List<Student> list = query.list();
session.getTransaction().commit();
return list;
}
}
12. service类
package com.zhq.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhq.dao.StudentDao;
import com.zhq.pojo.Student;
@Service
public class StudentService {
//访问StudentDaoImpl类
@Autowired
StudentDao dao;
public List<Student> list(){
return dao.list();
}
}
13. action类
package com.zhq.action;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
import com.zhq.pojo.Student;
import com.zhq.service.StudentService;
//注解,由Spring来进行扫描,扫描到IOC容器中实例化 value:是给对象取名
@Controller(value="studentAction")
public class StudentAction extends ActionSupport {
public List<Student> list;
public List<Student> getList() {
return list;
}
public void setList(List<Student> list) {
this.list = list;
}
@Autowired
StudentService service;
public String list(){
//访问StudentService类取数据
list = service.list();//从底层数据
return SUCCESS;
}
}
14. 实体类映射文件Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 映射文件的根节点,package:表示实体对象的包名 -->
<hibernate-mapping package="com.zhq.pojo">
<!-- class:表示实体类, name:类名 table:表示数据库中的表名-->
<class name="Student" table="student">
<!-- 表示主键 在Hibernate中如果属性名与字段名一致,则可以省略column属性-->
<id name="studentNo">
<!-- 主键的生成策略,native:表示交由数据库按自身特定的自动增长策略来自动生成增长值 -->
<generator class="native"></generator>
</id>
<!-- 表示主键和外键之外的普通列 name:表示实体对象的属性名,column:数据库表中的列名,如果属性名与列名一致,则可以省略column。 -->
<property name="name"></property>
<property name="sex"></property>
<property name="age"></property>
<property name="phone"></property>
<property name="address"></property>
<property name="email"></property>
<property name="birthday"></property>
</class>
</hibernate-mapping>
15. 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>主页</title>
</head>
<body>
<h3>欢迎来到XXX系统!</h3>
<p><a href="student_list">学生列表</a></p>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>学员信息列表</title>
<style type="text/css">
table{width:1000px;border-left: 1px solid #ccc; border-top: 1px solid #ccc;}
th,td{padding: 3px;border-right: 1px solid #ccc; border-bottom: 1px solid #ccc;}
</style>
</head>
<body>
<h2>学员信息列表</h2>
<p>学员人数是:${list.size() } 人</p>
<table>
<tr>
<th>学号</th><th>姓名</th><th>性别</th><th>年龄</th>
<th>电话</th><th>地址</th><th>邮箱</th><th>出生日期</th>
</tr>
<c:forEach items="${list}" var="stu">
<tr>
<td>${stu.studentNo }</td><td>${stu.name }</td>
<td>${stu.sex }</td><td>${stu.age }</td>
<td>${stu.phone }</td><td>${stu.address }</td>
<td>${stu.email }</td><td>${stu.birthday }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
16.运行效果: