Hibernate入门(以查询为例)

来源:互联网 发布:js when done 编辑:程序博客网 时间:2024/06/05 10:28

1、导入架包

2、配置hibernate.cfg.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hiberante-cofiguration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibrnate-configuration-3.0.dtd">
<!--
    hibernate配置文件;
    1、数据库的连接参数
    2、
 -->
<hibernate-configuration>
    <session-factory>
        <!-- 放数据库链接参数 -->
        <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="hibernate.connection.username">system</property>
        <property name="hibernate.connection.password">123456</property>
        <!-- hibernate自身属性
            dialect:方言,告知hibernate连接的数据的种类,进而hibernate可以针对对应的数据库做出相应的优化
        -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property><!-- 格式化输出sql -->
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect </property><!-- hibernate方言 -->
    org.hibernate.dialect.Oracle10gDialect
    </session-factory>
</hibernate-configuration><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hiberante-cofiguration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibrnate-configuration-3.0.dtd">
<!--
hibernate配置文件;
1、数据库的连接参数
2、hibernate自身属性
-->
<hibernate-configuration>
<session-factory>
<!-- 放数据库链接参数 -->
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">123456</property>
<!-- hibernate自身属性
dialect:方言,告知hibernate连接的数据的种类,进而hibernate可以针对对应的数据库做出相应的优化
-->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property><!-- 格式化输出sql -->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect </property><!-- hibernate方言 -->
</session-factory>
</hibernate-configuration>

*Oracle方言的配置:

     

                      

3、orm:搭建java实体和数据库表的映射:

               *映射java实体类和数据库表

               *映射实体中的属性和表中的字段

      *搭建hibernate的映射文件:位置任意,名称任意(建议命名—>一般行业默认为:XXX.hbm.xml)

          <!--将一个java类和数据库表映射在一起:

              *映射java实体类和数据库表

              *映射实体中的属性和表中的字段

              table=数据库表名

              name=类的全限定名

          -->

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- hibernate映射文件:
    *映射java实体类和数据表
    *映射实体中属性和表中的字段
 -->
<hibernate-mapping>
    <!-- 将一个java类和数据库表映射在一起:
        *映射java实体类和数据表
        *映射文件中的属性和表中字段
            table:是数据库表名
            name:是类的全限定名
     -->
     <class table="user" name="con.shijiejuju.User">
         <!--
             name=属性名
             column=字段名
             type=属性类型
          -->
        <id name="id" column="id" type="java.long.Integer">
        <!-- 指定主键维护(生成)策略 :
            increment:递增(仅测试用)
            -->
        <generator class="increment"></generator>
        </id>
         <property name="name" column="username" type="java.long.String"></property>
         <property name="age" column="age" type="java.long.Integer"></property>
     </class>
</hibernate-mapping>

4、注册映射文件(因为orm是任意名称、任意位置,所以要把orm映射文件注册到hibernate配置文件中)在hibernate.cfg.xml中添加配置

<!-- 注册映射文件 -->
        <mapping resource="com/shijiejuju/user.hbm.xml"/>

5、API:

查询为例:

package com.shijiejuju.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.shijiejuju.entity.User;

public class TestHibernate {
    @Test
    public void testHibernate(){
        //1、加载配置:configure()-->加载src下名为:hibernate.cfg.xml中的所有配置信息
        Configuration cfg = new Configuration().configure();
        //2、通过Configuration获得SessionFactory
        SessionFactory sf = cfg.buildSessionFactory();
        //3、通过SessionFactory获得Session对象
        //Session中会持有数据库连接,可以进行数据库所有操作
        Session session = sf.openSession();
        //通过id查询对应数据:查询除了id为1的用户
        User user =(User)session.get(User.class, 1);
        System.out.println(user);
    }
}




0 0
原创粉丝点击