hibernate的小例子及配置

首先我们看先hibernate的orm,下面是在某视频上引用的一段描述orm的简述

orm : object relational mapping (对象关系映射)

(1) 让实体类和数据库一一对应关系

(2)让实体类首先和数据库表对应

(3)让实体类属性和表里面字段对应

(4)不需要直接操作数据表,而操作表对应实体类对象

以上就对orm的文字描述

以下是hibernate4版本的小例子

<-----------------------------实体类Student--------------------------------->

public class Student {


private Integer id;
private String name;

省略get,set...

}

<-----------------------------在student.hbm.xml中配置------------------------------------>

//注意约束 student.hbm.xml和hibernate.cfg.xml约束是不一样的

<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">




<hibernate-mapping>
<class name="com.hbm.Student" table="t_student">
<id column="id" name="id">
<generator class="native" />  //自增长id
</id>
<property column="name" name="name" />



</class>
</hibernate-mapping>

<-----------------------------hibernate.cfg.xml配置-------------------------------->

<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
 
<!--第二部分  配置hibernate信息-->
<!--输出底层的sql语句  -->
<property name="hibernate.show_sql">true</property>
<!--输出底层的sql语句 格式 -->
<property name="hibernate.format_sql">true</property>
<!--hibernate帮创建表 ,需要配置之后 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置数据库方言
  让hibernate框架识别不同的数据库自己特有的语句
  -->
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  
  <!--在hibernate核心配置文件值配置  -->
<!--  <property name="hibernate.current_session_context_class">thread</property>
   -->
<!--第三部分  把映射文件放到核心配置文件中-->

<mapping resource="com/config/Student.hbm.xml"/>

</session-factory>

</hibernate-configuration>

<-----------------------------这里有个公共类hibernateUtils---------------------------------->

public class HibernateUtils {


    static SessionFactory SessionFactory = null;
static Configuration cfg=null;


static{

cfg=new Configuration();
cfg.configure();
SessionFactory = cfg.buildSessionFactory();

}

public static SessionFactory  getSessionFactory(){


return SessionFactory;
}

/* //提供返回与本地线程的session对象
public static Session getSessionObject(){

return SessionFactory.getCurrentSession();

}*/

//getSessionObject2工具类不需要在hibernate.cfg.xml里配置文件
public static Session getSessionObject2(){

return SessionFactory.openSession();

}
public static void CloseSession(Session session){
        if(session != null){
            session.close();
        }
    }    

<-----------------------------最后进行测试---------------------------->

@Test
public void  test05(){

Transaction beginTransaction = null;
try {
Session session = HibernateUtils.getSessionObject2();
beginTransaction = session.beginTransaction();


Criteria createQuery = session.createCriteria(Student.class);

List<Student> list = createQuery.list();

for (Student stu : list) {

System.out.println(stu.getName());
}

beginTransaction.commit();

} catch (Exception e) {
beginTransaction.rollback();
}
}

<-----------------------------控制台结果---------------------------------->


猜你喜欢

转载自blog.csdn.net/nvisalsd/article/details/78988263