经过十一周两场考试和电子实训后,学习也进入了新的阶段,突发奇想想尝试做一个简易的聊天app项目,以后在朋友间使用也是一件挺有趣的事情。
首先先从服务端的搭建开始,服务端最主要的就是进行数据的处理和数据库的连接。我目前学习的有了jdbc连接数据库+MVC开发模式、使用MyBatis搭建,虽然用以有的知识也能搞定但学多的知识总有好处,所以我选择了最经典的Struts2+Spring+Hibernate(SSH)来搭建服务端。
由于这次是直接使用ssh框架对每一个组成的框架我只是有一个大概了解,以后有时间再深入学习。
一、SSH所需开发包
刚学习SSH遇到的第一个困难就是导包,因为实在是太多太多了,毕竟我还是一只菜鸡没见过什么大工程= =。
spring所需的jar包
spring.png
struts2所需jar包
struts.png
Hibernate所需jar包
hibernate.png
项目使用MySql数据库,所以还有一个MySql的驱动包总共的jar包应该有41个,接下来对一些重要jar包做一下解释。
struts2-convention-plugin-2.3.15.3.jar -struts注解开发包
struts2-spring-plugin-2.3.15.3.jar -struts整合spring的jar包
事务管理jar包
spring-tx-3.2.0.RELEASE.jar
spring-jdbc-3.2.0.RELEASE.jar
spring-orm-3.2.0.RELEASE.jar -spring整合hibernate的jar包
spring-web-3.2.0.RELEASE.jar -spring整合web项目的jar包
spring-test-3.2.0.RELEASE.jar Junit测试单元
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar c3p0连接池jar包
二、引入配置文件
struts配置文件
web.xml
struts.xml
spring配置文件
web.xml
applicationContext.xml
在ssh整合框架中不需要Hibernate的配置文件因此不用引入
三、配置文件的配置
因为内容有些多而且不好解释直接上代码
web.xml
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">
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
struts
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts
/*
index.jsp
applicationContext.xml
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
struts.xml
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
这里需要万分注意的是Spring核心监听器的配置即
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
ContextLoaderListener中有一个参数,他默认是加载WEB-INF目录下的applicationContext.xml,但我们需要加载的是classes目录下的applicationContext.xml,所以在这里边我们需要配置一个全局触发参数。
首先引入源代码:
ctrl+左击点击org.springframework.web.context.ContextLoaderListener进入如下页面
ContextLoaderListener.png
然后点击AttachSource
引入源码.png
此部很重要因此在此详细说明,还有就是配置文件都应该放在src目录下
三、创建包结构
使用SSH框架在不知不觉中以使用了MVC开发模式,此包结构仅当参考
包结构的创建.png
四、Struts整合Spring-编写Action/Service/
public class EmployeeAction extends ActionSupport implements ModelDriven{
//模型驱动
private Employee employee=new Employee();
public Employee getModel() {
// TODO Auto-generated method stub
return employee;
}
//注入业务层
private EmployeeService employeeService;
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
}
在Spring中只要有set方法就可以完成自动注入,Service提供set方法注入Dao
public class EmployeeServiceImpl implements EmployeeService {
private EmployeeDao employeeDao;
/**
* @param employeeDao the employeeDao to set
*/
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
}
五、Struts整合Spring-在配置文件中配置Action/Service/
打开applicationContext.xml
六、Spring整合Hibernate-创建数据库
手动创建好数据库后,交给hibernat会自动帮我们创立表
找到相应的Bean并创建对应映射文件。
Department.java
public class Department {
private Integer did;
private String dname;
private String dinfo;
//员工集合
private Set employee=new HashSet();
public Department() {
}
public Integer getDid() {
return did;
}
public void setDid(Integer did) {
this.did = did;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getDinfo() {
return dinfo;
}
public void setDinfo(String dinfo) {
this.dinfo = dinfo;
}
public Set getEmployee() {
return employee;
}
public void setEmployee(Set employee) {
this.employee = employee;
}
}
与之相对应的xml
/p>
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
注意在配置相应xml的时候要引入Hibernate的约束条件。约束条件在相应hibernate的核心包下面的hibernate-mapping-3.0dtd中,将映射条件复制到我们数据库映射文件下即可。
七、spring整合hibernate配置文件
jdbc的配置文件
jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/good
jdbc.username=root
jdbc.password=12312313
jdbc配置文件要在applicationContext.xml中引入
org.hibernate.dialect.MySQLDialect
true
true
update
cn/crm/ssh/bean/xml/Department.hbn.xml
cn/crm/ssh/bean/xml/Employee.hbn.xml
总结
现在对SSH只是有一个基本了解,与之前学习最大的不同即是没有了Servlet取而代之的是Action。数据库不用手动去创建直接交给hibernate自动生成即可,在使用SSH框架时不知不觉间已使用了MVC的开发模式,这对以后进一步学习服务端搭建提供了很好的学习基础,最后希望这篇文章对你有用让我们共同进步。