1.User.java文件
package com.po;
public class User{
private Integer id;
private String name;
private Integer age;
public Integer getId(){
return id;
}
public void setId(Integer id){
this.id=id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public Integer getAge(){
return age;
}
public void setAge(Integer age){
this.age=age;
}
}
2.sql角本
create table user(
id int not null primary key,
name varchar(50) not null default '',
age int
)
3.User.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="com.po.User" table="user" schema="dbo" catalog="pubs">
<id name="id" column="id">
<generator class="native">
</id>
<property name="name" column="name">
<property name="age" column="age">
</class>
</hibernate-mapping>
4.IUserDAO.java文件
package com.po;
public interface IUserDAO
{
public void insert(User user);
public User find(Integer id);
}
5.UserDAO.java文件
package com.po;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibername.Transaction;
public class UserDAO implements IUserDAO
{
private SessionFactory sessionFactory;
public UserDAO(){
}
public UserDAO(SessionFactory sessionFactory){
this.setSessionFactory(sessionFactory);
}
public void setSessionFactory(SessionFactory sessionfactory){
this.sessionFactory=sessionFactory;
}
public void insert(User user){
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
session.close();
}
public User find(Integer id){
Session session = sessionFactory.openSession();
User user = (User)session.get(User.class,id);
Hibernate.initialize(user);
session.close();
return user;
}
}
6.hibernate.cfg.xml文件
<?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">
<hibernate-configuration>
<session-factory>
<property name="show_sql">
true
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost/demo
</property>
<property name="connection.username">
op
</property>
<property name="connection.password">
11111
</property>
<mapping resource="com/po/User.hbm.xml">
</session-factory>
</hibernate-configuration>
7.HibernateDemo.java文件
package com.po;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateDemo
{
public static void main(String[] args){
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
IUserDAO userDAO = new UserDAO(sessionFactory);
User user = new User();
user.setName("koko");
user.setAge(new Integer(30));
userDAo.insert(user);
user = userDAO.find(new Integer(1));
System.out.println("name:"+user.getName());
}
}