项目学习——电力系统底层架构ssh
电力系统底层架构
1、建立web工程
   创建数据库
   导入向对应的jar包
2、 持久层:
    (1)在cn.itcast.elec.domain中创建持久化类ElecText
	    @SuppressWarnings("serial")
		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;
			}
		}
	(2)在cn.itcast.elec.domain中创建ElecText.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="cn.itcast.elec.domain.ElecText" table="Elec_Text">
				<id name="textID" type="string">
					<column name="textID" sql-type="VARCHAR(50)"></column>
					<generator class="uuid"></generator>
				</id>
				<property name="textName" type="string">
					<column name="textName" sql-type="VARCHAR(50)"></column>
				</property>
				<property name="textDate" type="date">
					<column name="textDate" length="50"></column>
				</property>
				<property name="textRemark" type="string">
					<column name="textRemark" sql-type="VARCHAR(500)"></column>
				</property>
			</class>
		</hibernate-mapping>
   (3)在src的目录下,创建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">com.mysql.jdbc.Driver</property>
				<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/itcast0906elec?useUnicode=true&characterEncoding=utf8</property>
				<property name="hibernate.connection.username">root</property>
				<property name="hibernate.connection.password">root</property>
				<!-- 使事务自动提交 -->
				<!--<property name="hibernate.connection.autocommit">true</property>-->
				<!-- 配置 -->
				<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
				<property name="hibernate.hbm2ddl.auto">update</property>
				<property name="hibernate.show_sql">true</property>
<!-- 添加映射的hbm.xml -->
				<mapping resource="cn/itcast/elec/domain/ElecText.hbm.xml"/>
			</session-factory>
		</hibernate-configuration>
	(4)测试在junit包下TestHibernate
	    public class TestHibernate {
			@Test
			public void testSave(){
				Configuration configuration = new Configuration();
				//加载类路径的hibernate.cfg.xml
				configuration.configure();
				//调用sessionFactory
				SessionFactory sf = configuration.buildSessionFactory();
				//打开session
				Session s = sf.openSession();
				//开启事务
				Transaction tr = s.beginTransaction();
				//保存ElecText
				ElecText elecText = new ElecText();
				elecText.setTextName("测试Hibernate名称");
				elecText.setTextDate(new Date());
				elecText.setTextRemark("测试Hibernate备注");
				s.save(elecText);
				//事务提交
				tr.commit();
				//关闭session
				s.close();
}
		}
3、DAO层
   (1)在cn.itcast.elec.dao中创建对应的业务接口	IElecTextDao
public interface IElecTextDao extends ICommonDao<ElecText> {
			public static final String SERVICE_NAME = "cn.itcast.elec.dao.impl.ElecTextDaoImpl";
		}	  
   (2)在cn.itcast.elec.dao.impl中创建对应业务接口的实现类ElecTextDaoImpl
        @Repository(IElecTextDao.SERVICE_NAME)
		public class ElecTextDaoImpl extends CommonDaoImpl<ElecText> implements IElecTextDao {
}
    (3)在cn.itcast.elec.dao中创建对应的公用接口	ICommonDao
        public interface ICommonDao<T> {
			void save(T entity);
		}
    (4)在cn.itcast.elec.dao.impl中创建对应公用接口的实现类CommonDaoImpl,并注入sessionFactory给hibernateTemplate
	    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>
			 */
			@Resource(name="sessionFactory")
			public final void setSessionFactoryDi(SessionFactory sessionFactory) {
				super.setSessionFactory(sessionFactory);
			}
public void save(T entity) {
				this.getHibernateTemplate().save(entity);
			}
		}
	(5)在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="cn.itcast.elec"></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>
    (6)测试在junit包下
       public class TestDao {
			@Test
			public void testSaveElecText(){
				//加载类路径下的beans.xml
				ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
				//获取spring容器中的bean的id节点
				IElecTextDao elecTextDao = (IElecTextDao) ac.getBean(IElecTextDao.SERVICE_NAME);
				//保存
				ElecText elecText = new ElecText();
				elecText.setTextName("测试DAO名称");
				elecText.setTextDate(new Date());
				elecText.setTextRemark("测试DAO备注");
				elecTextDao.save(elecText);
			}
		}
4、业务层
   (1)在cn.itcast.elec.service中创建接口	IElecTextService
		public interface IElecTextService {
			public static final String SERVICE_NAME = "cn.itcast.elec.service.impl.ElecTextServiceImpl";
			void saveElecText(ElecText elecText);
		}
    (2)在cn.itcast.elec.service.impl中创建实现类ElecTextServiceImpl,在业务层要写入事务控制
        @Service(IElecTextService.SERVICE_NAME)
		@Transactional(readOnly=true)
		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);
			}
}
	(3)测试,在junit包下TextService测试
	    public class TestService {
			@Test
			public void testSaveElecText(){
				//加载类路径下的beans.xml
				ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
				//获取spring容器中的bean的id节点
				IElecTextService elecTextService = (IElecTextService) ac.getBean(IElecTextService.SERVICE_NAME);
				//保存
				ElecText elecText = new ElecText();
				elecText.setTextName("测试Service名称");
				elecText.setTextDate(new Date());
				elecText.setTextRemark("测试Service备注");
				elecTextService.saveElecText(elecText);
			}
		}
5、控制层
     (1)在cn.itcast.elec.web.action中创建ElecTextAction,使用模型驱动
	        @Controller("elecTextAction")
			@Scope(value="prototype")
			@SuppressWarnings("serial")
			public class ElecTextAction extends BaseAction implements ModelDriven<ElecText> {
private ElecText elecText = new ElecText();
@Resource(name=IElecTextService.SERVICE_NAME)
				private IElecTextService elecTextService;
public ElecText getModel() {
					return elecText;
				}
public String save(){
					elecTextService.saveElecText(elecText);
					System.out.println(request.getParameter("textDate"));
					return "success";
				}
			}
	 (2)在cn.itcast.elec.web.action中创建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;
				}
}
	 (3)在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"></constant>
				<!-- 设置开发模式,开发时输出更多的错误信息 -->
				<constant name="struts.devMode" value="true"></constant>
				<!-- 修改ui主题为简单主题 -->
				<constant name="struts.ui.theme" value="simple"></constant>
				<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>
	  (4)在web.xml中配置:添加:
	      <!-- 使用struts整合spring,web服务器启动时,需要加载beans.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>
	 (5)导入对应css,script,images,jsp页面
	 (6)整体测试
项目学习——电力系统底层架构ssh的更多相关文章
- 中小研发团队架构实践之生产环境诊断工具WinDbg  三分钟学会.NET微服务之Polly  使用.Net Core+IView+Vue集成上传图片功能  Fiddler原理~知多少?  ABP框架(asp.net core 2.X+Vue)模板项目学习之路(一)        C#程序中设置全局代理(Global Proxy)  WCF 4.0 使用说明   如何在IIS上发布,并能正常访问
		中小研发团队架构实践之生产环境诊断工具WinDbg 生产环境偶尔会出现一些异常问题,WinDbg或GDB是解决此类问题的利器.调试工具WinDbg如同医生的听诊器,是系统生病时做问题诊断的逆向分析工具 ... 
- ML平台_小米深度学习平台的架构与实践
		(转载:http://www.36dsj.com/archives/85383)机器学习与人工智能,相信大家已经耳熟能详,随着大规模标记数据的积累.神经网络算法的成熟以及高性能通用GPU的推广,深度学 ... 
- (转)MyBatis框架的学习(二)——MyBatis架构与入门
		http://blog.csdn.net/yerenyuan_pku/article/details/71699515 MyBatis框架的架构 MyBatis框架的架构如下图: 下面作简要概述: S ... 
- 洞悉MySQL底层架构:游走在缓冲与磁盘之间
		提起MySQL,其实网上已经有一大把教程了,为什么我还要写这篇文章呢,大概是因为网上很多网站都是比较零散,而且描述不够直观,不能系统对MySQL相关知识有一个系统的学习,导致不能形成知识体系.为此我撰 ... 
- SSH项目整合教学Eclipse搭建SSH(Struts2+Spring3+Hibernate3)
		这篇博文的目的 尝试搭建一个完整的SSH框架项目. 给以后的自己,也给别人一个参考. 读博文前应该注意: 本文提纲:本文通过一个用户注册的实例讲解SSH的整合.创建Struts项目,整合Hiberna ... 
- JavaWeb学习之三层架构实例(三)
		引言 通过上一篇博客JavaWeb学习之三层架构实例(二)我们基本上已经实现了对学生信息列表的增删改查操作(UI除外),但是不难看出,代码冗余度太高了,尤其是StudentDao这个类,其中的增删改查 ... 
- Linux设备驱动模型底层架构及组织方式
		1.什么是设备驱动模型? 设备驱动模型,说实话这个概念真的不好解释,他是一个比较抽象的概念,我在网上也是没有找到关于设备驱动模型的一个定义,那么今天就我所学.所了解 到的,我对设备驱动模型的一个理解: ... 
- Docker 基础底层架构浅谈
		docker学习过程中,免不了需要学习下docker的底层技术,今天我们来记录下docker的底层架构吧! 从上图我们可以看到,docker依赖于linux内核的三个基本技术:namespaces.C ... 
- Halo 开源项目学习(七):缓存机制
		基本介绍 我们知道,频繁操作数据库会降低服务器的系统性能,因此通常需要将频繁访问.更新的数据存入到缓存.Halo 项目也引入了缓存机制,且设置了多种实现方式,如自定义缓存.Redis.LevelDB ... 
随机推荐
- 安装和使用memcached
			引用:http://www.czhphp.com/archives/252 如何将 memcached 融入到您的环境中? 在开始安装和使用 using memcached 之前,我们需要了解如何将 ... 
- 【转】JVM 堆内存设置原理
			堆内存设置 原理 JVM堆内存分为2块:Permanent Space 和 Heap Space. Permanent 即 持久代(Permanent Generation),主要存放的是Java类定 ... 
- hive中的一些参数
			动态分区 设置如下参数开启动态分区:hive.exec.dynamic.partition=true默认值:false描述:是否允许动态分区hive.exec.dynamic.partition.mo ... 
- WebService 基础使用&cxf第三方Service使用
			1.通过Jax-ws自己发布一个webservice 解析:用webservice发布HelloWorld JAX-WS本质就是通过Socket来实现的.2.WSDL文档描述如何直接变成java代码 ... 
- H5-考试判断题
			1.所有的元素设置了浮动后都可以设置宽高. 2.行元素都不能设置宽高跟上下边距 3.所有的css样式优先级中“!important”优先级最高(及其不推荐使用) 4.改变元素的transition值, ... 
- Python:time模块&序列化&生成随机数&反射
			time模块:>>> import time >>> time.time <built-in function time> >>> t ... 
- java多线程详解(6)-线程间的通信wait及notify方法
			Java多线程间的通信 本文提纲 一. 线程的几种状态 二. 线程间的相互作用 三.实例代码分析 一. 线程的几种状态 线程有四种状态,任何一个线程肯定处于这四种状态中的一种:(1). 产生(New) ... 
- Activity使用startActivityForResult时出现onActivityResult()不执行的问题
			通过使用 startActivityForResult() 和 onActivityResult() 方法可以在Activity之间传递或接收参数.但有时候我们会遭遇onActivityResult( ... 
- Python网络爬虫Scrapy框架研究
			看到一个爬虫比较完整的教程.保留一下. https://github.com/yidao620c/core-scrapy 
- python 异常含义
			异常 描述 NameError 尝试访问一个没有申明的变量 ZeroDivisionError 除数为0 SyntaxError 语法错误 IndexError 索引超出序列范围 KeyError 请 ... 
