黑马程序员技术交流社区

标题: 【济南校区】hibernate基本接入步骤 [打印本页]

作者: 大山哥哥    时间: 2018-2-28 23:58
标题: 【济南校区】hibernate基本接入步骤
1.导入相关jar包

    拷贝hibernate-release-5.0.7.Final目录下lib/required目录下所有jar包
   
2.导入数据库驱动jar包

3.导入日志相关jar包

    log4j-1.2.16.jar
    slf4j-api-1.6.1.jar
    slf4j-log4j12-1.7.2.jar

4.拷贝hibernate-release-5.0.7.Final\lib\optional\c3p0下所有jar包
   
    c3p0-0.9.2.1.jar
    hibernate-c3p0-5.0.7.Final.jar
    mchange-commons-java-0.2.3.4.jar

5.将hibernate-release-5.0.7.Final目录下project/etc/log4j.properties文件拷贝到工程src目录下

6.创建javabean和数据库表的映射关系的配置文件

    a)在javabean目录下创建 类名.hbm.xml eg: Customer.hbm.xml(注意:该文件要和javabean类Customer在同一层级目录下)

    b)在配置文件中配置javabean和数据库表的关系

            
[XML] 纯文本查看 复制代码
        <?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">
        <!-- package属性代表javabean所在的包 -->
        <hibernate-mapping package="com.b3a4a.domain">
            <!--
                name    实体类的全名
                table   表的名称
                catalog 数据库名称
            -->
            <class name="Customer" table="t_customer" catalog="db_hibernate_study">
                <!--
                    id它是用于描述主键
                        name      实体类中那个属性对应的是主键
                        column    数据库表中哪一列是主键
                -->
                <id name="id" column="id">
                    <!-- 主键生成策略 -->
                    <generator class="native"></generator>
                </id>
                <!--
                    使用property来描述属性与字段的对应关系
                        name   实体类中的属性
                        column 实体类中属性对应数据库表中的列
                        length 该列的长度限制
                -->
                <property name="name" column="name" length="20"></property>
                <property name="address" column="address" length="50"></property>
                <property name="sex" column="sex" length="20"></property>
            </class>
        </hibernate-mapping>

        
    注意:该配置文件的约束可以在hibernate-core-5.0.7.Final.jar中的org.hibernate包下的hibernate-mapping-3.0.dtd文件中复制

7.在项目的src目录下创建hibernate核心配置文件hibernate.cfg.xml(关于属性的配置可以参考project/etc/hibernate.properties)

[XML] 纯文本查看 复制代码
        <?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>
                        <!-- 配置关于数据库连接的四个项 driverClass url username password -->
                        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
                        <property name="hibernate.connection.url">jdbc:mysql:///db_hibernate_study</property>
                        <property name="hibernate.connection.username">root</property>
                        <property name="hibernate.connection.password">root</property>

                        <!-- 设置连接提供者 -->
                        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
                        <!-- c3p0连接池的配置 -->
                        <property name="hibernate.c3p0.max_size">20</property> <!-- 最大连接数 -->
                        <property name="hibernate.c3p0.min_size">5</property> <!-- 最小连接数 -->
                        <property name="hibernate.c3p0.timeout">120</property> <!-- 超时 -->
                        <property name="hibernate.c3p0.idle_test_period">3000</property> <!-- 空闲连接 -->

                        <!-- 可以将向数据库发送的sql显示出来 -->
                        <property name="hibernate.show_sql">true</property>
                        <!-- 格式化sql -->
                        <property name="hibernate.format_sql">true</property>

                        <!-- hibernate的方言 -->
                        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

                        <!-- 自动创建表 -->
                        <property name="hibernate.hbm2ddl.auto">update</property>

                        <!-- 用于设置事务提交方式 -->
                        <property name="hibernate.connection.autocommit">false</property>

                        <!-- 设置seesion和线程绑定 需要配合SessionFactory的getCurrentSession()使用 -->
                        <property name="hibernate.current_session_context_class">thread</property>

                        <!-- 配置hibernate的映射文件所在位置 -->
                        <mapping resource="com/b3a4a/domain/Customer.hbm.xml" />
                </session-factory>

        </hibernate-configuration>       


注意:该配置文件的约束可以在hibernate-core-5.0.7.Final.jar中的org.hibernate包下的hibernate-configuration-3.0.dtd

8.使用hibernate操作数据库
单元测试

[Java] 纯文本查看 复制代码
   @Test
    public void modifyCustomerTest() {
        // 创建一个Customer
        Customer c = new Customer();
        c.setName("张三");
        c.setAddress("北京");
        c.setSex("男");

        // 使用hibernate的api来完成将customer信息保存到mysql中操作
        Configuration config = new Configuration().configure(); // 加载hibernate.cfg.xml
        SessionFactory sessionFactory = config.buildSessionFactory();
        Session session = sessionFactory.openSession(); // 相当于得到一个Connection。
        // 开启事务
        Transaction transaction = session.beginTransaction();
      
        //--------------------------操作数据库-----------------------
        //-----------添加操作-------------
        //session.save(c);
        //--------------------------------
        //-----------查询操作-------------
        //查询一条数据
        //Customer c = session.load(Customer.class, 1);
        //查询多条数据
        //Query query = session.createQuery("from Customer"); // 这里的from Customer不是sql语句,是HQL,这里的Customer代表的Customer类
        //List<Customer> list = query.list();
        //--------------------------------
        //-----------修改操作-------------
        //Customer c = session.get(Customer.class, 2);
        //c.setName("王五");
        //session.update(c);
        //--------------------------------
        //-----------删除操作-------------
        //Customer c = session.get(Customer.class, 1);
        //session.delete(c);   
        //--------------------------------
        //-----------------------------------------------------------
      
        // 事务提交
        transaction.commit();
        session.close();
        sessionFactory.close();
    }


作者: jingxian90    时间: 2018-3-1 08:33
好详细~




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2