hibernate ——helloWorld程序(annotation配置)

在  《hibernate  ——helloWorld程序(XML配置)》基础上,修改、添加部分文件;

1、Teacher类和Teacher表

package com.pt.hibernate;

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        //加在get方法上面
    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;
    }
    
}
Teacher.java
1 CREATE TABLE `teacher` (
2   `id` int(20) NOT NULL,
3   `name` varchar(20) DEFAULT NULL,
4   `title` varchar(20) DEFAULT NULL,
5   PRIMARY KEY (`id`)
6 ) ENGINE=InnoDB DEFAULT CHARSET=utf8
Teacher.sql

2、hibernate.cfg.xml配置文件

 1 <?xml version='1.0' encoding='utf-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7 
 8     <session-factory>
 9 
10         <!-- Database connection settings -->
11         <!-- <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
12                     <property name="connection.url">jdbc:hsqldb:hsql://localhost/TestDB</property> -->
13 
14         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
15         <property name="connection.url">jdbc:mysql://localhost/test?useUnicode=true&amp;characterEncoding=UTF-8</property>
16         <property name="connection.username">root</property>
17         <property name="connection.password"></property>
18 
19         <!-- SQL dialect -->
20         <property name="dialect">
21             org.hibernate.dialect.MySQLInnoDBDialect
22         </property>
23         <property name="show_sql">true</property>
24          <mapping resource="com/pt/hibernate/Student.xml"/>
25          <mapping class="com.pt.hibernate.Teacher" />
26     </session-factory>
27 
28 </hibernate-configuration>
hibernate.cfg.xml

3、测试类

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.fasterxml.classmate.AnnotationConfiguration;
import com.pt.hibernate.Student;
import com.pt.hibernate.Teacher;


public class TeacherTest {
    public static void main(String[] arges){
        Teacher t= new Teacher();
        t.setId(20111911);
        t.setName("潘腾");
        t.setTitle("英语~~");
        Configuration cfg = new Configuration();
        SessionFactory sessionFactory = cfg.configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(t);
        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
        
    }
}
TeacherTest.java
原文地址:https://www.cnblogs.com/tengpan-cn/p/5432354.html