zoukankan      html  css  js  c++  java
  • Hibernate学习笔记1.2(Annotation版本的Helloworld)

    hibernate 3.0之后开始支持Annotation

    接着1.1的项目

    首先 需要创建model Teacher.java、

    package com.hw.hibernate.model;
    
    public class Teacher {
         private int id;
         private String name;
         private String age;
         private String title;
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        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 getAge() {
            return age;
        }
        public void setAge(String age) {
            this.age = age;
        }
         
    }

    建立数据库teacher表 hibernate可以自动创建 本次先通过手动创建

    create table teacher(id int primary key,name varchar(20),age int,title varchar(20));

    引入Annotation的jar包

    在1.1项目userlibariray里加上annotation的jar包

    加完之后在Teacher.java里加上@Entity

    Teacher.java

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

    在hibernate.cfg.xml上加上对类的描述(最后一句)

    <?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>
    
            <!-- Database connection settings -->
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
            <property name="connection.username">root</property>
            <property name="connection.password"></property>
    
            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>
    
            <!-- SQL dialect -->
            <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.NoCacheProvider</property>
    
            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>
    
            <!-- Drop and re-create the database schema on startup -->
            <property name="hbm2ddl.auto">update</property>
    
            <mapping resource="com/hw/hibernate/model/Student.hbm.xml"/>
            <mapping class="com.hw.hibernate.model.Teacher"/> 
        </session-factory>
    
    </hibernate-configuration>

    创建新的测试类 Teacher.java

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.hibernate.cfg.Configuration;
    
    import com.hw.hibernate.model.Student;
    import com.hw.hibernate.model.Teacher;
    
    import javassist.bytecode.annotation.Annotation;
    
    public class teacherTest2 {
       public static void main(String args[]){
           Teacher s = new Teacher();
           s.setId(1);
           s.setName("huangwei");
           s.setAge("21");
           s.setTitle("教授");
           Configuration cf = new AnnotationConfiguration();
           SessionFactory sf = cf.configure().buildSessionFactory();
           Session ss = sf.openSession();
           ss.beginTransaction();
           ss.save(s);
           ss.getTransaction().commit();
           ss.close();
           sf.close();
       }
    }

    运行Teacher.java 

     附带:如果敲@不给提示

    window prefenrence

    Conrent exiets  如果觉得提示的时间过慢可以调整里面的数字 

  • 相关阅读:
    vscode入门使用教程(页面调试)
    .net core3.1开始页面实时编译
    Ubuntu 编辑文件、安装、删除软件等常用命令(持续更新)
    .NetCore3.1中的WebApi如何配置跨域
    PC电脑端如何多开Skype,一步搞定!
    简单几步为博客园添加动态动漫妹子
    如何在SqlServer中使用层级节点类型hierarchyid
    Entity framework Core 数据库迁移
    牛客网剑指offer【Python实现】——part1
    Linux实战——Shell编程练习(更新12题)
  • 原文地址:https://www.cnblogs.com/frankzone/p/9431878.html
Copyright © 2011-2022 走看看