您的位置:首页 > 其它

完整的简单的Annotation版本的hibernate

2017-03-28 14:54 239 查看
1、所需要的jar包:



2、建表语句:

CREATE TABLE teacher(

id INT NOT NULL PRIMARY KEY,

name VARCHAR(20),

age int

)

3、java类:Teacher.java

package com.gao.hibernate.entity;

import javax.persistence.Entity;

import javax.persistence.Id;

@Entity

public class Teacher {

    private int id;

    private String name;

    private int age;

   

    @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 int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

   

   

   

}

4、配置文件: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://127.0.0.1:3306/hibernate?characterEncoding=utf8&useSSL=true</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.internal.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/huxing/hibernate/model/Student.hbm.xml"/> -->

  <mapping class="com.gao.hibernate.entity.Teacher"/>

 </session-factory>

</hibernate-configuration>

6、测试类:TeacherTestMain.java

package com.gao.hibernate.entity;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.AnnotationConfiguration;

import org.hibernate.cfg.Configuration;

public class TeacherTestMain {

    public static void main(String[] args) {

        Teacher t = new Teacher();

        t.setId(1);

        t.setName("aa");

        t.setAge(22);

       

        Configuration cfg = new AnnotationConfiguration();

        SessionFactory cf = cfg.configure().buildSessionFactory();

        Session session = cf.openSession();

        session.beginTransaction();

        session.save(t);

        session.getTransaction().commit();

        session.close();

        cf.close();

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: