测试Hibernate连接数据库的测试代码
来源:互联网 发布:ios看本子的软件 编辑:程序博客网 时间:2024/05/03 09:26
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>
- 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!");
- // }
- }
- }
0 0
- 测试Hibernate连接数据库的测试代码
- 测试Hibernate连接数据库的测试代码
- 测试数据库的连接代码,备用
- oracle连接数据库测试代码
- oracle连接数据库测试代码
- oracle连接数据库测试代码
- VC连接数据库测试代码
- VC连接数据库测试代码
- oracle连接数据库测试代码
- 测试数据库的连接
- 测试Hibernate连接数据库是否成功
- java 连接MySQL数据库的步骤与代码测试连通
- 测试Oracle数据库的连接
- 测试与数据库的连接
- JSP测试代码连接ACCESS数据库
- Java连接SQL 数据库测试代码
- Java MySQL Oracle数据库 连接测试代码
- java 连接SqlServer数据库测试代码
- 希尔排序
- 关于解决 Navicat for mysql 远程连接 mySql数据库10061错误问题
- iOS开发之OCR光学识别储蓄卡以及信用卡
- 处理模型的闪面
- 内存分配的形式
- 测试Hibernate连接数据库的测试代码
- PHP 随机用户名账号的生成
- mysql命令行备份数据库
- 网易yum源
- Handoff使用指南 - 理论篇
- EXP-00056:ORA-06550:PLS-00201: identifier ‘SYS.DBMS_EXPORT_EXTENSION’ must be declared
- C#控制台基础 用户不断输入直到输入exit 退出循环或是退出控制台
- Java回顾之集合
- 手把手教你使用UICollectionView写公司的项目