1.运用myeclipse建立ssh框架
项目分层:
cn.itcast.elec.containner:自定义的spring容器,用于在控制层调用操作业务层
cn.itcast.elec.dao:项目的dao层,负责链接数据库的操作
cn.itcast.elec.domain:po对象,映射数据库表的映射文件
cn.itcast.elec.service:项目service层,负责操作各个功能模块的业务逻辑
cn.itcast.elec.util封装功用类的方法和属性
cn.itcast.elec.web.action:系统控制层,负责页面和后台跳转
cn.itcast.elec.web.form封装值对象(vo)对象,对应页面传递的表单值属性
junit、:测试类
2.建立持久层对象。
在domain中创建ElecText.java
package cn.itcast.elec.domain;
import java.util.Date;
public class ElecText implements java.io.Serializable{
private String textId;
private String textName;
private Date textDate;
private String textRemark;
public String getTextId() {
return textId;
}
public void setTextId(String textId) {
this.textId = textId;
}
public String getTextName() {
return textName;
}
public void setTextName(String textName) {
this.textName = textName;
}
public Date getTextDate() {
return textDate;
}
public void setTextDate(Date textDate) {
this.textDate = textDate;
}
public String getTextRemark() {
return textRemark;
}
public void setTextRemark(String textRemark) {
this.textRemark = textRemark;
}
}
创建映射文件
<?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="cn.itcast.elec.domain.ElecText" table="Elec_Text">
<id name="textId" type="string">
<column name="textID" sql-type="varchar(50)" not-null="true"/>
<generator class="uuid"/>
</id>
<property name="textName" type="string">
<column name="textName" sql-type="varchar(50)"/>
</property>
<property name="textDate" type="date">
<column name="textDate" length="50"/>
</property>
<property name="textRemark" type="string">
<column name="textRemark" sql-type="varchar(500)"/>
</property>
</class>
</hibernate-mapping>
在hibernate配置文件中创建配置信息
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8</property>
<property name="connection.username">root</property>
<property name="connection.password">c</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto"></property>
<property name="hibernate.format_sql"></property>
<property name="hibernate.show_sql">true</property>
<mapping resource="cn/itcast/elec/domain/ElecText.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3.dao层搭建
dao创建公共接口
package cn.itcast.elec.dao;
public interface ICommonDao<T> {
public void save(T entity);
}
daoImp创建实现类
package cn.itcast.elec.dao.impl;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.itcast.elec.dao.ICommonDao;
public class CommonDaoImpl<T> extends HibernateDaoSupport implements ICommonDao<T> {
public void save(T entity) {
this.getHibernateTemplate().save(entity);
}
@Resource(name="sessionFactory")
public final void setSessionFactoryDi(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
}
dao创建对象接口
package cn.itcast.elec.dao;
import cn.itcast.elec.domain.ElecText;
public interface IElecTextDao extends ICommonDao<ElecText>{
public final static String service_name = "cn.itcast.elec.dao.impl.ElecTextDaoImpl";
}
dao对象实现类
package cn.itcast.elec.dao.impl;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.itcast.elec.dao.ICommonDao;
public class CommonDaoImpl<T> extends HibernateDaoSupport implements ICommonDao<T> {
public void save(T entity) {
this.getHibernateTemplate().save(entity);
}
@Resource(name="sessionFactory")
public final void setSessionFactoryDi(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}
}
配置beans.xml文件(Spring配置文件)
<!-- 1:配置注解的自动扫描的范围 -->
<context:component-scan base-package="cn.itcast.elec"></context:component-scan>
<!-- 2:配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=UTF-8"/>
<property name="user" value="root" />
<property name="password" value="c" />
<!--连接池中保留的最小连接数。-->
<property name="minPoolSize" value="5" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="30" />
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="10"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60"/>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
<property name="maxStatements" value="0" />
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60" />
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
<property name="acquireRetryAttempts" value="30" />
<!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
获取连接失败后该数据源将申明已断开并永久关闭。Default: false-->
<property name="breakAfterAcquireFailure" value="true" />
</bean>
<!-- 3:创建sessionFactory,这是spring整合hibernate的入口 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation">
<value>
classpath:hibernate.cfg.xml
</value>
</property>
</bean>
<!-- 4:创建事务管理器 -->
<bean id="txManage" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 5:以注解的形式管理事务 -->
<tx:annotation-driven transaction-manager="txManage"/>
</beans>
4.Service层
package cn.itcast.elec.service;
import cn.itcast.elec.domain.ElecText;
public interface IElecTextService {
public final static String Service_name="cn.itcast.elec.service.impl.ElecTextServiceImpl";
public void saveElecText(ElecText elecText);
}
实现类
package cn.itcast.elec.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.elec.dao.IElecTextDao;
import cn.itcast.elec.domain.ElecText;
import cn.itcast.elec.service.IElecTextService;
@Transactional(readOnly=true)
@Service(IElecTextService.Service_name)
public class ElecTextServiceImpl implements IElecTextService {
@Resource(name=IElecTextDao.service_name)
private IElecTextDao elecTextDao;
@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
public void saveElecText(ElecText elecText) {
elecTextDao.save(elecText);
}
}
5.建立Action层
创建
package cn.itcast.elec.web.action;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.elec.container.ServiceProvider;
import cn.itcast.elec.domain.ElecText;
import cn.itcast.elec.service.IElecTextService;
import cn.itcast.elec.util.StringHelper;
import cn.itcast.elec.web.form.ElecTextForm;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class ElecTextAction extends BaseAction implements ModelDriven<ElecTextForm> {
private IElecTextService elecTextService = (IElecTextService)ServiceProvider.getService(IElecTextService.Service_name);
private ElecTextForm elecTextForm = new ElecTextForm();
public ElecTextForm getModel() {
// TODO Auto-generated method stub
return elecTextForm;
}
public String save(){
ElecText elecText = new ElecText();
elecText.setTextName(elecTextForm.getTextName());
elecText.setTextDate(StringHelper.stringConvertDate(elecTextForm.getTextDate()));
elecText.setTextRemark(elecTextForm.getTextRemark());
elecTextService.saveElecText(elecText);
return "save";
}
}
创建form的值对象
package cn.itcast.elec.web.form; import java.util.Date; public class ElecTextForm implements java.io.Serializable{ private String textId; private String textName; private String textDate; private String textRemark; public String getTextId() { return textId; } public void setTextId(String textId) { this.textId = textId; } public String getTextName() { return textName; } public void setTextName(String textName) { this.textName = textName; } public String getTextDate() { return textDate; } public void setTextDate(String textDate) { this.textDate = textDate; } public String getTextRemark() { return textRemark; } public void setTextRemark(String textRemark) { this.textRemark = textRemark; } }
建立baseActionpackage cn.itcast.elec.web.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class BaseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{ protected HttpServletRequest request = null; @SuppressWarnings("unused") protected HttpServletResponse response = null; public void setServletResponse(HttpServletResponse response) { this.response=response; } public void setServletRequest(HttpServletRequest arg0) { this.request = request; } }
建立struts文件web.xml<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.action.extension" value="do"></constant> <constant name="struts.devMode" value="true"></constant> <constant name="struts.ui.theme" value="simple"></constant> <package name="system" namespace="/system" extends="struts-default"> <action name="elecTextAction_*" class="cn.itcast.elec.web.action.ElecTextAction" method="{1}"> <result name="save"> /system/textAdd.jsp </result> </action> </package> </struts>
<?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"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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></web-app>