Hibernate版本3.1
1.在MyEclipse内创建数据库资源管理器,找到“Perspective”
2.点击“Open Perspective”
3.找到“MyEclipse Database Explorer”
4.新建数据库驱动连接
5.
Driver template : 根据自己要使用的数据库选择;我这里用的是MySQL数据库,因此以MySQL为例 。
Driver name : 名字自己随便写。
Connection URL : 连接数据库的URL,不同数据库连接方式不同。
User name : 数据库用户名。
Password : 数据库密码。
Driver JARs : 添加连接数据库驱动jar包。
保存密码,点击“next”。
URL:jdbc:oracle:thin:@localhost:1521:ORCL另外jdbc:mysql://localhost:3308/fresh
点击Test Driver,选择Save password
6.添加已创建要连接的数据库
7.在已建项目中安装hibernate的组件
8.选择Use JDBC Driver
9.选择Driver name :
9.1:不建立SessionFactory
9.2:在hibernate.cfg.xml的Properties内增加show_sql值为true
10.建立po包并建立类,来对应数据库的表,增加set和get函数,空的构建函数,和加上所有列为参数,增加this.x=x
11.在po包内建立customer.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>
<class name="po.Customer" table="T_CUSTOMER" schema="SCOTT"> 类和表对应
<id name="属性" column="列名"> 主键
<generator class="assigned"/> 主键的生成策略,由用户赋值
</id>
<property name="属性" column="列名"/> 各个属性和各个列
</class>
</hibernate-mapping>
12.拖动customer.hbm.xml进入hibernate.cfg.xml的Mappings
========================================================================
插入:
//读取配置文件
Configuration conf = new Configuration().configure();
//生成SessionFactory来管理Session
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
//增加事务提交
Transaction tran = session.beginTransaction();
//设置customer
Customer cus = new Customer("","","",40);
//或者session.saveOrUpdate()保存或者修改;
session.save(cus);
tran.commit();
========================================================================
查询一条语句:
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Customer cus = new Customer();
//或者session.get()保存或者修改;
session.load(cus,"111");//111代表主键内容
=========================================================================
修改:
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Customer cus = new Customer();
//或者session.get()保存或者修改;
session.load(cus,"111");//111代表主键内容
//增加事务提交
Transaction tran = session.beginTransaction();
cus.set(cus.get+1000);
session.update(cus);
tran.commit();
=========================================================================
删除:
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Customer cus = new Customer();
//或者session.get()保存或者修改;
session.load(cus,"111");//111代表主键内容
//增加事务提交
Transaction tran = session.beginTransaction();
session.delete(cus);
tran.commit();
1.在MyEclipse内创建数据库资源管理器,找到“Perspective”
2.点击“Open Perspective”
3.找到“MyEclipse Database Explorer”
4.新建数据库驱动连接
5.
Driver template : 根据自己要使用的数据库选择;我这里用的是MySQL数据库,因此以MySQL为例 。
Driver name : 名字自己随便写。
Connection URL : 连接数据库的URL,不同数据库连接方式不同。
User name : 数据库用户名。
Password : 数据库密码。
Driver JARs : 添加连接数据库驱动jar包。
保存密码,点击“next”。
URL:jdbc:oracle:thin:@localhost:1521:ORCL另外jdbc:mysql://localhost:3308/fresh
点击Test Driver,选择Save password
6.添加已创建要连接的数据库
7.在已建项目中安装hibernate的组件
8.选择Use JDBC Driver
9.选择Driver name :
9.1:不建立SessionFactory
9.2:在hibernate.cfg.xml的Properties内增加show_sql值为true
10.建立po包并建立类,来对应数据库的表,增加set和get函数,空的构建函数,和加上所有列为参数,增加this.x=x
11.在po包内建立customer.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>
<class name="po.Customer" table="T_CUSTOMER" schema="SCOTT"> 类和表对应
<id name="属性" column="列名"> 主键
<generator class="assigned"/> 主键的生成策略,由用户赋值
</id>
<property name="属性" column="列名"/> 各个属性和各个列
</class>
</hibernate-mapping>
12.拖动customer.hbm.xml进入hibernate.cfg.xml的Mappings
========================================================================
插入:
//读取配置文件
Configuration conf = new Configuration().configure();
//生成SessionFactory来管理Session
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
//增加事务提交
Transaction tran = session.beginTransaction();
//设置customer
Customer cus = new Customer("","","",40);
//或者session.saveOrUpdate()保存或者修改;
session.save(cus);
tran.commit();
========================================================================
查询一条语句:
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Customer cus = new Customer();
//或者session.get()保存或者修改;
session.load(cus,"111");//111代表主键内容
=========================================================================
修改:
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Customer cus = new Customer();
//或者session.get()保存或者修改;
session.load(cus,"111");//111代表主键内容
//增加事务提交
Transaction tran = session.beginTransaction();
cus.set(cus.get+1000);
session.update(cus);
tran.commit();
=========================================================================
删除:
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.openSession();
Customer cus = new Customer();
//或者session.get()保存或者修改;
session.load(cus,"111");//111代表主键内容
//增加事务提交
Transaction tran = session.beginTransaction();
session.delete(cus);
tran.commit();