1 。建立数据库
drop database if exists SAMPLEDB;
create database hello;
use hello;
create table CUSTOMERS (
ID int not null primary key,
NAME varchar(15) not null,
PASSWORD varchar(8) not null,
);
2。在eclipse 中新建工程hbtest
3。在src中新建package hbm
4。新建pojo类Customer
package hbm;
public class Customer {
private int id;
private String name;
private String password;
/**
* @return Returns the id.
*/
public int getId() {
return id;
}
/**
* @param id
* The id to set.
*/
public void setId(int id) {
this.id = id;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name
* The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Returns the password.
*/
public String getPassword() {
return password;
}
/**
* @param password
* The password to set.
*/
public void setPassword(String password) {
this.password = password;
}
//constructor
public Customer() {
}
}
5。使用myeclipse插件建立hibernate.cfg.xml 位于src目录下
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/hello</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="myeclipse.connection.profile">hbmysql</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="hbm/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
6。在hbm package内新建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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-mapping>
<class name="hbm.Customer" table="CUSTOMERS">
<id name="id" column="ID" type="int">
<generator class="increment"/>
</id>
<property name="name" column="NAME" type="string" not-null="true"/>
<property name="password" column="PASSWORD" type="string" not-null="true"/>
</class>
</hibernate-mapping>
7。使用hibernate操作数据库
package hbm;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.Iterator;
import java.util.List;
public class Hbmain {
public static SessionFactory sessionFactory;//数据存储源
static {
try {
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 将一个customer对象存入database
* @Customer customer Object
*/
public void saveCustomer(Customer ct) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(ct);
tx.commit();
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
/*
* 查找所有的customer object
*/
public void findAll() {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
List customers = session.createQuery(
"from Customer as c order by c.name asc").list();
Iterator it = customers.iterator();
System.out.println("append:"+customers.size());
while(it.hasNext())
{
Customer c = (Customer)it.next();
System.out.println("ID:" + c.getId());
System.out.println("Name:" + c.getName());
System.out.println("Pass:" + c.getPassword());
}
tx.commit();
} catch (Exception e) {
tx.rollback();
} finally {
session.close();
}
}
/*
* 修改customer Name
* @name
*/
public void loadUpdate(int customer_id, String name) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Customer c = (Customer) session.load(Customer.class, customer_id);
c.setName(name);
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
}
/*
* 测试以上的方法
* save() find() update()
*/
public void test()
{
Customer ct = new Customer();
//ct.setId(5);
ct.setName("buaa");
ct.setPassword("5768");
this.saveCustomer(ct);
this.findAll();
this.loadUpdate(ct.getId(),"phop");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Hbmain hb = new Hbmain();
hb.test();
sessionFactory.close();
}
}