hibernate学习 hibernate项目搭建

来源:互联网 发布:网络硬盘 编辑:程序博客网 时间:2024/05/24 04:31

hibernate的基础构建是基于配置的所以配置起来有点繁琐

这里写图片描述

先来张配置好的项目图压压惊先:
关于jar包的导入,待会会给出源码,就不做多解释,同学们自行拷贝。

使用hibernate之前,先要安装mysql
不会的童鞋自行百度,这里也不过多讲解,我用的数据库是5.0.18,这里用稍微高版本的数据库驱动,亲测没有问题

在mysql中建立数据库itheima12_hibernate
待会项目用到的数据库就是这个。

新建配置文件:
hibernate.cfg.xml,位置在上图显示的位置

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <!-- 一个sessionFactory·代表一个链接 -->    <session-factory>        <!-- 链接数据库的用户名 -->        <property name="connection.username">root</property>        <!-- 链接数据库的密码 -->        <property name="connection.password">root</property>        <!-- 链接数据库的驱动 -->        <property name="connection.driver_class">            com.mysql.jdbc.Driver        </property>        <!-- 链接数据库的url -->        <property name="connection.url">            jdbc:mysql://localhost:3306/itheima12_hibernate        </property>        <!-- 方言 告诉hibernate使用什么样的数据库,hibernate就会在底层拼接什么样的sql语句 -->        <property name="dialect">            org.hibernate.dialect.MySQLDialect        </property>        <!-- 根据持久化类生成表的策略 validate update create create-drop -->        <property name="hbm2ddl.auto">update</property>        <property name="show_sql">true</property>        <!-- 告诉hibernate,类的配置文件在哪 -->        <mapping resource="com/itheima12/hibernate/domain/Person.hbm.xml" />    </session-factory></hibernate-configuration>

代码注释很详细,就不做多解释了,关于项目约束的配置,直接拷贝就好,几乎不会变动

然后就是建立实体类:

package com.itheima12.hibernate.domain;import java.io.Serializable;public class Person implements Serializable {    private Long pid;    private String name;    private String description;    public Long getPid() {        return pid;    }    public void setPid(Long pid) {        this.pid = pid;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getDescription() {        return description;    }    public void setDescription(String description) {        this.description = description;    }}

在同一包名下新建Person.hbm.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>    <!--         class用来描述一个类        name 类的全名        table 该持久化类对应的表名 可以不写,默认值为类名        catalog 数据库名字   一般不写     -->    <class name="com.itheima12.hibernate.domain.Person" table="person">        <!--             用来描述主键            name 属性都得名称            column 属性名称对应的标的字段 可以不写,默认就是属性的名称            length 属性的名称对应的表的字段的长度 如果不写,默认是最大的长度         -->        <id name="pid" column="pid" length="5">            <!--                 主键的产生器             -->            <generator class="increment"></generator>        </id>        <property name="name" length="20" type="java.lang.String"></property>        <property name="description" length="50" type="java.lang.String"></property>    </class></hibernate-mapping>

然后就可以愉快的测试了:
新建CreateTable.java测试类:

package com.itheima12.hibernate.db;import org.hibernate.cfg.Configuration;import org.junit.Test;public class CreateTable {    @Test    public void testCreateTable() {        Configuration cfg = new Configuration();        cfg.configure();        cfg.buildSessionFactory();    }}

去数据库观察,就能发现表已经建好

接着可以对数据库进行crud操作:
新建测试类PersonDao.java,位置看第一张图

package com.itheima12.hibernate.crud;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.classic.Session;import org.junit.Test;import com.itheima12.hibernate.domain.Person;public class PersonDao {    /**     * 保存person     */    @Test    public void testSavePerson() {        // 加载了hibernate的配置文件        Configuration cfg = new Configuration();        cfg.configure();        SessionFactory sessionFactory = cfg.buildSessionFactory();        // 产生session        Session session = sessionFactory.openSession();        // 产生事物         Transaction transaction = session.beginTransaction();        // 创建一个对象        Person person = new Person();        person.setName("王二麻子");        person.setDescription("很爷们");        // 保存该对象        session.save(person);        // 事务提交,session关闭        transaction.commit();        session.close();    }    @Test    public void testGetPersonById() {        Configuration cfg = new Configuration();        cfg.configure();        SessionFactory sessionFactory = cfg.buildSessionFactory();        Session session = sessionFactory.openSession();        /**         * 第一个参数为持久化类的class形式 第二个参数为主键的值         */        Person person = (Person) session.get(Person.class, 1L);        System.out.println(person.getName());        session.close();    }    @Test    public void testUpdate() {        /**         * 1.先根据ID把要修改的对象查询出来          * 2.对对象进行修改         */        Configuration cfg = new Configuration();        cfg.configure();        SessionFactory sessionFactory = cfg.buildSessionFactory();        Session session = sessionFactory.openSession();        Transaction transaction = session.beginTransaction();        /**         * 先根据主键把其中一行数据查询出来,查询出来就是一个对象         */        Person person = (Person) session.get(Person.class, 1L);        /**         * 修改person对象         */        person.setDescription("就是爷们");        // 执行修改操作        session.update(person);        transaction.commit();        session.close();    }    @Test    public void testDelete() {        /**         * 1.先根据ID把要修改的对象查询出来          * 2.删除该对象         */        Configuration cfg = new Configuration();        cfg.configure();        SessionFactory sessionFactory = cfg.buildSessionFactory();        Session session = sessionFactory.openSession();        Transaction transaction = session.beginTransaction();        /**         * 把pid为1的对象提取出来         */        Person person = (Person) session.get(Person.class, 1L);        //删除一个对象        session.delete(person);        transaction.commit();        session.close();    }}

注释写的很详细,就不一一解释了,代码链接:
http://download.csdn.net/detail/qq_23356275/9891727

原创粉丝点击