此demo项目采用hibernate3.2
new 一个普通的java项目,引入hibernate所需要的jar包:核心jar hibernate3.jar,其他依赖jar /lib 下 ,mysql 驱动jar
hibernate 核心配置文件 :默认就放到classpath根目录下(src根目录)即可
hibernate.cfg.xml
注意:session-factory 元素中不要配置 name 属性值,否则会报如下异常
Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: Java.naming.factory.initial
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory >
<!--
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3316/hibernatedemo?useUnicode=true&characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">winmshl</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping resource="com/jelly/hibernatedemo/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
User.hbm.xml 映射文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="com.jelly.hibernatedemo.entity">
<class name="User" >
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="birthday"/>
</class>
</hibernate-mapping>
实体bean :
package com.jelly.hibernatedemo.entity;
import java.util.Date;
public class User {
private int id;
private String name;
private Date birthday;
public int getId() {
return id;
}
public String getName() {
return name;
}
public Date getBirthday() {
return birthday;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", birthday=" + birthday
+ "]";
}
}
一个简单的demo类(测试类)
package com.jelly.hibernatedemo.main;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.jelly.hibernatedemo.entity.User;
public class MainTest {
public static void main(String[] args) {
try {
Configuration conf=new Configuration();
SessionFactory sessionFactory= conf.configure().buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
User user=new User() ;
user.setName("张三");
user.setBirthday(new Date());
session.save(user);
tx.commit();
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}