搭建hibernate
来源:互联网 发布:如何做免费网络推广 编辑:程序博客网 时间:2024/06/04 19:05
一.引入hibernate所需jar包
二.配置hibernate.cfg.xml
1.从hibernate.3中找到这个dtd,复制头文件,并配置数据库连接信息
<?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.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myelec?useUnicode=true&characterEncoding=utf8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">67903319</property>
<!-- 其他配置 -->
<!-- 映射文件 -->
<mapping resource="com/dream/domain/ElecText.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.创建实体类
3.创建映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.domain.ElecText" table="electest">
<id name="textId" type="string" column="textId">
<generator class="uuid"></generator>
</id>
<property name="textDate" type="date" column="textDate"></property>
<property name="textRemark" type="string" column="textRemark"></property>
<property name="textName" type="string" column="textName"></property>
</class>
</hibernate-mapping>
四.测试
package com.junit;
import java.util.Date;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.Test;
import com.domain.ElecText;
public class HibernateTest {
@Test
public void save(){
Configuration cf=new Configuration();
cf.configure();
SessionFactory sf=cf.buildSessionFactory();
Session session=sf.openSession();
Transaction tr=session.beginTransaction();
ElecText elecText=new ElecText();
elecText.setTextDate(new Date());
elecText.setTextName("测试7.10");
elecText.setTextRemark("测试备注7.10");
session.save(elecText);
tr.commit();
session.close();
}
}
- Hibernate搭建
- hibernate搭建
- 搭建hibernate
- Hibernate 搭建
- 【hibernate框架】搭建hibernate环境
- hibernate--2.搭建hibernate环境
- hibernate学习 hibernate项目搭建
- 搭建Hibernate应用
- 搭建Hibernate应用
- spring+hibernate+operamask搭建
- 第一章 Hibernate环境搭建
- hibernate框架的搭建
- 搭建hibernate开发环境
- Hibernate环境搭建
- Hibernate环境的搭建
- Hibernate框架的搭建
- Hibernate环境搭建
- hibernate的搭建
- OC 宏入门
- 利用元数据编写查询的通用方法
- Java中自写ArrayList容器
- 关于数组方面的大小计算
- 《virtualbox完全学习手册》之 虚拟机也玩多屏显示
- 搭建hibernate
- POJ 3311 Hie with the Pie (状态压缩DP ,TSP问题)
- TensorFlow进阶:CNN对CIFAR10图像分类
- Just a Hook HDU
- reveal 4 for xcode 8 2017 reveal配置xcode详解
- 让网页中footer总在底部的有效办法
- 华为OJ——按字节截取字符串
- 列表以及自定义列表的使用
- JAVA基础—递归的使用