• Hibernate 使用注释书写配置文件


    Hibernate 使用注释

    Hibernate使用注释有个好处就是我们不需要建立.hbm.xml文件,直接在实体类中添加注解就可以完成往数据库中进行数据操作

    配置文件:hibernate.cfg.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
    
        <session-factory>
            <!-- Database connection settings -->
          <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
          <property name="connection.url">jdbc:mysql://localhost:3306/student?serverTimezone=UTC</property>
          <property name="connection.username">root</property>
          <property name="connection.password">123456</property>
    
          <!-- JDBC connection pool (use the built-in) -->
          <!-- <property name="connection.pool_size">1</property> -->
    
          <!-- SQL dialect 方言 MySQLDialect不同数据库不一样-->
          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    
          <!-- Enable Hibernate's automatic session context management -->
          <!-- <property name="current_session_context_class">thread</property> -->
    
          <!-- Disable the second-level cache -->
          <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
    
          <!-- Echo all executed SQL to stdout  生产的sql打印出来-->
          <property name="show_sql">true</property>
          <!-- <mapping resource="com/xxc/model/Student.hbm.xml"></mapping> -->
          <mapping class="com.xxc.model.Teacher"></mapping>
        </session-factory>
    
    </hibernate-configuration>

    实体类Teacher.java

    package com.xxc.model;
    
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="teacher")
    public class Teacher {
        private int id;
        private String name;
        private String title;
        @Id
        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 String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        
    }

    操作类TeacherTest.java

     在这里,我们使用StandardServiceRegistryBuilder类和MetadataSources类从持久化类获取映射的信息。

    package hibernateTest;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.boot.MetadataSources;
    import org.hibernate.boot.registry.StandardServiceRegistry;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    
    import com.xxc.model.Teacher;
    
    public class TeacherTest {
    
        public static void main(String[] args) {
            Teacher t = new Teacher();
            t.setId(1);
            t.setName("xxc");
            t.setTitle("高级");
            final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
            SessionFactory sf = new MetadataSources(registry).buildMetadata().buildSessionFactory();
            Session session = sf.openSession();
            //开始事务    
            session.beginTransaction();
            session.save(t);
            //结束事务
            session.getTransaction().commit();
            session.close();
            sf.close();
        }
    }

  • 相关阅读:
    Chrome developer tool:本人钟爱的 console、Network 功能简谈
    Node.js:实现知乎(www.zhihu.com)模拟登陆,获取用户关注主题
    简谈 JavaScript、Java 中链式方法调用大致实现原理
    jQuery 源码解析二:jQuery.fn.extend=jQuery.extend 方法探究
    jQuery UI 多选下拉框插件:jquery-ui-multiselect
    为什么 JavaScript 中基本数据类型拥有 toString 之类方法?
    谈谈 JavaScript 中的 this 指向问题
    重写和重载的区别
    JAVA的静态方法,静态变量,静态类。
    重载
  • 原文地址:https://www.cnblogs.com/alex-xxc/p/9751901.html
Copyright © 2020-2023  润新知