• 20160504-hibernate入门


    关系型数据库与面向对象

    模型不匹配(阻抗不匹配)
    Java面向对象语言,对象模型,其主要概念有:继承、关联、多态等;数据库是关系模型,其主要概念有:表、主键、外键等。
    解决办法:
    1使用JDBC手工转换。
    2使用ORM(Object Relation Mapping对象关系映射)框架来解决,主流的ORM框架有Hibernate、TopLink、OJB。
    安装配置
    将下载目录/hibernate3.jar和/lib下的hibernate运行时必须的包加入classpath中:
    antlr.jar,cglib.jar,asm.jar,commons-collections.jar,commons-logging.jar,jta.jar,dom4j.jar
     
    ①配置文件hibernate.cfg.xml和hibernate.properties,XML和properties两种,这两个文件的作用一样,提供一个即可,推荐XML格式,下载目录/etc下是示例配置文件。
    可以在配置文件指定:
    数据库的URL、用户名、密码、JDBC驱动类、方言等。
    启动时Hibernate会在CLASSPATH里找这个配置文件。
    ②映射文件(hbm.xml,对象模型和关系模型的映射)。在/eg目录下有完整的hibernate示例。
    小例子
    hibernate.cfg.xml
    <!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.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql:///test1</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password"></property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="hibernate.hbm2ddl.auto">create</property>
            <mapping resource="com/dzq/domain/User.hbm.xml"/>
        </session-factory>
        
    </hibernate-configuration>

    User.java

    package com.dzq.domain;
    
    import java.util.Date;
    
    public class User {
        private int id;
        private String username;
        private Date birthday;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    
    }

    User.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.dzq.domain">
    
        <class name="User">
            <id name="id">
                <generator class="native" />
            </id>
            <property name="username" />
            <property name="birthday" />
        </class>
    
    </hibernate-mapping>

    TestHibernate.java

    package com.dzq.test;
    
    import java.util.Date;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    
    import com.dzq.domain.User;
    
    public class TestHibernate {
      public static void main(String[] args) {
        Configuration cfg=new Configuration();
        cfg.configure();
        SessionFactory sc=cfg.buildSessionFactory();
        Session s=sc.openSession();
        Transaction tx=s.beginTransaction();
        
        User user=new User();
        user.setBirthday(new Date());
        user.setUsername("dudu");
        s.save(user);
        tx.commit();
        s.close();
        System.out.println("end");
    }
    }
     
  • 相关阅读:
    数据库之完整性约束
    数据库之数据类型
    数据库之表操作,数据操作
    mysql数据库之基本操作和存储引擎
    MySQL数据库之安装
    并发编程之socketserver模块
    python并发编程之IO模型
    [BZOJ 3207] 花神的嘲讽计划Ⅰ【Hash + 可持久化线段树】
    [BZOJ 1046] [HAOI2007] 上升序列 【DP】
    [BZOJ 1816] [Cqoi2010] 扑克牌 【二分答案】
  • 原文地址:https://www.cnblogs.com/xiaoduc-org/p/5460033.html
Copyright © 2020-2023  润新知