Hibernate3.5.4---java application的xml和annotation环境搭建(hibernate.cfg.xml配置文件说明,映射文件Student.hbm.xml说明
2010-12-02 16:31
761 查看
Hibernate负责持久层,将modal持久化到数据库。由于jdbc使用的sql语言不是面向对象,感觉不舒服,写起来也比较复杂。Hibernate对其进行了封装,让我们可以按照面向对象的思维方式编程。Hibernate采用了2中方式将modal持久化到DB。
环境搭建:到官网下载http://www.hibernate.org/ hibernate-distribution-3.5.4-Final-dist.zip
在myeclipse中添加userlibary:Window->Preferences->Java->Bulid Path->User Libraries->New,加入相关包。Hibernate3.jar(hibernate核心包,包含了以前版本所需要的hibernate-commons-annotations.jar,hibernate-annotations.jar),antlr-2.7.6.jar,commos-collections-3.1.jar,dom4j-1.6.1.jar,javassist-3.9.0.GA.jar,jta-1.1.jar,slf4j-api-1.5.8.jar(这是一个接口包,其实现为:slf4j-nop-1.5.8.jar)slf4j-nop-1.5.8.jar(其下载地址:http://www.slf4j.org/download.html),hibernate-jpa-api-1.0.0.Filnal.jar。
3.5版本以前的hibernate还需要hibernate-commons-annotations.jar,hibernate-annotations.jar(反射时需要的包),ejb3-persistence.jar
Xml实现的持久化:
Hibernate.cfg.xml
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">centre</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect 方言,官方的HSQL语言,将转换成各种数据库的sql-->
<property name="dialect">org.hibernate.dialect.MySQLMyISAMDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache优化hibernate -->
<!--
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
-->
<!-- Echo all executed SQL to stdout打印生成的sql语句 -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup ddl数据定义语句-->
<!--
<property name="hbm2ddl.auto">update</property>
-->
<mapping resource="com/xie/hibernate/modal/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
映射xml:
Student.hbm.xml前面的Student与modal名完全相同,同时该文件应该与modal为与同一包中
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- hibernate映射,包名对应java的包名 -->
<hibernate-mapping package="com.xie.hibernate.modal">
<!-- class用于指定modal名和表名对应,name属性指modal名,table指与modal对应的表名,
当2者相同时,可以不指定table -->
<class name="Student" table="Student">
<!-- id指定modal的属性对应与表的主键 -->
<id name="id" column="id"></id>
<!-- property指定modal属性与表的字段的对应,如果名字相同,column可以不写 -->
<property name="name" column="name"></property>
<property name="age" column="age"></property>
</class>
</hibernate-mapping>
Mysql创建数据库和表的语句:
create database hibernate;
create table student(
id int primary key auto_increment,
name varchar(12) not null,
age int);
annotation持久化(目前公司常用):
这种方式不用配置*.hbm.xml文件,在modal中的设置如下:
package com.xie.hibernate.modal;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.annotations.Table;
//指明这是一个实体
@Entity
@Table(appliesTo = "Teacher")
public class Teacher {
private int id;
private String title;
private String name;
private int age;
//指明主键,放于get方法前
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在hibernate.cfg.xml指定如下:
<mapping class="com.xie.hibernate.modal.Teacher"/>
Msyql建表语句为:
create table teacher(
id int primary key auto_increment,
title varchar(12) not null,
name varchar(12) not null,
age int);
说明:hibernate能帮助我们建表。
主函数类如下:
package com.xie.hibernate.main;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
//import com.xie.hibernate.modal.Student;
import com.xie.hibernate.modal.Teacher;
public class TestHibernate {
public static void main(String[] args) {
/* Student s=new Student();
s.setId(1);
s.setName("xie");
s.setAge(25);
Configuration cfg=new Configuration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close(); */
Teacher t=new Teacher();
t.setId(3);
t.setName("liu");
t.setAge(52);
t.setTitle("中级");
Configuration cfg=new AnnotationConfiguration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
}
}
环境搭建:到官网下载http://www.hibernate.org/ hibernate-distribution-3.5.4-Final-dist.zip
在myeclipse中添加userlibary:Window->Preferences->Java->Bulid Path->User Libraries->New,加入相关包。Hibernate3.jar(hibernate核心包,包含了以前版本所需要的hibernate-commons-annotations.jar,hibernate-annotations.jar),antlr-2.7.6.jar,commos-collections-3.1.jar,dom4j-1.6.1.jar,javassist-3.9.0.GA.jar,jta-1.1.jar,slf4j-api-1.5.8.jar(这是一个接口包,其实现为:slf4j-nop-1.5.8.jar)slf4j-nop-1.5.8.jar(其下载地址:http://www.slf4j.org/download.html),hibernate-jpa-api-1.0.0.Filnal.jar。
3.5版本以前的hibernate还需要hibernate-commons-annotations.jar,hibernate-annotations.jar(反射时需要的包),ejb3-persistence.jar
Xml实现的持久化:
Hibernate.cfg.xml
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">centre</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect 方言,官方的HSQL语言,将转换成各种数据库的sql-->
<property name="dialect">org.hibernate.dialect.MySQLMyISAMDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache优化hibernate -->
<!--
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
-->
<!-- Echo all executed SQL to stdout打印生成的sql语句 -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup ddl数据定义语句-->
<!--
<property name="hbm2ddl.auto">update</property>
-->
<mapping resource="com/xie/hibernate/modal/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
映射xml:
Student.hbm.xml前面的Student与modal名完全相同,同时该文件应该与modal为与同一包中
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- hibernate映射,包名对应java的包名 -->
<hibernate-mapping package="com.xie.hibernate.modal">
<!-- class用于指定modal名和表名对应,name属性指modal名,table指与modal对应的表名,
当2者相同时,可以不指定table -->
<class name="Student" table="Student">
<!-- id指定modal的属性对应与表的主键 -->
<id name="id" column="id"></id>
<!-- property指定modal属性与表的字段的对应,如果名字相同,column可以不写 -->
<property name="name" column="name"></property>
<property name="age" column="age"></property>
</class>
</hibernate-mapping>
Mysql创建数据库和表的语句:
create database hibernate;
create table student(
id int primary key auto_increment,
name varchar(12) not null,
age int);
annotation持久化(目前公司常用):
这种方式不用配置*.hbm.xml文件,在modal中的设置如下:
package com.xie.hibernate.modal;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.annotations.Table;
//指明这是一个实体
@Entity
@Table(appliesTo = "Teacher")
public class Teacher {
private int id;
private String title;
private String name;
private int age;
//指明主键,放于get方法前
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在hibernate.cfg.xml指定如下:
<mapping class="com.xie.hibernate.modal.Teacher"/>
Msyql建表语句为:
create table teacher(
id int primary key auto_increment,
title varchar(12) not null,
name varchar(12) not null,
age int);
说明:hibernate能帮助我们建表。
主函数类如下:
package com.xie.hibernate.main;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
//import com.xie.hibernate.modal.Student;
import com.xie.hibernate.modal.Teacher;
public class TestHibernate {
public static void main(String[] args) {
/* Student s=new Student();
s.setId(1);
s.setName("xie");
s.setAge(25);
Configuration cfg=new Configuration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close(); */
Teacher t=new Teacher();
t.setId(3);
t.setName("liu");
t.setAge(52);
t.setTitle("中级");
Configuration cfg=new AnnotationConfiguration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
}
}
相关文章推荐
- Hibernate3.5.4---java application的xml和annotation环境搭建(hibernate.cfg.xml配置文件说明,映射文件Student.hbm.xml说明
- Hibernate3.5.4---java application的xml和annotation环境搭建(hibernate.cfg.xml配置文件说明,映射文件Student.hbm.xml说明)
- [原创]java WEB学习笔记77:Hibernate学习之路---Hibernate 版本 helloword 与 解析,.环境搭建,hibernate.cfg.xml文件及参数说明,持久化类,对象-关系映射文件.hbm.xml,Hibernate API (Configuration 类,SessionFactory 接口,Session 接口,Transaction(事务))
- Hibernate3.5.4---web application的xml和annotation环境搭建,整合struts2.1.8
- Hibernate框架 主配置文件 Hibernate.cfg.xml 映射配置 说明
- SSH整合,applicationContext.xml中配置hibernate映射文件问题
- 使用Xdoclet生成Hibernate配置文件(hibernate.cfg.xml)和Hibernate映射文件(*.hbm.xml)
- Java---Hibernate.cfg.xml文件配置
- Hibernate框架中hibernate.properties属性文件,hibernate.cfg.xml配置文件,以及Users.hbm.xml映射文件的配置
- 关于hibernate.cfg.xml配置文件的几点说明
- Hibernate学习-08:配置-核心配置文件hibernate.cfg.xml、映射文件*.hbm.xml
- Hibernate中持久化注解的java文件在applicationContext.xml中的配置
- Hibernate配置文件:hibernate.cfg.xml常用属性说明
- hibernate hbm.xml配置文件说明
- Hibernate核心配置文件cfg参数说明
- java中读取配置文件的方法fileSystemXmlapplicationContext与classPathXmlApplicationContext
- Nginx 实战(一) 集群环境搭建 Nginx配置文件详细说明
- 为什么hibernate在不联网的时候解析不了配置文件?(hibernate.cfg.xml)
- Gradle搭建Struts 2.5.14.1基于xml配置文件的开发环境
- Hibernate配置文件——hibernate.cfg.xml