• 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();

  • 相关阅读:
    JavaScript排序算法——快速排序
    JavaScript排序算法——归并排序
    JavaScript排序算法——堆排序
    JavaScript排序算法——插入排序
    ubuntu sublime text3 lisence
    jQuery学习笔记——弹出对话框
    CSS学习笔记——响应式布局
    CSS学习笔记——选择器
    JavaScript学习笔记——事件
    JavaScript学习笔记——节点
  • 原文地址:https://www.cnblogs.com/chengxuyuandashu/p/3615588.html
Copyright © 2020-2023  润新知