测试Hibernate连接数据库的测试代码
Spring中整合了Hibernate,数据库的连接配置也可以写在applicationContext.xml文件中,下面是hibernate.cfg.xml文件内容,数据库用的是mysql
- <?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.username">root</property>
- <property name="hibernate.connection.password">123456</property>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</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>
- <mapping resource="cn/itcast/elec/domain/ElecText.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
<?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.username">root</property><property name="hibernate.connection.password">123456</property><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</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><mapping resource="cn/itcast/elec/domain/ElecText.hbm.xml"/></session-factory>
</hibernate-configuration>
下面是测试数据库连接是否成功的测试代码HibernateTest.java
- package junit;
- import java.util.Date;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import org.junit.Test;
- import cn.itcast.elec.domain.ElecText;
- public class TestHibernate {
- @Test
- public void testElecText(){
- Configuration config = new Configuration();
- config.configure("/hibernate.cfg.xml");
- //创建sessionFactory对象
- SessionFactory sf = config.buildSessionFactory();
- //打开session,操作数据库
- Session session = sf.openSession();
- //开启事务
- Transaction tr = session.beginTransaction();
- //实例化ElecText对象,添加数据,执行保存操作
- ElecText elecText = new ElecText();
- elecText.setTextName("测试Hibernate_liu");
- elecText.setTextDate(new Date());
- elecText.setTextRemark("测试Hibernate_liu");
- //保存
- session.save(elecText);
- //提交事务
- tr.commit();
- session.close();
- // if(session!=null){
- // System.out.println("Contection Success!");
- // session.close();
- // }else{
- // System.out.println("Contection Failed!");
- // }
- }
- }
package junit;import java.util.Date;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;import cn.itcast.elec.domain.ElecText;public class TestHibernate {@Testpublic void testElecText(){Configuration config = new Configuration();config.configure("/hibernate.cfg.xml");//创建sessionFactory对象SessionFactory sf = config.buildSessionFactory();//打开session,操作数据库Session session = sf.openSession();//开启事务Transaction tr = session.beginTransaction();//实例化ElecText对象,添加数据,执行保存操作ElecText elecText = new ElecText();elecText.setTextName("测试Hibernate_liu");elecText.setTextDate(new Date());elecText.setTextRemark("测试Hibernate_liu");//保存session.save(elecText);//提交事务tr.commit();session.close();
// if(session!=null){
// System.out.println("Contection Success!");
// session.close();
// }else{
// System.out.println("Contection Failed!");
// }}
}
该测试代码是通过对一个表添加了一条数据来测试数据库是否连接成功,而注释部分可以直接测试数据库是否连接成功,
测试Hibernate连接数据库的测试代码相关推荐
- 配置hibernate_测试Hibernate的最低配置
配置hibernate 介绍 在上一篇文章中,我宣布了我打算创建个人Hibernate课程的意图. 首先要做的是最小的测试配置. 这些示例与Hibernate 4有关. 您只需要Hibernate 在 ...
- 测试Hibernate的最低配置
介绍 在上一篇文章中,我宣布了我打算创建个人Hibernate课程的意图. 首先要做的是最小的测试配置. 这些示例与Hibernate 4有关. 您只需要休眠 在实际的生产环境中,您不会单独使用Hib ...
- 独家 | 如何创建用于离线估算业务指标的测试集?(附代码链接)
作者:AARSHAY JAIN 翻译:张若楠 校对:张玲 本文约6500字,建议阅读10+分钟 本文将从原理及应用两方面出发,介绍如何采用日志数据对新模型进行上线测试前的初步筛选评估. 标签:机器学习 ...
- 【趣图】测试刚写完的代码...
1.被老板委派接手刚刚离职同事的项目... 2.当他们要求我测试所有应用功能时 3.准备下班的时候,测试又提bug过來了- 4.使用新框架却忘记阅读文档 5.测试实习生的代码 6.网络延迟的危害.. ...
- eclipse中测试Hibernate异常报 ORA-00926: 缺失 VALUES 关键字
eclipse中测试Hibernate异常报 ORA-00926: 缺失 VALUES 关键字 参考文章: (1)eclipse中测试Hibernate异常报 ORA-00926: 缺失 VALUES ...
- 【Web自动化测试——代码篇十二】自动化测试模型——数据驱动测试和关键字驱动测试...
自动化测试模型可以看作自动化测试框架与工具设计的思想.随着自动化测试技术的发展,演化为以下几种模型: 线性测试 模块化驱动侧式 数据驱动测试 关键字驱动测试 数据驱动测试 前一篇所讲的模块化驱动测试能 ...
- spock测试_使用Spock测试您的代码
spock测试 Spock是针对Java和Groovy应用程序的测试和规范框架. Spock是: 极富表现力 简化测试的"给定/何时/然后" 语法 与大多数IDE和CI服务器兼容. ...
- (保守群组测试 非保守群组测试 二次重复测试 自适应二次重复测试)四种群体测试的C++代码
目录 原理 保守组检测 非保守组检测 二次重复测试 自适应二次重复测试 四种测试方法的核心代码 保守群组测试 非保守群组测试 二次重复测试与自适应二次重复测试 测试代码 参考文献 原理 假设该病在人群 ...
- python编写代码_用 Python 编写干净、可测试、高质量的代码
用 Python 编写干净.可测试.高质量的代码 Noah Gift 2010 年 12 月 20 日发布 简介 编写软件是人所承担的最复杂的任务之一.AWK 编程语言和 "K and R ...
最新文章
- 细说 Java 主流日志工具库
- 深度学习 vs SLAM
- java 内存溢出和内存泄漏_JAVA中的内存溢出和内存泄漏有很大的区别
- spark2.1:rdd.combineByKeyWithClassTag的用法示例
- leetcode35. 搜索插入位置(二分搜索)
- Bitmap添加文字水印
- gt designer2不能初始化字体管理器_Windows Terminal 1.1预览版发布:新增字体粗细、随开机启动等功能...
- [BZOJ3456]城市规划
- 大朗机器人餐厅在哪里_获得海内外一致好评,送餐机器人为中国餐饮打开新世界...
- 利用工具,促进有效沟通
- 如何调整硬盘分区大小
- seo站长,必备批量工具
- 使用js脚本实现微信定时发送信息
- Azure NSG Flow Log 引发的自嗨 -- 日志查询分析
- 点云特征提取--vfh
- GIS实验之制作核密度分析图
- 摄影测量外方位元素代码
- mysql2004报错_mysqlbinlog备份时候报错Sanity check failed
- LeetCode 374题
- opc服务器上层传输协议,OPC服务器 (OPC Server) 之间数据传递的桥梁 — OPC Data Manager (ODM)...