Hibernate入门学习笔记1
来源:互联网 发布:淘宝各行业排名 编辑:程序博客网 时间:2024/05/16 12:42
开发步骤:
1.持久化类,即对象->数据库(getxxx和setxxx方法)
2.持久化类和数据库的映射
3.应用的开发
(xml配置)jar包:
cglib-2.1.3.jar,commons-collections-2.1.1.jar,commons-logging-api.jar,antlr-2.7.6.jar,asm.jar,asm-attrs.jar,c3p0-0.9.1.jar,
mysql-connector-java-3.1.7-bin.jar,jta.jar,hibernate3.jar,dom4j-1.6.1.jar
hibernate lib中需要加入annotation的jar包
hibernate annotation.jar ,ejb3 persistence.jar ,hibernate common annotations.jar
范例:
1.建立数据库:
create table student
(
id int(5) primary key,
name varchar(20),
age int(2)
);
添加jar包:
cglib-2.1.3.jar,commons-collections-2.1.1.jar,commons-logging-api.jar,antlr-2.7.6.jar,asm.jar,asm-attrs.jar,c3p0-0.9.1.jar,
mysql-connector-java-3.1.7-bin.jar,jta.jar,hibernate3.jar,dom4j-1.6.1.jar
2.持久化类:Student类
package com.test.model;
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;
}
}
3.映射文件:Student.hbm.xml跟Student类放在一个包中
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--HBM根元素,指定POJO类所在的包-->
<hibernate-mapping
package="com.test.model">
<!--指定Student类和student表的映射-->
<class name="com.test.model.Student" table="student" lazy="true">
<!--表中主键-->
<id name="id" >
<!--主键的生成方式:assigned:表示由用户自行管理-->
<generator class="assigned"/>
</id>
<!--表中其他字段的映射-->
<property name="name" column="name"/>
<property name="age" column="age"/>
</class>
</hibernate-mapping>
4.hibernate.cfg.xml:对Hibernate环境配置的,包括使用的数据库或数据源。
范例:连接Mysql数据库
<?xml version="1.0" encoding="GBK"?>
<!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="show_sql">
true
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.driver_class">
org.gjt.mm.mysql.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost/test
</property>
<property name="connection.username">
root
</property>
<property name="connection.password">
system
</property>
<mapping resource="com/test/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5.建立应用文件
StudentTest类
package yzh;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.test.model.Student;
public class StudentTest {
public static void main(String args[]){
Student s=new Student();
s.setId(1);
s.setName("heizi");
s.setAge(22);
//导入hibernate.cfg.xml配置
//开始实例化session对象
Configuration cfg=new Configuration();
//创建Hibernate会话工厂
SessionFactory sf=cfg.configure().buildSessionFactory();
//创建hibernate会话,从SessionFactory中取出一个session
Session sess=sf.openSession();
//开始一个事务
sess.beginTransaction();
//将Student对象保存进数据库
sess.save(s);
//提交会话中的数据
sess.flush();
//提交事务
sess.getTransaction().commit();
//关闭会话
sess.close();
sf.close();
}
}
- Hibernate入门学习笔记1
- 【Hibernate学习笔记】hibernate入门
- hibernate学习笔记--入门
- Hibernate入门学习笔记
- Hibernate入门学习笔记
- Hibernate入门学习笔记
- Hibernate 学习笔记(1):入门
- hibernate学习笔记 第 1 章 Hibernate入门
- Hibernate入门学习笔记2
- Hibernate框架入门(学习笔记)
- hibernate入门笔记(1)
- Hibernate 学习入门1
- hibernate学习笔记-1入门初体验对象持久化
- Hibernate学习笔记(1)-----------jar包引入及入门
- Hibernate学习之入门案例笔记
- Hibernate入门笔记 1, 2
- Hibernate学习笔记 第一章 Hibernate 入门概念与配置
- hibernate学习笔记1
- Div高度控制问题
- SQL 游标创建和应用
- CSDN是个好网站
- 重载有什么意义呢?
- SQL 分页存储过程
- Hibernate入门学习笔记1
- youdao 搜索框
- 11.28
- 第一次注册
- 如何获得十亿分之一秒的时钟精度
- 着重C++编程规则,为找工作准备
- 编程吗?小case。。。
- 刚刚开通 很高兴呢
- 奇怪的const