电力系统项目学习(2)--框架搭建1-数据持久化
来源:互联网 发布:对云计算的理解ibm 编辑:程序博客网 时间:2024/06/05 19:20
本系统框架采用J2EE三层开发体系架构,使用Stuts+Spring+Hibernate的技术框架。
本系统架构采用:模型–视图–控制器(Model-View-Controller,MVC)体系,共划分为:表现层、控制层、业务逻辑层、持久层,在实现中,用户请求被发送到一个控制器,该控制器决定请求的性质,并且根据请求的类型传送给适合的处理器。每个处理器都和一个特别的模型相关,里面封装有业务逻辑来执行一些特别的业务函数集合,业务逻辑层会调用相应的数据服务层的方法处理数据库操作并返回结果。处理完毕后,结果会发送回处理器,处理器选择合适的视图显示它。
- 新建一个Web项目,项目名称为:study0001elec;
- 导入shh整合jar包至 study001elec/WebRoot/WEB-INF/lib文件夹下;
- 创建持久层,配置hibernate:
-创建实体类ElecTest .java
package com.tts.elec.pojo;import java.util.Date;/** * 实体类 * @author Administrator * */@SuppressWarnings("serial")public class ElecTest 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; }}
-配置映射文件ElecTest .hbm.xml,与ElecTest .java于同一目录下
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="com.tts.elec.pojo.ElecTest" table="Elec_Text"> <id name="textId"> <generator class="uuid"></generator> </id> <property name="textName"></property> <property name="textDate"> <column name="textDate" length="50"></column> </property> <property name="textRemark"></property> </class></hibernate-mapping>
-配置hibernate的配置文件hibernate.cfg.xml,放于类路径下
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/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/mysql?useUnicode=true&characterEncoding=utf8 </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <!--基本配置 --> <property name="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect </property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 事务的自动提交 --> <property name="hibernate.connection.autocommit">true</property> <mapping resource="com/tts/elec/pojo/ElecTest .hbm.xml" /></session-factory></hibernate-configuration>
-写一个测试类TestElec1.java,测试将数据持久化
package test;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionException;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;import org.springframework.orm.hibernate3.HibernateTemplate;import com.tts.elec.pojo.ElecTest;public class TestElec1 { @Test public void TestAdd() { Configuration configuration = new Configuration(); // 加载类路径的hibernate.cfg.xml配置文件 configuration.configure(); // 创建sessionfactory SessionFactory sessionFactory = configuration.buildSessionFactory(); // 打开session Session session = sessionFactory.openSession(); // 开启事务 Transaction transaction = session.beginTransaction(); ElecTest elecTest = new ElecTest(); elecTest.setTextName("name1"); elecTest.setTextDate(new Date()); elecTest.setTextRemark("remark1"); // 数据持久化 session.save(elecTest); // 提交事务 transaction.commit(); // 关闭session session.close(); }}
测试结果如下:
此博客为学习时所写,内容有摘抄,若有侵权请联系本人删除。
阅读全文
0 0
- 电力系统项目学习(2)--框架搭建1-数据持久化
- 电力系统项目学习(1)--项目介绍
- day48_电力系统框架搭建
- 【SSH框架】电力项目持久层搭建
- android开发框架(三)数据持久化(2)-SharedPreferences
- 数据持久化框架(JPA)
- 数据持久层框架Hibernate入门学习
- Android-学习-数据持久化
- Redis学习-数据持久化
- 饿了么开源项目:便捷高效的Android数据持久化存储框架
- android开发框架(二)数据持久化(1)-文件存储
- SSH框架-HIbernate之数据持久化
- iOS开发 - CoreData框架 数据持久化
- iOS开发 - CoreData框架 数据持久化
- iOS项目拆分:数据本地持久化(2)
- iOS项目拆分:数据本地持久化(1)
- 持久数据层框架设计(二)
- 数据持久层框架
- Hadoop体系结构之 HDFS
- 找长字符串中的最长回文
- 正则表达式基本语法
- 算法系列-一致性哈希算法consistent hash
- JavaScript获取URL参数
- 电力系统项目学习(2)--框架搭建1-数据持久化
- Hadoop体系结构之 Mapreduce
- 主机字节序大端和小端
- golang mysql-1(初识)
- Hadoop体系结构之 Yarn
- 函数
- 作用域的作用
- 2017百度之星初赛(A) 1005 今夕何夕(阅读题)
- python : pip install matplotlib