hibernate ——helloWorld程序(annotation配置)

本文详细介绍了在已有hibernate配置基础上,如何修改Teacher类和Teacher表,通过配置文件实现数据库连接,并展示了一个简单的测试类演示如何在数据库中插入教师信息。

在  《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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值