[Java Web]Hibernate基础总结(一)
2014-10-12 11:23
316 查看
配置文件及基本属性
SessionFactory单例方法
对象持久化方式
XML方式
Annotation方式
<?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; } }
相关文章推荐
- [Java Web]Hibernate基础总结(二)
- [Java Web]Hibernate基础总结(四)
- [Java Web]Hibernate基础总结(四)
- [Java Web]Hibernate基础总结(三)
- java web B/s的简单实用基础总结(1)
- Java web基础总结十之—— jsp EL表达式
- SpringMVC_v02中的总结(web项目、java项目中引入Config.properties,加载hibernate映射文件的配置的区别)
- 【JavaWeb】基础知识总结05 jQuery
- Java web基础总结十之—— jsp EL表达式
- Java web基础总结三之—— java web 服务器
- Java web基础总结九之—— jsp标签
- 【JavaWeb】基础知识总结02 JSP
- 【JavaWeb】基础知识总结03 Servlet
- java web 开发中hibernate常见的基础问题
- javaWeb开发之基础--------------MySQL数据库总结
- Java web基础总结八之—— jsp基础
- [Java Web]Struts2基础总结(二)
- java web B/s的简单实用基础总结(2)
- [Java Web]Struts2基础总结(一)
- [Java Web]Struts2基础总结(三)