您的位置:首页 > 编程语言 > Java开发

[Java Web]Hibernate基础总结(一)

2014-10-12 11:23 316 查看
配置文件及基本属性

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- 数据库驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 连接url -->
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<!-- 用户名 -->
<property name="connection.username">root</property>
<!-- 密码 -->
<property name="connection.password">aaa</property>

<!-- 数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

<!-- Session上下文 -->
<property name="current_session_context_class">thread</property>

<!-- 是否使用二级缓存 -->
<property name="cache.use_second_level_cache">true</property>
<!-- 二级缓存方案 -->
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- 是否使用查询缓存 -->
<property name="cache.use_query_cache">true</property>

<!-- 是否打印sql语句 -->
<property name="show_sql">true</property>
<!-- 是否格式化sql语句 -->
<property name="format_sql">true</property>

<!-- 是否自动生成建表语句 -->
<!-- <property name="hbm2ddl.auto">update</property> -->

<!-- XML映射方法 -->
<mapping resource="hibernate/bean/Student.hbm.xml"/>
<!-- 注解(Annotation)映射方法 -->
<mapping class="hibernate.bean.Teacher" />

</session-factory>

</hibernate-configuration>


SessionFactory单例方法

package hibernate.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
// 根据hibernate.cfg.xml创建SessionFactory
Configuration cfg = new Configuration().configure();
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());
sessionFactory = cfg.buildSessionFactory(ssrb.build());
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

}


对象持久化方式

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 package="hibernate.bean">
<!-- 设置类名以及对应表名 -->
<class name="Student" table="_student">
<!-- 设置主键 -->
<id name="id" column="id">
<!-- 主键生成策略 -->
<generator class="native"/>
</id>
<!-- 设置其他属性 -->
<property name="name" column="name"/>
<property name="age" column="age"/>
</class>
</hibernate-mapping>

Annotation方式
package hibernate.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

//设置该对象可持久化
@Entity
//设置对应的表名
@Table(name="_teacher")
public class Teacher {

private int id;
private String name;
private int age;

//设置主键
@Id
//设置主键生成策略,默认为AUTO(native)
@GeneratedValue
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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: