SSH实例(2)
在WebContent\WEB-INF\下新建两个文件:applicationContext.xml和web.xml。

web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd"> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> </web-app>
web.xml指定了filter和listener。
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" >
<ref local="dataSource"/>
</property>
<!-- 配置Hibernate的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- 指定Hibernate映射文件的路径 -->
<property name="mappingResources">
<list>
<value>com/school/entity/Clas.hbm.xml</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/myssh
</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<bean id="clasDAO"
class="com.school.dao.ClasDAOImpl"
abstract="false" lazy-init="default" autowire="default">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="clasService" class="com.school.service.ClasServiceImpl">
<property name="clasDAO" ref="clasDAO"></property>
</bean>
<bean id="clasQueryAction" class="com.school.action.ClasQueryAction">
<property name="clasService" ref="clasService"></property>
</bean>
<bean id="clasAction" class="com.school.action.ClasAction">
<property name="clasService" ref="clasService"></property>
</bean>
</beans>
applicationContext.xml定义了多个bean,其中dataSource定义了连接数据库的url、用户名、密码等属性。sessionFactory配置了Hibernate的属性以及映射文件的路径,映射的com/school/entity/Clas.hbm.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.school.entity.Clas" table="clas">
<id name="id" type="java.lang.Integer">
<column name="ID" precision="22" scale="0" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="50" not-null="true">
<comment>课程名称</comment>
</column>
</property>
<property name="comment" type="java.lang.String">
<column name="COMMENT" length="500" not-null="false">
<comment>课程介绍</comment>
</column>
</property>
</class>
</hibernate-mapping>
该文件对应MySQL数据库中的clas表,表的结构如下:

对应的Clas文件如下:
package com.school.entity;
public class Clas {
// 课程id
private int id;
// 课程名称
private String name;
// 课程介绍
private String comment;
// 默认构造方法
public Clas() {
}
// 包含全部属性的构造方法
public Clas(int id, String name, String comment) {
super();
this.id = id;
this.name = name;
this.comment = comment;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}
在applicationContext.xml中,各个bean之间存在引用关系:
clasDAO对应的类为com.school.dao.ClasDAOImpl,以sessionFactory作为参数;
clasService对应的类为com.school.service.ClasServiceImpl,以clasDAO作为参数;
clasQueryAction对应的类为com.school.action.ClasQueryAction,以clasService作为参数;
clasAction对应的类为com.school.action.ClasAction,以clasService作为参数。
SSH实例(2)的更多相关文章
- python之socket-ssh实例
本文转载自大王http://www.cnblogs.com/alex3714/articles/5830365.html 加有自己的注释,应该会比原文更突出重点些 一. 基本Socket实例 前面讲了 ...
- 【 SSH 实例】使用ssh开发的简单项目
简单的员工管理项目,使用spring.struts1.hibernate开发 applicationContext.xml <?xml version="1.0" encod ...
- SSH实例(7)
运行结果. 浏览课程: 添加课程: 还有删除课程,这里就不演示了.
- SSH实例(6)
在WebContent文件夹下新建query.jsp和save.jsp文件. query.jsp: <%@ page language="java" import=" ...
- SSH实例(5)
在src中新建struts.xml文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...
- SSH实例(4)
Clas.hbm.xml文件如下: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibe ...
- SSH实例(3)
src文件夹的结构如下: clasDao文件: package com.school.service; import java.util.List; import com.school.dao.Cla ...
- SSH实例(1)
首先,配置struts.xml文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE st ...
- SSH搭建完美CURD,含分页算法
今日开始研究使用java平台上的框架解决web服务端的开发. 这是一个完整的SSH实例,在马士兵老师的SSH整合代码基础上,增加用户的增删改查,同时实现structs方式的分页 放出源代码供大家学习参 ...
随机推荐
- web系统架构设计中需要知道的点(前端篇)
上周没写东西,这周写点互联网系统开发中需要了解的技术点,每个点都可以发散出去,连接更多的知识点,打算做个逐步细化的记录. 一个应用的整个生命周期中(生,老,病,死)都需要有一个整体规划. 前期 评估需 ...
- swift tableview的分割线不能到头
1,设置tableview的separatorInset, layoutMargins if(myTableView.respondsToSelector("setSeparatorInse ...
- Java-继承,多态0922-05
28.按要求编写一个Java应用程序: (1)定义一个类,描述一个矩形,包含有长.宽两种属性,和计算面积方法. (2)编写一个类,继承自矩形类,同时该类描述长方体,具有长.宽.高属性, 和计算体积的方 ...
- WebApi系列~基于单请求封装多请求的设计
回到目录 怎么说,单请求封装多请求,这句话确实有点绕了,但还是要看清楚,想明白这到底是怎么一回事,单请求即一次请求(get,post,put,delete),封闭多请求,即在客户端发送的一个请求中可能 ...
- 知方可补不足~sqlserver中的几把锁~续
回到目录 之前写过相关的文章,对脏读,不可重复读,幻读都做了相当的研究,而今天在程序中又出现了这个问题,即当一条数据被update时,另一个线程同时发起了读的操作,这对于序列化级别的事务是不被允许的, ...
- EF架构~对AutoMapper实体映射的扩展
回到目录 AutoMapper在之前我曾经介绍过,今天主要是把它作一下扩展,因为它的调用太麻烦了,呵呵,扩展之后,用着还可以,感觉.net3.5之后,有了扩展方法这个东西,在程序开发速度及表现力上都有 ...
- Java程序员的日常 —— 多进程开发
最近再弄进程管理相关的工作,因此必要的就涉及到各种系统下关于进程的管理. 这里简单的介绍下: 如何在Java中执行命令 在windows下肯定是dos命令了,而在linux则为shell命令.执行的方 ...
- [Java面试十二]数据库概念相关
1. 什么是存储过程?它有什么优点? 答:存储过程是一组予编译的SQL语句,它的优点有: 允许模块化程序设计,就是说只需要创建一次过程,以后在程序中就可以调用该过程任意次. 允许更快执 ...
- DNS正向解析与反向解析
DNS:(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网, 而不去记住能够被机器直接读取的IP数串.通过主机名,最 ...
- PHP性能优化工具–xhprof安装
PHP性能优化工具–xhprof安装,这里我先贴出大致的步骤: 1.获取xhprof 2.编译前预处理 3.编译安装 4.配置php.ini 5.查看运行结果 那么下面我们开始安装xhprof工具吧: ...