【Hibernate】入门搭建之XML方式
来源:互联网 发布:淘宝学生男鞋店铺排行 编辑:程序博客网 时间:2024/06/05 18:29
本程序使用Hibernate3.6.10版本
Hibernate 核心包下载地址:http://sourceforge.net/projects/hibernate/files/hibernate3/3.6.10.Final/
Hibernate 对应Annotation下载地址:http://sourceforge.net/projects/hibernate/files/hibernate-annotations/3.4.0.GA/hibernate-annotations-3.4.0.GA.zip/download
一、XML方式所需要的jar包有:
hibernate-distribution-3.6.10.Final/hibernate3.jar
hibernate-distribution-3.6.10.Final/lib/required/所有
hibernate-distribution-3.6.10.Final/lib/jpa/hibernate-jpa-2.0-api-1.0.1.Final.jar
slf4j-1.6.1/slf4j-nop-1.6.1.jar (注:此处的slf4j的版本取决于hibernate-distribution-3.6.10.Final/lib/required 下 slf4j-api.*.*.*.jar的版本号,后期可换成log4j)
二、在项目src目录下创建hibernate.cfg.xml文件(此文件的放置地方可以不在此处,但通常放在这里),源码如下
<?xml version='1.0' encoding='utf-8'?><!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="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- JDBC 连接池 --> <!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect 方言--> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- Enable Hibernate's automatic session context management --> <!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache --> <!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> --> <!-- Echo all executed SQL to stdout 打印执行语句--> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">update</property> --> <!-- xml方式配置 --> <mapping resource="com/ycao/bean/Student.hbm.xml"/> <!-- annotation方式配置 --> <mapping class="com.ycao.bean.Teacher"/> </session-factory></hibernate-configuration>
package com.ycao.bean;public class Student { private int id; private String name; private int age; public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
三、创建实体映射文件Student.hbm.xml
<?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 package="com.ycao.bean"> <class name="Student" table="student"> <id name="id" column="id"> </id> <property name="age"/> <property name="name"/> </class></hibernate-mapping>
四、写测试文件
package com.util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.ycao.bean.Student;public class TestStudent {public static void main(String[] args) {Student s = new Student(); s.setName("zhang san");s.setAge(20); Configuration cfg = new Configuration(); SessionFactory sessionFactory = cfg.configure().buildSessionFactory(); //创建SessionFactory Session session = sessionFactory.openSession(); //创建hibernate session //所有的操作都要放在一个事务里面 session.beginTransaction(); session.save(s); //执行插入操作 session.getTransaction().commit(); //提交事务管理 session.clear(); sessionFactory.close();}}
项目分布截图:
注:HibernateUtil 、TestTeacher.java、Teacher.java 是用来测试Annotation时所用到,与本程序无关,如果想了解Annotation的写方请查看下一节。
- 【Hibernate】入门搭建之XML方式
- 【Hibernate】入门搭建之Annotation
- Mybatis3入门之xml方式
- Spring-hibernate使用全xml配置结合方式搭建开发
- 一、Mybatis3入门之xml方式
- xml解析方式之JAXP解析入门
- Hibernate入门(四)之hibernate中session的创建方式
- hibernate Xml 方式
- hibernate xml配置方式
- hibernate-入门xml配置文件
- Hibernate(5.2.10)快速入门 (一) 初识Hibernate、框架搭建、xml基本配置
- Hibernate映射组件属性xml形式之方式一
- Hibernate映射组件属性xml形式之方式二
- Hibernate入门与环境搭建
- Hibernate环境搭建基础入门
- hibernate入门-------eclipse环境搭建
- Hibernate入门 :不使用hibernate.cfg.xml
- Hibernate之检索方式
- 修改设置,远离DNS劫持---您是不是要访问xxx的解决方法
- s3c6410 uboot代码分析《二》
- 嵌入式Linux开发
- 初识苏嵌
- 从Foursquare看手机端程序设计
- 【Hibernate】入门搭建之XML方式
- 测试流程与测试过程的区别
- JavaScript 脚本收集、乱码
- 弹出可拖动提示层(模式窗口)
- xx is not in the sudoers file
- 每天一道C语言题目7/5
- Nand Flash -详述(1)
- 新手上路,求前辈带路....
- 2012.07.04