hibernate 入门

时间:2024-07-10 15:06:20

工程截图

hibernate 入门

1.jar包 和 hibernate配置文件 /src/hibernate.cfg.xml    , /src/log4j.properties   ,   /src/db.sql

<?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/study?useUnicode=true&amp;characterEncoding=UTF8</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- Enable Hibernate's automatic session conttext 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 show -->
<property name="show_sql">true</property> <!-- Format SQL -->
<property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property> <mapping resource="de/bvb/domain/Student.hbm.xml"/>
<mapping class="de.bvb.domain.Teacher"/>
</session-factory>
</hibernate-configuration>
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c:%L - %m%n ### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=warn, stdout #log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug ### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug ### log just the SQL
#log4j.logger.org.hibernate.SQL=debug ### log JDBC bind parameters ###
#log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug ### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug ### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug ### log cache activity ###
#log4j.logger.org.hibernate.cache=debug ### log transaction activity
#log4j.logger.org.hibernate.transaction=debug ### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug ### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
create database study DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

2.实体类

2.1基于配置 /src/de/bvb/domain/Student.java 和/src/de/bvb/domain/Student.hbm.xml

package de.bvb.domain;

public class Student {
private int id;
private String name;
private int age; 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 int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} }
<?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="de.bvb.domain.Student" table="_Student">
<id name="id"></id>
<property name="name"/>
<property name="age"/>
</class>
</hibernate-mapping>

2.2基于注解 /src/de/bvb/domain/Teacher.java

package de.bvb.domain;

import javax.persistence.Entity;
import javax.persistence.Id; @Entity
public class Teacher {
@Id
private int id;
private String name;
private String title; 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 getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} }

3.测试类 /src/de/bvb/test/Test1.java

package de.bvb.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.Test; import de.bvb.domain.Student;
import de.bvb.domain.Teacher; public class Test1 {
@Test
public void testStudent() {
Student s = new Student();
s.setId(1);
s.setName("zhangsan");
s.setAge(8); SessionFactory sessionFactory = new AnnotationConfiguration()
.configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
} @Test
public void testTeacher() {
Teacher t = new Teacher();
t.setId(1);
t.setName("t1");
t.setTitle("middle"); SessionFactory sessionFactory = new AnnotationConfiguration()
.configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
}
}

参考:  http://www.cnblogs.com/LonelyShadow/p/4800814.html#guying01sjcjhgn