Spring中整合了Hibernate,数据库的连接配置也可以写在applicationContext.xml文件中,下面是hibernate.cfg.xml文件内容,数据库用的是mysql

[html] view plain copy print?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <property name="hibernate.connection.username">root</property>
  8. <property name="hibernate.connection.password">123456</property>
  9. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  10. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
  11. <property name="hibernate.connection.autocommit">true</property>
  12. <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
  13. <property name="hibernate.hbm2ddl.auto">update</property>
  14. <property name="hibernate.show_sql">true</property>
  15. <mapping resource="cn/itcast/elec/domain/ElecText.hbm.xml"/>
  16. </session-factory>
  17. </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

[java] view plain copy print?
  1. package junit;
  2. import java.util.Date;
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.Transaction;
  6. import org.hibernate.cfg.Configuration;
  7. import org.junit.Test;
  8. import cn.itcast.elec.domain.ElecText;
  9. public class TestHibernate {
  10. @Test
  11. public void testElecText(){
  12. Configuration config = new Configuration();
  13. config.configure("/hibernate.cfg.xml");
  14. //创建sessionFactory对象
  15. SessionFactory sf = config.buildSessionFactory();
  16. //打开session,操作数据库
  17. Session session = sf.openSession();
  18. //开启事务
  19. Transaction tr = session.beginTransaction();
  20. //实例化ElecText对象,添加数据,执行保存操作
  21. ElecText elecText = new ElecText();
  22. elecText.setTextName("测试Hibernate_liu");
  23. elecText.setTextDate(new Date());
  24. elecText.setTextRemark("测试Hibernate_liu");
  25. //保存
  26. session.save(elecText);
  27. //提交事务
  28. tr.commit();
  29. session.close();
  30. //      if(session!=null){
  31. //          System.out.println("Contection Success!");
  32. //          session.close();
  33. //      }else{
  34. //          System.out.println("Contection Failed!");
  35. //      }
  36. }
  37. }
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连接数据库的测试代码相关推荐

  1. 配置hibernate_测试Hibernate的最低配置

    配置hibernate 介绍 在上一篇文章中,我宣布了我打算创建个人Hibernate课程的意图. 首先要做的是最小的测试配置. 这些示例与Hibernate 4有关. 您只需要Hibernate 在 ...

  2. 测试Hibernate的最低配置

    介绍 在上一篇文章中,我宣布了我打算创建个人Hibernate课程的意图. 首先要做的是最小的测试配置. 这些示例与Hibernate 4有关. 您只需要休眠 在实际的生产环境中,您不会单独使用Hib ...

  3. 独家 | 如何创建用于离线估算业务指标的测试集?(附代码链接)

    作者:AARSHAY JAIN 翻译:张若楠 校对:张玲 本文约6500字,建议阅读10+分钟 本文将从原理及应用两方面出发,介绍如何采用日志数据对新模型进行上线测试前的初步筛选评估. 标签:机器学习 ...

  4. 【趣图】测试刚写完的代码...

    1.被老板委派接手刚刚离职同事的项目... 2.当他们要求我测试所有应用功能时 3.准备下班的时候,测试又提bug过來了- 4.使用新框架却忘记阅读文档 5.测试实习生的代码 6.网络延迟的危害.. ...

  5. eclipse中测试Hibernate异常报 ORA-00926: 缺失 VALUES 关键字

    eclipse中测试Hibernate异常报 ORA-00926: 缺失 VALUES 关键字 参考文章: (1)eclipse中测试Hibernate异常报 ORA-00926: 缺失 VALUES ...

  6. 【Web自动化测试——代码篇十二】自动化测试模型——数据驱动测试和关键字驱动测试...

    自动化测试模型可以看作自动化测试框架与工具设计的思想.随着自动化测试技术的发展,演化为以下几种模型: 线性测试 模块化驱动侧式 数据驱动测试 关键字驱动测试 数据驱动测试 前一篇所讲的模块化驱动测试能 ...

  7. spock测试_使用Spock测试您的代码

    spock测试 Spock是针对Java和Groovy应用程序的测试和规范框架. Spock是: 极富表现力 简化测试的"给定/何时/然后" 语法 与大多数IDE和CI服务器兼容. ...

  8. (保守群组测试 非保守群组测试 二次重复测试 自适应二次重复测试)四种群体测试的C++代码

    目录 原理 保守组检测 非保守组检测 二次重复测试 自适应二次重复测试 四种测试方法的核心代码 保守群组测试 非保守群组测试 二次重复测试与自适应二次重复测试 测试代码 参考文献 原理 假设该病在人群 ...

  9. python编写代码_用 Python 编写干净、可测试、高质量的代码

    用 Python 编写干净.可测试.高质量的代码 Noah Gift 2010 年 12 月 20 日发布 简介 编写软件是人所承担的最复杂的任务之一.AWK 编程语言和 "K and R ...

最新文章

  1. 细说 Java 主流日志工具库
  2. 深度学习 vs SLAM
  3. java 内存溢出和内存泄漏_JAVA中的内存溢出和内存泄漏有很大的区别
  4. spark2.1:rdd.combineByKeyWithClassTag的用法示例
  5. leetcode35. 搜索插入位置(二分搜索)
  6. Bitmap添加文字水印
  7. gt designer2不能初始化字体管理器_Windows Terminal 1.1预览版发布:新增字体粗细、随开机启动等功能...
  8. [BZOJ3456]城市规划
  9. 大朗机器人餐厅在哪里_获得海内外一致好评,送餐机器人为中国餐饮打开新世界...
  10. 利用工具,促进有效沟通
  11. 如何调整硬盘分区大小
  12. seo站长,必备批量工具
  13. 使用js脚本实现微信定时发送信息
  14. Azure NSG Flow Log 引发的自嗨 -- 日志查询分析
  15. 点云特征提取--vfh
  16. GIS实验之制作核密度分析图
  17. 摄影测量外方位元素代码
  18. mysql2004报错_mysqlbinlog备份时候报错Sanity check failed
  19. LeetCode 374题
  20. opc服务器上层传输协议,OPC服务器 (OPC Server) 之间数据传递的桥梁 — OPC Data Manager (ODM)...

热门文章

  1. 将C++里的Mat矩阵保存并由matlab提取分析
  2. java plc通讯_树莓派+西门子PLC+Aliyun
  3. TDD 与 CI 在 Python 中的实践
  4. 序列化和反序列化二叉搜索树 Serialize and Deserialize BST
  5. 让Python不在mac的dock上显示火箭图标
  6. win7清除系统托盘图标的方法
  7. 敏捷个人纸质书:第一章 源于生活和工作的敏捷个人
  8. Spring Cloud Config 配置中心
  9. js阻止a标签默认事件的几种方法
  10. linux-xargs