所需的jar包(八个基本包)
slf4j-nop-1.5.8.jar
slf4j-api-1.5.8.jar
jta-1.1.jar
javassist-3.9.0.GA.jar
hibernate3.jar
dom4j-1.6.1.jar
commons-collections-3.1.jar
antlr-2.7.6.jar
Annotation包
ejb3-persistence.jar
hibernate-commons-annotations.jar
hibernate-annotations.jar
hibernate.cfg.xml
<property name="hbm2ddl.auto">update</property><!--会自动创建表-->
SLF只是一种接口的定义。 类似于 JDBC JPA
slf4j-nop-1.5.8.jar 为 SLF 的实现jar包
log4j-1.2.15.jar 也为 SLF 的实现jar包
log4j-1.2.15.jar 实现需要引入 SLF 中的转换jar包slf4j-log4j12-1.5.8.jar (适配器模式)
<!--src目录下的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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/icdpub</property>
<property name="connection.username">icdpub</property>
<property name="connection.password">icdpub</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>-->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!--XML 文件映射关系配置-->
<mapping resource="com/meiyoudao/domain/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
//domain类
package com.meiyoudao.domain;
/**
* @author meiyoudao
*
*/
public class Person {
private int id;
private String name;
private String weight;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
<!--和person类同一目录下,名字和映射的domain一致:Person.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.meiyoudao.domain">
<class name="Person">
<id name="id"/>
<property name="name"/>
<property name="weight"/>
</class>
</hibernate-mapping>
//测试类
package com.meiyoudao.test;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import com.meiyoudao.domain.Person;
/**
* @author meiyoudao
*
*/
public class PersonText {
/**
* @param args
*/
public static void main(String[] args) {
Session session = new Configuration().configure().buildSessionFactory()
.getCurrentSession();
session.beginTransaction();
Person person = new Person();
person.setName("meiyoudao");
person.setId(122);
session.save(person);
session.getTransaction().commit();
}
}
//Annotation版本
package com.meiyoudao.domain;
import javax.persistence.Entity;
import javax.persistence.Id;
//注解实体类
@Entity
public class PersonAnno {
private int id;
private String name;
private String weight;
@Id //注解ID 放在get方法上
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 String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
}
<!--hibernate.cfg.xml中添加这一条映射关系-->
<mapping class="com.meiyoudao.domain.PersonAnno"/>
//测试方法
package com.meiyoudao.test;
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import com.meiyoudao.domain.PersonAnno;
/**
* @author meiyoudao
*
*/
public class PersonAnnoText {
/**
* @param args
*/
public static void main(String[] args) {
//不同之处是AnnotationConfiguration类
Session session = new AnnotationConfiguration().configure()
.buildSessionFactory().getCurrentSession();
session.beginTransaction();
PersonAnno person = new PersonAnno();
person.setName("meiyoudao");
person.setId(1242);
session.save(person);
session.getTransaction().commit();
}
}