本示例是普通的java工程,IDE:Eclipse 3.4, JDK 1.5
所需jar包:
antlr-2.7.6.jar
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
log4j-1.2.11.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ehcache-1.2.jar
jta.jar
hibernate3.jar
jtds-1.2.2.jar(数据库驱动)
目录结构:
一。users类
public class Users {
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;
}
}
与数据库里 Users 表映射的xml文件
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Users的 id 设置成自增长
二。DAO接口
public interface IUserDao {
public void insert(User user);
public User find(Integer id);
}
三。DAO接口的实现类
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class UserDaoImpl implements IUserDao {
private SessionFactory sessionFactory;
public UserDaoImpl(){
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public UserDaoImpl(SessionFactory sessionFactory){
this.setSessionFactory(sessionFactory);
}
public User find(Integer id) {
Session session = sessionFactory.openSession();
User user = (User)session.get(User.class, id);
Hibernate.initialize(user);
session.close();
return user;
}
public void insert(User user) {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
session.close();
}
}
四。hibernate配置文件
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
注意:
上面红色部分路径的写法是 斜杠(/) 而不是 点(.) 写点会有异常
连接数据库的jar驱动是 jtds ,数据库是Sql Server 2005
微软官方的驱动写法:com.microsoft.sqlserver.jdbc.SQLServerDriver
连接字符串: jdbc:sqlserver://localhost:1433; DatabaseName=sample
不同数据库的 dialect(方言) :
MS Sql Server : org.hibernate.dialect.SQLServerDialect (本示例用Sql Server)
My Sql : org.hibernate.dialect.MySQLDialect
Oracle : org.hibernate.dialect.Oracle9Dialect 或者 org.hibernate.dialect.OracleDialect
五。 log4j.properties(该步骤可省略)
Hibernate默认会在控制台产生许多日志,为了方便观看放置一个配置文件设置只有显示异常信息时才显示
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
六。执行程序HibernateDemo类
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateDemo {
public static void main(String[] arg){
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
IUserDao userDao = new UserDaoImpl(sessionFactory);
User user = new User();
user.setName("jerry");
user.setAge(new Integer(10));
userDao.insert(user);
System.out.println(userDao.find(new Integer(18)).getName());
}
}
执行结果:
Hibernate: insert into Users (name, age) values (?, ?)
Hibernate: select user0_.id as id0_0_, user0_.name as name0_0_, user0_.age as age0_0_ from Users user0_ where user0_.id=?
tom33