zoukankan      html  css  js  c++  java
  • java web开发入门三(Hibernate)基于intellig idea

    Hibernate

    1.开发流程

    项目配置好后的结构:

    1.下载源码: 版本:hibernate-distribution-3.6.0.Final

    2.引入hibernate需要的开发包(3.6版本),如果没有引用成功,在jar包上右键执行:add as library

    3.编写实体对象及对象的映射xml文件

    实体类:

    package com.eggtwo.test;
    
    import java.util.Date;
    
    public class Student {
        private int  id;
        private String name;
        private int age;
        private Date birthday;
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    
    
    
    }

    数据库表结构:

     映射xml文件:(放在包下面)

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="com.eggtwo.test">
    
        <class name="Student" table="student">
            <!-- 主键 ,映射-->
            <id name="id" column="id">
                <generator class="native"/>
            </id>
            <!-- 非主键,映射 -->
            <property name="name" column="name"></property>
            <property name="age" column="age"></property>
            <property name="birthday" column="birthday"></property>
        </class>
    
    </hibernate-mapping>

    4.配置主配置文件:src/hibernate.cfg.xml

    <!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>
            <!-- 数据库连接配置 -->
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql:///test</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">123456</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    
            <property name="hibernate.show_sql">true</property>
    
            <!-- 加载所有映射 -->
            <mapping resource="com/eggtwo/test/Student.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>

    5.测试添加一条数据:

    package com.eggtwo.test;
    import java.util.Date;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.junit.Test;
    
    public class TestApp {
        @Test
        public void testHello() throws Exception {
            // 创建对象:主键自增长
            Student s = new Student();
            s.setAge(12);
            s.setName("班长");
            s.setBirthday(new Date());
    
            // 获取加载配置文件的管理类对象
            Configuration config = new Configuration();
            config.configure();  // 默认加载src/hibenrate.cfg.xml文件
            // 创建session的工厂对象
            SessionFactory sf = config.buildSessionFactory();
            // 创建session (代表一个会话,与数据库连接的会话)
            Session session = sf.openSession();
            // 开启事务
            Transaction tx = session.beginTransaction();
            //保存-数据库
            session.save(s);
            // 提交事务
            tx.commit();
            System.out.println("save success");
            // 关闭
            session.close();
            sf.close();
        }
    
    }

    2.映射详解

  • 相关阅读:
    es6箭头函数 this 指向问题
    vue脚手架搭建移动端项目--flexible.js
    vue通过自定义指令 v-py 将名字转拼音
    ES6相关
    闭包
    apply和call与this
    RPC
    Linux根目录下各目录文件类型及各项缩写全称
    网站分析相关
    [转]作为首席架构师,我是如何选择并落地架构方案的?
  • 原文地址:https://www.cnblogs.com/eggTwo/p/11822305.html
Copyright © 2011-2022 走看看