Hibernate

Hibernate:用面向对象的方法除了数据库操作,利用ORM对象关系映射框架

1.Hibernate的映射文件:一个类对应一个映射文件,存储类的特点,以XML的形式存放。将对象映射成数据库中的表,是链接数据库与对象的纽带

2.Hibernate的配置文件:对数据库的描述,如使用什么数据库,与数据库交互所要用到的驱动类的设置等,文件为XML或properties属性文件

    XML文件:格式类似HTML文件,有自己的标签,用于定义自己的数据结构。具有格式化检查机制,可以指定映射文件的加载

映射文件:

<?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">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="Chapter04.Apple" table="apple">
        <id name="id" type="java.lang.String" column="id" length="32" >
            <generator class="uuid.hex" />
        </id>
        <property name="color" type="java.lang.String" update="true" insert="true" column="color" length="50" not-null="false" />
        <property name="size" type="java.lang.Float" update="true" insert="true" column="size" not-null="false" />
        <property name="weight" type="java.lang.Float" update="true" insert="true" column="weight" not-null="false" />
    </class>
</hibernate-mapping>

配置文件:

<?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>
    <session-factory>
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost:3306/community
        </property>
        
        <property name="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">chenhaibin</property>
    
    <!-- 指定映射文件 -->
    <mapping resource="Chapter04/Apple.hbm.xml"/>
    
    </session-factory>
</hibernate-configuration>

操作:

Configuration configuration = new Configuration(); //读取配置文件
        configuration = configuration.configure();
        SessionFactory factory = configuration.buildSessionFactory();//打开i与数据库的会话,并开始事务
        Session session = factory.openSession();
        Transaction transaction = session.beginTransaction();

        Apple apple = new Apple();
        apple.setColor("red");
        apple.setSize(20.0f);
        apple.setWeight(0.5f);

        session.save(apple);

        Apple apple2 = new Apple();
        apple2.setColor("red");
        apple2.setSize(15.0f);
        apple2.setWeight(0.4f);

        session.save(apple2);//保存入数据库

        String id = apple.getId();
        String id2 = apple2.getId();

        Apple newApple = null;
        newApple = (Apple) session.get(Apple.class, id);//根据id得到对应对象相关信息
        System.out.println("ID:" + newApple.getId() + ", Color:"
                + newApple.getColor() + ", Size:" + newApple.getSize()
                + ", Weight:" + newApple.getWeight());
        
        newApple = (Apple) session.get(Apple.class, id2);
        System.out.println("ID:" + newApple.getId() + ", Color:"
                + newApple.getColor() + ", Size:" + newApple.getSize()
                + ", Weight:" + newApple.getWeight());
        
        newApple.setColor("green");
        session.update(newApple);
        newApple = (Apple) session.get(Apple.class, id2);
        System.out.println("ID:" + newApple.getId() + ", Color:"
                + newApple.getColor() + ", Size:" + newApple.getSize()
                + ", Weight:" + newApple.getWeight());
        
        newApple = (Apple) sConfiguration configuration = new Configuration();
        configuration = configuration.configure();
        SessionFactory factory = configuration.buildSessionFactory();
        Session session = factory.openSession();
        Transaction transaction = session.beginTransaction();

        Apple apple = new Apple();
        apple.setColor("red");
        apple.setSize(20.0f);
        apple.setWeight(0.5f);

        session.save(apple);

        Apple apple2 = new Apple();
        apple2.setColor("red");
        apple2.setSize(15.0f);
        apple2.setWeight(0.4f);

        session.save(apple2);

        String id = apple.getId();
        String id2 = apple2.getId();

        Apple newApple = null;
        newApple = (Apple) session.get(Apple.class, id);
        System.out.println("ID:" + newApple.getId() + ", Color:"
                + newApple.getColor() + ", Size:" + newApple.getSize()
                + ", Weight:" + newApple.getWeight());
        Configuration configuration = new Configuration();
        configuration = configuration.configure();
        SessionFactory factory = configuration.buildSessionFactory();
        Session session = factory.openSession();
        Transaction transaction = session.beginTransaction();

        Apple apple = new Apple();
        apple.setColor("red");
        apple.setSize(20.0f);
        apple.setWeight(0.5f);

        session.save(apple);

        Apple apple2 = new Apple();
        apple2.setColor("red");
        apple2.setSize(15.0f);
        apple2.setWeight(0.4f);

        session.save(apple2);

        String id = apple.getId();
        String id2 = apple2.getId();

        Apple newApple = null;
        newApple = (Apple) session.get(Apple.class, id);
        System.out.println("ID:" + newApple.getId() + ", Color:"
                + newApple.getColor() + ", Size:" + newApple.getSize()
                + ", Weight:" + newApple.getWeight());
        
        newApple = (Apple) session.get(Apple.class, id2);
        System.out.println("ID:" + newApple.getId() + ", Color:"
                + newApple.getColor() + ", Size:" + newApple.getSize()
                + ", Weight:" + newApple.getWeight());
        
        newApple.setColor("green");
        session.update(newApple);
        newApple = (Apple) session.get(Apple.class, id2);
        System.out.println("ID:" + newApple.getId() + ", Color:"
                + newApple.getColor() + ", Size:" + newApple.getSize()
                + ", Weight:" + newApple.getWeight());
        
        newApple = (Apple) session.get(Apple.class, id);
        session.delete(newApple);
        newApple = (Apple) session.get(Apple.class, id2);
        session.delete(newApple);
        
        transaction.commit();
        
        session.close();
        newApple = (Apple) session.get(Apple.class, id2);
        System.out.println("ID:" + newApple.getId() + ", Color:"
                + newApple.getColor() + ", Size:" + newApple.getSize()
                + ", Weight:" + newApple.getWeight());
        
        newApple.setColor("green");
        session.update(newApple);
        newApple = (Apple) session.get(Apple.class, id2);
        System.out.println("ID:" + newApple.getId() + ", Color:"
                + newApple.getColor() + ", Size:" + newApple.getSize()
                + ", Weight:" + newApple.getWeight());
        
        newApple = (Apple) session.get(Apple.class, id);
        session.delete(newApple);
        newApple = (Apple) session.get(Apple.class, id2);
        session.delete(newApple);
        
        transaction.commit();Configuration configuration = new Configuration();
        configuration = configuration.configure();
        SessionFactory factory = configuration.buildSessionFactory();
        Session session = factory.openSession();
        Transaction transaction = session.beginTransaction();

        Apple apple = new Apple();
        apple.setColor("red");
        apple.setSize(20.0f);
        apple.setWeight(0.5f);

        session.save(apple);

        Apple apple2 = new Apple();
        apple2.setColor("red");
        apple2.setSize(15.0f);
        apple2.setWeight(0.4f);

        session.save(apple2);

        String id = apple.getId();
        String id2 = apple2.getId();

        Apple newApple = null;
        newApple = (Apple) session.get(Apple.class, id);
        System.out.println("ID:" + newApple.getId() + ", Color:"
                + newApple.getColor() + ", Size:" + newApple.getSize()
                + ", Weight:" + newApple.getWeight());
        
        newApple = (Apple) session.get(Apple.class, id2);
        System.out.println("ID:" + newApple.getId() + ", Color:"
                + newApple.getColor() + ", Size:" + newApple.getSize()
                + ", Weight:" + newApple.getWeight());
        
        newApple.setColor("green");
        session.update(newApple);
        newApple = (Apple) session.get(Apple.class, id2);
        System.out.println("ID:" + newApple.getId() + ", Color:"
                + newApple.getColor() + ", Size:" + newApple.getSize()
                + ", Weight:" + newApple.getWeight());
        
        newApple = (Apple) session.get(Apple.class, id);
        session.delete(newApple);
        newApple = (Apple) session.get(Apple.class, id2);
        session.delete(newApple);
        Configuration configuration = new Configuration();
        configuration = configuration.configure();
        SessionFactory factory = configuration.buildSessionFactory();
        Session session = factory.openSession();
        Transaction transaction = session.beginTransaction();

        Apple apple = new Apple();
        apple.setColor("red");
        apple.setSize(20.0f);
        apple.setWeight(0.5f);

        session.save(apple);

        Apple apple2 = new Apple();
        apple2.setColor("red");
        apple2.setSize(15.0f);
        apple2.setWeight(0.4f);

        session.save(apple2);

        String id = apple.getId();
        String id2 = apple2.getId();

        Apple newApple = null;
        newApple = (Apple) session.get(Apple.class, id);
        System.out.println("ID:" + newApple.getId() + ", Color:"
                + newApple.getColor() + ", Size:" + newApple.getSize()
                + ", Weight:" + newApple.getWeight());
        
        newApple = (Apple) session.get(Apple.class, id2);
        System.out.println("ID:" + newApple.getId() + ", Color:"
                + newApple.getColor() + ", Size:" + newApple.getSize()
                + ", Weight:" + newApple.getWeight());
        
        newApple.setColor("green");
        session.update(newApple);
        newApple = (Apple) session.get(Apple.class, id2);
        System.out.println("ID:" + newApple.getId() + ", Color:"
                + newApple.getColor() + ", Size:" + newApple.getSize()
                + ", Weight:" + newApple.getWeight());
        
        newApple = (Apple) session.get(Apple.class, id);
        session.delete(newApple);
        newApple = (Apple) session.get(Apple.class, id2);
        session.delete(newApple);
        
        transaction.commit();//提交事务
        
        session.close();//关闭会话
        transaction.commit();
        
        session.close();
        
        session.close();ession.get(Apple.class, id);
        session.delete(newApple);
        newApple = (Apple) session.get(Apple.class, id2);
        session.delete(newApple);
        
        transaction.commit();
        
        session.close();

Hibernate,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。