Hibernate3.5.4---java application的xml和annotation环境搭建(hibernate.cfg.xml配置文件说明,映射文件Student.hbm.xml说明)
来源:互联网 发布:沙驰皮鞋 知乎 编辑:程序博客网 时间:2024/05/19 01:11
原文转自:http://blog.csdn.net/centre10/article/details/6050466
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
<?xmlversion='1.0'encoding='utf-8'?>
<!DOCTYPEhibernate-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 -->
<propertyname="connection.driver_class">com.mysql.jdbc.Driver</property>
<propertyname="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate</property>
<propertyname="connection.username">root</property>
<propertyname="connection.password">centre</property>
<!-- JDBC connection pool (use the built-in) -->
<propertyname="connection.pool_size">1</property>
<!-- SQL dialect 方言,官方的HSQL语言,将转换成各种数据库的sql-->
<propertyname="dialect">org.hibernate.dialect.MySQLMyISAMDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<propertyname="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语句 -->
<propertyname="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为与同一包中
<?xmlversion="1.0"?>
<!DOCTYPEhibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- hibernate映射,包名对应java的包名 -->
<hibernate-mappingpackage="com.xie.hibernate.modal">
<!-- class用于指定modal名和表名对应,name属性指modal名,table指与modal对应的表名,
当2者相同时,可以不指定table -->
<classname="Student"table="Student">
<!-- id指定modal的属性对应与表的主键 -->
<idname="id"column="id"></id>
<!-- property指定modal属性与表的字段的对应,如果名字相同,column可以不写 -->
<propertyname="name"column="name"></property>
<propertyname="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说明
- Hibernate配置文件hibernate.cfg.xml、类与表间的关系映射文件Student.hbm.xml的书写和属性注解
- hibernate hbm.xml配置文件说明
- Hibernate的映射文件(hbm.xml)属性说明
- Hibernate的映射文件(hbm.xml)属性说明
- Hibernate的映射文件(hbm.xml)属性说明
- Hibernate框架中hibernate.properties属性文件,hibernate.cfg.xml配置文件,以及Users.hbm.xml映射文件的配置
- hibernate.cfg.xml文件说明
- Hibernate学习-08:配置-核心配置文件hibernate.cfg.xml、映射文件*.hbm.xml
- hibernate.cfg.xml配置文件和hbm.xml配置文件
- hibernate 配置文件之hibernate.cfg.xml说明
- hibernate 配置文件之hibernate.cfg.xml说明
- Hibernate.cfg.xml注解和*.hbm.xml配置文件
- hibernate.cfg.xml注解和*.hbm.xml配置文件详解
- Hibernate-配置文件-映射文件.hbm.xml
- Hibernate3.5.4---web application的xml和annotation环境搭建,整合struts2.1.8
- 使用hibernate连接mysql数据库正向工程hibernate.cfg.xml文件和xxx.hbm.xml配置文件
- 学习 Box2D 个人笔记(一)b2World
- 【shell】sed的-e选项
- Cocos2d-x 2.0变速动画深入分析
- VC2010工程中加入SplashScreen及原理释解
- 第3课 使用( Get...Set ) 函数
- Hibernate3.5.4---java application的xml和annotation环境搭建(hibernate.cfg.xml配置文件说明,映射文件Student.hbm.xml说明)
- php使用cookie
- java reflect 反射
- 习题9.38
- AC算法
- Oracle导入导出命令
- 苹果 助手
- test
- MySQL 深入剖析 char varchar 类型,有了VARCHAR,为什么还要有CHAR?