• 小峰Hibernate简介与HelloWorld


    一、Hibernate简介:                              

    二、Hibernate4 版Hello World 实现                                  

    工程结构:

    com.cy.model.Student:

    package com.cy.model;
    
    public class Student {
        private long id;
        private String name;
        
        public long getId() {
            return id;
        }
        public void setId(long id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
    View Code

    Student.hbm.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.cy.model">
        <class name="Student" table="t_student">
            <!-- 
                <id>代表主键
                类的属性名是id,对应表中列名是stuId
             -->
            <id name="id" column="stuId">
                <!-- 主键生成策略,这里采用mysql的native,自增 -->
                <generator class="native"></generator>
            </id>
            
            <!-- 普通属性采用property 
                 不写column,那么数据库table的列名也叫name
            -->
            <property name="name"></property>
            
        </class>
        
    
    </hibernate-mapping>

    com.cy.service.StudentTest.java:

    package com.cy.service;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.boot.registry.StandardServiceRegistry;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    import org.hibernate.cfg.Configuration;
    
    import com.cy.model.Student;
    
    public class StudentTest {
        public static void main(String[] args) {
            Configuration configuration = new Configuration().configure();    //实例化配置文件,默认指定的就是hibernate.cfg.xml
            StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();//实例化服务登记,服务注册
            SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);    //获取session工厂
            Session session = sessionFactory.openSession();    //生成一个session
            session.beginTransaction();        //开启事务
            
            Student s = new Student();
            s.setName("张三");
            session.save(s);
            
            session.getTransaction().commit();    //提交事务
            session.close();    //关闭session
            sessionFactory.close();    //关闭session工厂
        }
    }

    hibernate配置文件:hibernate.cfg.xml:

    <?xml version='1.0' encoding='utf-8'?>
    <!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="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
            <property name="connection.username">root</property>
            <property name="connection.password">root</property>
           
            <!-- 方言 -->
            <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        
            <!-- 控制台显示SQL -->
            <property name="show_sql">true</property>
    
            <!-- 自动更新表结构 -->
            <property name="hbm2ddl.auto">update</property>
            
            <mapping resource="com/cy/model/Student.hbm.xml"/>
        </session-factory>
    
    </hibernate-configuration>

    运行StudentTest.java,测试结果:

  • 相关阅读:
    (*^_^*)Pose Estimation:DensePose
    (^_^)Pose Estimation:SamplePose
    [*v*/]人脸识别任务算法RetinaFace
    [*v*/]人脸识别任务算法MTCNN
    从Winograd算法看INT8量化及卷积加速原理
    [*_*/]Darknet && Mobilnet
    从TensorRT看INT8量化原理
    SSD算法精度
    信用评分卡模型的理论准备
    分类模型评估指标
  • 原文地址:https://www.cnblogs.com/tenWood/p/7237224.html
Copyright © 2020-2023  润新知