三层架构的电力系统
2015-03-20 11:51
281 查看
1.
**使用hibernate,写持久化类,映射文件,hibernate配置文件**
<?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
package="com.itcast.elec.domain">
<class
name="ElecText" table="elec_text"> <id name="textID" column="textID" type="string"> <generator class="uuid"/>
</id> <property name="textName" column="textName" type="string"/> <property name="textDate" column="textDate" type="java.util.Date"/>
<property name="textRemark" column="textRemark" type="string"/>
</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">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property
name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property
name="connection.url">jdbc:mysql://localhost:3306/itcasttest?useUnicode=true&charEncoding=utf8</property>
<property
name="connection.username">root</property>
<property
name="connection.password">527425</property>
<!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property>
<!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property><!--
<property name="hibernate.connection.autocommit">true</property>-->//hibernate事务自动提交
<mapping
resource="com/itcast/elec/domain/ElecText.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.
**数据访问层**
由于有一些数据库操作是类似的,因此创建一个公共的接口ICommonDao public interface ICommonDao<T>
{ void save(T entity);}其他的类可以继承这个公共接口如IElecTextDao专门负责电力设备管理的接口public
interface IElecTextDao extends ICommonDao<ElecText>{
public static final String SERVICE_NAME = "com.itcast.elec.dao.Impl.IElecTextDaoImpl";//定义实现这个接口的类为常量,便于寻找}**实现类**public
class ICommonImpl<T>
extends HibernateDaoSupport implements ICommonDao<T>
{
@Resource(name="hibernateTemplate")
public final void setHibernateTemplateDi(HibernateTemplate hibernateTemplate){ this.setHibernateTemplate(hibernateTemplate); }
public void save(T entity) { this.getHibernateTemplate().save(entity); }}@Repository(IElecTextDao.SERVICE_NAME)//数据层用@Repository,控制层用@resource,业务层用@service来标注需要被spring配置文件实例化的类public
class IElecTextDaoImpl extends ICommonImpl<ElecText>
implements IElecTextDao {}
3. **引入spring**
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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 ">
<context:annotation-config/>
<!--
1、配置容器资源扫描的包 ,如@resource,@Repository--> <context:component-scan
base-package="com.itcast.elec"></context:component-scan>
<!--
2、? -->
<!--
3、创建sessionfactory,由spring提供 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property
name="configLocation"> <value> classpath:hibernate.cfg.xml </value>
</property> </bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></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>**业务层**跟数据层一样,也是先写一个接口,再写一个实现类public
interface IElecTextService {
public static final String SERVICE_NAME= "com.itcast.elec.service.Impl.ElecTextServiceImpl";
void saveElecText(ElecText elecText);}package
com.itcast.elec.service.Impl;import
javax.annotation.Resource;import
org.springframework.stereotype.Repository;import
org.springframework.stereotype.Service;import
org.springframework.transaction.annotation.Isolation;import
org.springframework.transaction.annotation.Propagation;import
org.springframework.transaction.annotation.Transactional;import
com.itcast.elec.dao.IElecTextDao;import
com.itcast.elec.domain.ElecText;import
com.itcast.elec.service.IElecTextService;@Repository(IElecTextService.SERVICE_NAME)@Transactional(readOnly=true)public
class ElecTextServiceImpl implements IElecTextService {
@Resource(name=IElecTextDao.SERVICE_NAME)
private IElecTextDao iElecTextDao;
//Propagation.REQUIRED,没有事务创建事务,有就在事务中运行,Isolation.DEFAULT与数据库的事务隔离机制一样
@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)//Isolation.DEFAULT使用和数据库一样的事务隔离机制,Propagation.REQUIRED表示有事务就是用当下的事务,没有就创建新事务
public void saveElecText(ElecText elecText) {
iElecTextDao.save(elecText);
}}
4. **控制层,引入Struts2**先建立一个基础action,实现actionsupport,并实现ServletRequestAware,ServletResponseAware,获得request和responsepublic
class BaseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware {
protected ServletRequest request;
protected ServletResponse response;
public void setServletRequest(HttpServletRequest req) {
this.request = req;
}
public void setServletResponse(HttpServletResponse res) { this.response = res; }}@SuppressWarnings("serial")@Controller("elecTextAction")@Scope(value="prototype")//spring
默认scope 是单例模式即@Scope(value="singleton"),这样只会创建一个Action对象每次访问都是同一个Action对象,数据不安全,struts2
是要求 每次次访问 都对应不同的Action,scope="prototype" 可以保证 当有请求的时候 都创建一个Action对象public
class ElecTextAction extends BaseAction implements ModelDriven<ElecText>{
@Resource(name=IElecTextService.SERVICE_NAME)
private IElecTextService iElecTextService;
ElecText elecText = new ElecText();
public ElecText getModel() {
return elecText;
}
public String save(){
iElecTextService.saveElecText(elecText);
return SUCCESS;
}}struts2的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.devMode"
value="true"></constant>
<constant
name="struts.ui.theme"
value="simple"></constant>
<constant
name="struts.action.extension"
value="do"></constant>//只接受后缀名为.do的action
<package
name="system"
namespace="/system"
extends="struts-default">
<action
name="elecTextAction_*"
class="elecTextAction"
method="{1}">
<result
name="success">/system/textAdd.jsp</result>
</action>
</package>
</struts>xml配置,设置struts2的过滤器,web服务器启动需要加载applicationContext.xml,添加spring监听器<?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">
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener></web-app>
5. **junit测试**省略
**使用hibernate,写持久化类,映射文件,hibernate配置文件**
<?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
package="com.itcast.elec.domain">
<class
name="ElecText" table="elec_text"> <id name="textID" column="textID" type="string"> <generator class="uuid"/>
</id> <property name="textName" column="textName" type="string"/> <property name="textDate" column="textDate" type="java.util.Date"/>
<property name="textRemark" column="textRemark" type="string"/>
</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">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property
name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property
name="connection.url">jdbc:mysql://localhost:3306/itcasttest?useUnicode=true&charEncoding=utf8</property>
<property
name="connection.username">root</property>
<property
name="connection.password">527425</property>
<!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property>
<!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property><!--
<property name="hibernate.connection.autocommit">true</property>-->//hibernate事务自动提交
<mapping
resource="com/itcast/elec/domain/ElecText.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.
**数据访问层**
由于有一些数据库操作是类似的,因此创建一个公共的接口ICommonDao public interface ICommonDao<T>
{ void save(T entity);}其他的类可以继承这个公共接口如IElecTextDao专门负责电力设备管理的接口public
interface IElecTextDao extends ICommonDao<ElecText>{
public static final String SERVICE_NAME = "com.itcast.elec.dao.Impl.IElecTextDaoImpl";//定义实现这个接口的类为常量,便于寻找}**实现类**public
class ICommonImpl<T>
extends HibernateDaoSupport implements ICommonDao<T>
{
@Resource(name="hibernateTemplate")
public final void setHibernateTemplateDi(HibernateTemplate hibernateTemplate){ this.setHibernateTemplate(hibernateTemplate); }
public void save(T entity) { this.getHibernateTemplate().save(entity); }}@Repository(IElecTextDao.SERVICE_NAME)//数据层用@Repository,控制层用@resource,业务层用@service来标注需要被spring配置文件实例化的类public
class IElecTextDaoImpl extends ICommonImpl<ElecText>
implements IElecTextDao {}
3. **引入spring**
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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 ">
<context:annotation-config/>
<!--
1、配置容器资源扫描的包 ,如@resource,@Repository--> <context:component-scan
base-package="com.itcast.elec"></context:component-scan>
<!--
2、? -->
<!--
3、创建sessionfactory,由spring提供 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property
name="configLocation"> <value> classpath:hibernate.cfg.xml </value>
</property> </bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></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>**业务层**跟数据层一样,也是先写一个接口,再写一个实现类public
interface IElecTextService {
public static final String SERVICE_NAME= "com.itcast.elec.service.Impl.ElecTextServiceImpl";
void saveElecText(ElecText elecText);}package
com.itcast.elec.service.Impl;import
javax.annotation.Resource;import
org.springframework.stereotype.Repository;import
org.springframework.stereotype.Service;import
org.springframework.transaction.annotation.Isolation;import
org.springframework.transaction.annotation.Propagation;import
org.springframework.transaction.annotation.Transactional;import
com.itcast.elec.dao.IElecTextDao;import
com.itcast.elec.domain.ElecText;import
com.itcast.elec.service.IElecTextService;@Repository(IElecTextService.SERVICE_NAME)@Transactional(readOnly=true)public
class ElecTextServiceImpl implements IElecTextService {
@Resource(name=IElecTextDao.SERVICE_NAME)
private IElecTextDao iElecTextDao;
//Propagation.REQUIRED,没有事务创建事务,有就在事务中运行,Isolation.DEFAULT与数据库的事务隔离机制一样
@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)//Isolation.DEFAULT使用和数据库一样的事务隔离机制,Propagation.REQUIRED表示有事务就是用当下的事务,没有就创建新事务
public void saveElecText(ElecText elecText) {
iElecTextDao.save(elecText);
}}
4. **控制层,引入Struts2**先建立一个基础action,实现actionsupport,并实现ServletRequestAware,ServletResponseAware,获得request和responsepublic
class BaseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware {
protected ServletRequest request;
protected ServletResponse response;
public void setServletRequest(HttpServletRequest req) {
this.request = req;
}
public void setServletResponse(HttpServletResponse res) { this.response = res; }}@SuppressWarnings("serial")@Controller("elecTextAction")@Scope(value="prototype")//spring
默认scope 是单例模式即@Scope(value="singleton"),这样只会创建一个Action对象每次访问都是同一个Action对象,数据不安全,struts2
是要求 每次次访问 都对应不同的Action,scope="prototype" 可以保证 当有请求的时候 都创建一个Action对象public
class ElecTextAction extends BaseAction implements ModelDriven<ElecText>{
@Resource(name=IElecTextService.SERVICE_NAME)
private IElecTextService iElecTextService;
ElecText elecText = new ElecText();
public ElecText getModel() {
return elecText;
}
public String save(){
iElecTextService.saveElecText(elecText);
return SUCCESS;
}}struts2的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.devMode"
value="true"></constant>
<constant
name="struts.ui.theme"
value="simple"></constant>
<constant
name="struts.action.extension"
value="do"></constant>//只接受后缀名为.do的action
<package
name="system"
namespace="/system"
extends="struts-default">
<action
name="elecTextAction_*"
class="elecTextAction"
method="{1}">
<result
name="success">/system/textAdd.jsp</result>
</action>
</package>
</struts>xml配置,设置struts2的过滤器,web服务器启动需要加载applicationContext.xml,添加spring监听器<?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">
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener></web-app>
5. **junit测试**省略
相关文章推荐
- 三层架构的电力系统
- 三层架构之数据库访问层完全篇(C#)
- 三层架构实例
- 谈谈三层架构中Model的作用
- 三层架构中 工厂模式和单例模式的使用
- NET三层经典架构PetShop3.0分析之数据访问层
- C#三层架构详细解剖
- 从三层架构到MVC
- 三层架构的搭建
- 【ASP.NET开发】ASP.NET(MVC)三层架构知识的学习总结 分类: ASP.NET 2012-09-27 17:09 1985人阅读 评论(1) 收藏
- [javaEE] 三层架构案例-用户模块(二)
- .NET三层经典架构PetShop3.0分析之数据访问层
- ssm项目加载顺序 及 三层架构
- 三层架构的项目中通常异常的处理方式
- [导入]三层架构理论的理解
- 插件式开发怎样和三层架构结合起来?
- 【转载】我的架构经验小结(三)-- 深入三层架构
- ASP.NET MVC 三层架构知识的学习总结
- 三层架构中使用实体类填充泛型集合代替DataTable解决方案(ASP.NET+C#)
- c# 三层架构