1.创建数据库
新建数据库
create database test;
新建表:
主键id(自增长)
字符串格式的name
浮点数格式的price
use test;
CREATE TABLE prodyct_ (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(30) ,
price float ,
PRIMARY KEY (id)
) DEFAULT CHARSET=UTF8;
2.创建java项目,导入项目所需的包
3.创建与表对应的实体类product
package com.ljx;
public class product {
int id;
String name;
float price;
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 float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
4.配置 Product.hbm.xml(连接数据库的一系列配置)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ljx">
<class name="product" table="prodyct_">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name"/>
<property name="price"/>
</class>
</hibernate-mapping>
<id name="id" column="id">
<generator class="native"></generator>
</id>
表示属性id,映射表里的字段id
<generator class="native">意味着id的自增长方式采用数据库的本地方式
<property name="name"/>
只写了属性name,没有通过column=”name” 显式的指定字段,那么对应表里的字段的名字也是name.
5.配置 hibernate.cfg.xml(有关hibernate的一系列配置)
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test2?characterEncoding=UTF-8</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/ljx/product.hbm.xml"/>
</session-factory>
</hibernate-configuration>
1.配置访问数据库要用到的驱动,url,账号密码
2.<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
表示告诉Hibernate底层使用的数据库是mysql
3.<property name="current_session_context_class">thread</property>
是Hibernate事务管理方式,即一个线程对应一个事务
4.<property name="show_sql">true</property>
表示是否在控制台显示执行的mysql语句
5.<property name="hbm2ddl.auto">update</property>
表示是否自动更新数据库的表结构
6.<mapping resource="com/ljx/product.hbm.xml"/>
表示Hibernate会去识别Product这个实体类
6.测试类TestHibernate
package com.ljx;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class testHibernate {
public static void main(String[] args){
Configuration configuration = new Configuration();
configuration.configure("com/ljx/hibernate.cfg.xml");
SessionFactory sf = configuration.buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
product p = new product();
p.setName("xiaoa");
p.setPrice(1000);
s.save(p);
s.getTransaction().commit();
s.close();
sf.close();
}
}
创建一个Product对象,并通过hibernate把这个对象,插入到数据库中
hibernate的基本步骤是:
1.获取hibernate配置文件
2.获取SessionFactory
3.通过SessionFactory获取Session
4.在Session上开启事务
5.通过调用Session的save方法把对象保存到数据库
6.提交事务
7.关闭Session
8. 关闭SessionFactory
7.hibernate实现增删改查
package com.ljx;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class testHibernate2 {
Configuration configuration;
SessionFactory sf;
Session s;
Transaction t;
public testHibernate2(){
configuration = new Configuration();
configuration.configure("com/ljx/hibernate.cfg.xml");
sf= configuration.buildSessionFactory();
}
public void add(product product){
s = sf.openSession();
t = s.beginTransaction();
s.save(product);
t.commit();
s.close();
}
public void delete(int id){
s = sf.openSession();
t = s.beginTransaction();
s.delete(s.get(product.class,id));
// product p = (product)s.get(product.class,new Integer(2));
t.commit();
s.close();
}
public void update(product product){
s = sf.openSession();
t = s.beginTransaction();
s.update(product);
t.commit();
s.close();
}
public product getById(int id){
s = sf.openSession();
t = s.beginTransaction();
product p = (product)s.get(product.class,id);
t.commit();
s.close();
return p;
}
public static void main(String[] args){
testHibernate2 th = new testHibernate2();
//增加
product p_1 = new product();
p_1.setName("add");
p_1.setPrice(10);
th.add(p_1);
//删除
int id_2 = 3;
th.delete(id_2);
//修改
product p_3 = new product();
p_3.setId(1);
p_3.setName("update");
p_3.setPrice(20);
th.update(p_3);
//查找
int id_4 = 1;
product p_4 = th.getById(id_4);
System.out.println(p_4.getId());
System.out.println(p_4.getName());
System.out.println(p_4.getPrice());
}
}
运行前:
运行后:
控制台输出: