测试Hibernate连接数据库的测试代码

来源:互联网 发布:ios看本子的软件 编辑:程序博客网 时间:2024/05/03 09:26


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

[html] view plain copy print?在CODE上查看代码片派生到我的代码片
  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.            
  8.         <property name="hibernate.connection.username">root</property>  
  9.         <property name="hibernate.connection.password">123456</property>  
  10.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  11.         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>  
  12.         <property name="hibernate.connection.autocommit">true</property>  
  13.           
  14.         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>  
  15.         <property name="hibernate.hbm2ddl.auto">update</property>  
  16.         <property name="hibernate.show_sql">true</property>  
  17.            
  18.         <mapping resource="cn/itcast/elec/domain/ElecText.hbm.xml"/>  
  19.     </session-factory>  
  20. </hibernate-configuration>  
下面是测试数据库连接是否成功的测试代码HibernateTest.java

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. package junit;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.hibernate.Session;  
  6. import org.hibernate.SessionFactory;  
  7. import org.hibernate.Transaction;  
  8. import org.hibernate.cfg.Configuration;  
  9. import org.junit.Test;  
  10.   
  11. import cn.itcast.elec.domain.ElecText;  
  12.   
  13. public class TestHibernate {  
  14.     @Test  
  15.     public void testElecText(){  
  16.         Configuration config = new Configuration();  
  17.         config.configure("/hibernate.cfg.xml");  
  18.         //创建sessionFactory对象  
  19.         SessionFactory sf = config.buildSessionFactory();  
  20.         //打开session,操作数据库  
  21.         Session session = sf.openSession();  
  22.         //开启事务  
  23.         Transaction tr = session.beginTransaction();  
  24.         //实例化ElecText对象,添加数据,执行保存操作  
  25.         ElecText elecText = new ElecText();  
  26.         elecText.setTextName("测试Hibernate_liu");  
  27.         elecText.setTextDate(new Date());  
  28.         elecText.setTextRemark("测试Hibernate_liu");  
  29.         //保存  
  30.         session.save(elecText);  
  31.         //提交事务  
  32.         tr.commit();  
  33.         session.close();  
  34. //      if(session!=null){  
  35. //          System.out.println("Contection Success!");  
  36. //          session.close();  
  37. //      }else{  
  38. //          System.out.println("Contection Failed!");  
  39. //      }  
  40.     }  
  41. }  
该测试代码是通过对一个表添加了一条数据来测试数据库是否连接成功,而注释部分可以直接测试数据库是否连接成功,

0 0
原创粉丝点击