2014-7-15
一.SSH框架搭建
1.新建一个工程,CheHypertension.项目右键,编码格式更改为utf-8.
2.导入所有lib包.包括连接oracle的驱动.ojdbc6.jar
3.从后往前搭框架:先是持久层(domain)。创建一个测试数据库:
4.安装oracle 11g数据库HYPERDB,开启服务后使用系统管理员sys/system登陆,密码为安装数据库的时候设置的密码,为123456.
5.创建用户chejinqiang: create user chejinqiang identified by chejinqiang;
6.授权用户chejinqiang: grant dba,connect to chejinqiang.
7.以用户chejinqiang登陆数据库: conn chejinqiang/chejinqiang<用户名/密码>
8.创建测试数据表:
create table department(
id char(36) primary key,
name varchar2(30) unique not null
);
9. 新建一个包:che.domain。新建一个与表名一样的java类Department实现java.io.serializable接口。将表中的属性改成java类当中的属性。实现相应的getter,setter方法。
10.在相同包下新建一个xml文件,名称为:Department.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="che.domain.Department" table="department">
<id name="id" type="string">
<column name="id" sql-type="CHAR(36)"></column>
<!-- <generator class="uuid"></generator> 自动增长 -->
</id>
<property name="name" type="string">
<column name="name"></column>
</property>
</class>
</hibernate-mapping>
11.在src下新建一个XML文件,hibernate.cfg.xml.。配置数据库连接信息。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 基本信息 -->
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:HYPERDB</property>
<property name="hibernate.connection.username">chejinqiang</property>
<property name="hibernate.connection.password">chejinqiang</property>
<!-- 使事务自动提交 -->
<!--<property name="hibernate.connection.autocommit">true</property>-->
<!-- 配置 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<!-- 添加映射的hbm.xml -->
<mapping resource="che/domain/Department.hbm.xml"/>
</session-factory>
</hibernate-configuration>
12.新建一个包,junit.新建一个测试类,TestHibernate.java类。
@Test
public void testSave(){
//
Configuration configuration=new Configuration();
//加载类路径下的hibernate.cfg.xml
configuration.configure();
//调用sessionFactory
SessionFactory sfFactory=configuration.buildSessionFactory();
//打开session
Session session=sfFactory.openSession();
//开启事务
Transaction tr=session.beginTransaction();
Department department=new Department();
department.setId("1");
department.setName("人员管理");
session.save(department);
//提交事务
tr.commit();
//关闭session
session.close();
}
13.系统DAO层的封装
新建包che.dao。新建包che.dao.impl。新建一个接口,IDepartmentDao.java。新建该接口的实现类DepartmentDaoImpl.java实现IDepartmentDao接口。
14.新建一个ICommonDao.java接口
Public interface ICommonDao<T>{
Void save(T entity);
}
public interface IDepartmentDao extends ICommonDao<Department> {
public static final String SERVICE_NAME = "che.dao.impl.DepartmentDaoImpl";
}
@Repository(IDepartmentDao.SERVICE_NAME)
public class DepartmentDaoImpl extends CommonDaoImpl<Department> implements
IDepartmentDao {
}
15.新建一个实现类,实现ICommonDao接口。CommonDaoImpl.让DepartmentDaoImpl继承CommonDaoImpl类。IDepartmentDao继承ICommonDao<ElecText>
public class CommonDaoImpl<T> extends HibernateDaoSupport implements ICommonDao<T> {
/**
* <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
注入sessionFactory给hibernateTemplate。sessionFactory在beans.xml可找到。
*/
@Resource(name="sessionFactory")
public final void setSessionFactoryDi(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
@Override
public void save(T entity) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(entity);
}
}
16.在src目录下创建beans.xml<spring容器>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!-- 1、注解的自动扫描,表示组件(如:@controler,@Service,@Repository,@Resource等)的扫描 -->
<context:component-scan base-package="che"></context:component-scan>
<!-- 2、? -->
<!-- 3、创建由spring提供的sessionFactory,这是spring整合hibernate的核心 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>
classpath:hibernate.cfg.xml
</value>
</property>
</bean>
<!--4、创建事务管理器,由spring负责创建 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 5、使用注解的形式管理事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans
17.创建测试文件
public void testSaveDepartment(){
//加载类路径下的beans.xml
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
//获取spring容器中的bean的id结点
IDepartmentDao departmentDao=(IDepartmentDao)ac.getBean(IDepartmentDao.SERVICE_NAME);
Department department=new Department();
department.setId("2");
department.setName("部门管理");
//必须在hibernate.cfg.xml中配置事务自动提交
departmentDao.save(department);
}
18.业务层的封装
新建包che.service,创建IDepartmentService.java
public interface IDepartmentService {
public static final String SERVICE_NAME = "che.service.impl.DepartmentServiceImpl";
void saveDepartment(Department department);
}
19.新建包che.service.impl,创建DepartmentServiceImpl实现类。
@Service(IDepartmentService.SERVICE_NAME)
@Transactional(readOnly=true)
public class DepartmentServiceImpl implements IDepartmentService {
@Resource(name=IDepartmentDao.SERVICE_NAME)
private IDepartmentDao departmentDao;
@Override
@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
public void saveDepartment(Department department) {
// TODO Auto-generated method stub
departmentDao.save(department);
}
}
20.新建testService.java.
@Test
public void testSaveDepartment(){
//加载类路径下的beans.xml
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
//获取spring容器中的bean的id结点
IDepartmentService departmentService=(IDepartmentService)ac.getBean(IDepartmentService.SERVICE_NAME);
Department department=new Department();
department.setId("2");
department.setName("部门管理");
//必须在hibernate.cfg.xml中配置事务自动提交
departmentService.save(department);
}
21.控制层action层的封装
创建包che.action,创建DepartmentAction,使用模型驱动,继承BaseAction.
@Controller("departmentAction")
@Scope(value="prototype")//可以利用容器的scope="prototype"来保证每一个请求有一个单独的Action来处理,避免struts中Action的线程安全问题
public class DepartmentAction extends BaseAction implements
ModelDriven<Department> {
private Department department=new Department();
@Resource(name=IDepartmentService.SERVICE_NAME)
private IDepartmentService departmentService;
@Override
public Department getModel() {
// TODO Auto-generated method stub
return department;
}
public String save(){
departmentService.save(department);
//request对象获取
System.out.println(request.getParameter("name"));
return "success";
}
}
创建BaseAction,用于获取request,response.
@SuppressWarnings("serial")
public class BaseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware {
protected HttpServletRequest request = null;
protected HttpServletResponse response = null;
public void setServletRequest(HttpServletRequest req) {
this.request = req;
}
public void setServletResponse(HttpServletResponse res) {
this.response = res;
}
}
22.在src的目录下,创建struts.xml文件.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 修改访问链接的后缀名 -->
<constant name="struts.action.extension" value="do,action"></constant>
<!-- 设置开发模式,开发时输出更多的错误信息 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 修改ui主题为简单主题 -->
<constant name="struts.ui.theme" value="simple"></constant>
<!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- name属性为每个package设置一个唯一的标识,这个标识在所有的package定义中不能重复。 -->
<!-- 定义package命名空间 该命名空间影响到url的地址,例如此命名空间为/test那么访问是的地址为http://localhost:8080/struts2/test/XX.action -->
<package name="department" namespace="/" extends="struts-default">
<action name="departmentAction_*" class="departmentAction" method="{1}">
<result name="success">/system/textAdd.jsp</result>
</action>
</package>
</struts>
25.在web.xml中配置:添加:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:beans.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>
至此,框架搭建完毕。