hibernate配置案例
来源:互联网 发布:网站源码如何使用 编辑:程序博客网 时间:2024/04/29 16:26
搭建一个Hibernate环境,开发步骤:
1. 下载源码
版本:hibernate-distribution-3.6.0.Final
下载地址 http://download.csdn.net/detail/jlh912008548/9192169
2. 引入jar文件
hibernate3.jar核心 + required必须引入的(6个) + jpa 目录 + 数据库驱动包 共9个核心jar 包
3. 写对象以及对象的映射
Employee.java 对象
Employee.hbm.xml 对象的映射 (映射文件)
4. src/hibernate.cfg.xml 主配置文件
-à 数据库连接配置
-à 加载所用的映射(*.hbm.xml)
5. App.java 测试
Employee.java 对象public class Employee {private int empId;private String empName;private Date workDate;}
Employee.hbm.xml 对象的映射
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.itcast.a_hello"><class name="Employee" table="employee"><!-- 主键 ,映射--><id name="empId" column="id"><generator class="native"/></id><!-- 非主键,映射 --><property name="empName" column="empName"></property><property name="workDate" column="workDate"></property></class></hibernate-mapping>
hibernate.cfg.xml 主配置文件
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/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:///hib_demo</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">root</property><property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property><property name="hibernate.show_sql">true</property><!-- 加载所有映射 --><mapping resource="cn/itcast/a_hello/Employee.hbm.xml"/></session-factory></hibernate-configuration>
App.java 测试类
public class App {@Testpublic void testHello() throws Exception {// 对象Employee emp = new Employee();emp.setEmpName("班长");emp.setWorkDate(new Date());// 获取加载配置文件的管理类对象Configuration config = new Configuration();config.configure(); // 默认加载src/hibenrate.cfg.xml文件// 创建session的工厂对象SessionFactory sf = config.buildSessionFactory();//如果是使用注解的话 请使用// SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();// 创建session (代表一个会话,与数据库连接的会话)Session session = sf.openSession();// 开启事务Transaction tx = session.beginTransaction();//保存-数据库session.save(emp);// 提交事务tx.commit();// 关闭session.close();sf.close();}}
1 0
- hibernate配置案例
- Hibernate配置入门案例
- Hibernate入门配置案例
- Hibernate中配置ehcache案例
- hibernate一对多关系配置案例
- Hibernate简单配置及应用案例
- Hibernate 5.2.12 二级缓存配置及案例
- 框架漫谈之hibernate(二)hibernate配置以及入门案例
- Hibernate中一对多的注解和配置案例
- hibernate系列七:多对一关联配置及案例
- hibernate系列八:一对多关联配置及案例
- hibernate系列十一:一对一关联配置及案例
- Hibernate------>第一个hibernate案例
- Hibernate单独使用案例
- hibernate查询案例
- HIbernate+struts2分页案例
- hibernate 小案例
- Hibernate之入门案例
- 表达式必须包含类类型
- 从1到15有几种方法
- Java
- 第二章软件测试基础
- POI操作Word
- hibernate配置案例
- lua学习(一)
- 9月8
- 不爽 主要溢出 递归变化
- OGG
- 第三章基于生命周期的软件测试
- java内存区域各部分内容及内存溢出介绍
- 群硕笔试题
- 继承和类的高级概念