版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sky_20131213/article/details/77126964
本系统框架采用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();
}
}
测试结果如下:
此博客为学习时所写,内容有摘抄,若有侵权请联系本人删除。