第一个annotation版的HelloWorld

来源:互联网 发布:数据库系统的二级映射 编辑:程序博客网 时间:2024/05/21 01:43

需要导入jar包:
1、hibernate中hibernate-release-5.2.10.Final\hibernate-release-5.2.10.Final\lib\required中的包。
2、mysql的驱动包:mysql-connector-java-5.1.25-bin
创建hibernate配置文件: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">  <![CDATA[jdbc:mysql://localhost:3306/cncode?useUnicode=true&characterEncoding=utf8]]></property>        <property name="connection.username">root</property>        <property name="connection.password">123</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> --><!--        <property name = "hibernate.cache.user_second_level_cache">true</property> -->        <property name="cache.provider_class">org.hibernate.cache.EHCacheProvider</property><!--            <property name="hibernate.cache.region.facory_class">org.hibernate.cache.ehcache.EnCacheRegionFactory</property> --><!--        <property name="hibernate.cache.user_query_cache">true</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">create</property> --><!--         <mapping resource="com/hibernate/Student.hbm.xml"/> -->        <mapping class = "com.hibernate.Teacher"/>    </session-factory></hibernate-configuration>

创建持久化类Teacher.java

package com.hibernate;import javax.persistence.Entity;import javax.persistence.Id;@Entity 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:

package com.hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class TeacherTest {    public static void main(String[] args){        Teacher t=  new Teacher();        t.setId(3);        t.setName("lun");        t.setTitle("中级");        Configuration cfg = new Configuration();        SessionFactory sf = cfg.configure().buildSessionFactory();        Session session =sf.openSession();        System.out.println("");        session.beginTransaction();        session.save(t);        session.getTransaction().commit();        session.close();        sf.close();    }}
原创粉丝点击