Hibernate 使用注释

Hibernate使用注释有个好处就是我们不需要建立.hbm.xml文件,直接在实体类中添加注解就可以完成往数据库中进行数据操作

配置文件:hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//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:3306/student?serverTimezone=UTC</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect 方言 MySQLDialect不同数据库不一样-->
<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 生产的sql打印出来-->
<property name="show_sql">true</property>
<!-- <mapping resource="com/xxc/model/Student.hbm.xml"></mapping> -->
<mapping class="com.xxc.model.Teacher"></mapping>
</session-factory> </hibernate-configuration>

实体类Teacher.java

package com.xxc.model;

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

在这里,我们使用StandardServiceRegistryBuilder类和MetadataSources类从持久化类获取映射的信息。

package hibernateTest;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import com.xxc.model.Teacher; public class TeacherTest { public static void main(String[] args) {
Teacher t = new Teacher();
t.setId(1);
t.setName("xxc");
t.setTitle("高级");
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
SessionFactory sf = new MetadataSources(registry).buildMetadata().buildSessionFactory();
Session session = sf.openSession();
//开始事务
session.beginTransaction();
session.save(t);
//结束事务
session.getTransaction().commit();
session.close();
sf.close();
}
}

最新文章

  1. Eclipse中怎么安装TestNG单元测试框架
  2. vi技巧合集
  3. 使用decode函数实现统计
  4. MOOCULUS微积分-2: 数列与级数学习笔记 4. Alternating series
  5. Context3D 不可用
  6. Designing Evolvable Web API with ASP.NET 随便读,随便记 &ldquo;The Internet,the World Wide Web,and HTTP&rdquo;
  7. NIO组件Selector工作机制详解(上)
  8. HDU 5543 Pick The Sticks
  9. 【Java基础】 static
  10. GROUP BY,WHERE,HAVING间的区别和用法
  11. Python入门指南(超详细)
  12. Python内置方法中不明了的部分
  13. 20175209 《Java程序设计》第八周学习总结
  14. vue 之组件递归;
  15. windows Server 2008 R2 TFS2010的备份
  16. Java基础-Java中的堆内存和离堆内存机制
  17. VS2015 之 多行缩进
  18. BZOJ2662: [BeiJing wc2012]冻结 spfa+分层图
  19. 使用overflow:hidden之后使的同行元素不对齐
  20. Linux之du命令的使用

热门文章

  1. c# 类中使用ResolveUrl
  2. HTML的DOM树结构
  3. Solr开发文档(转)
  4. qt下的跨目录多工程编译(转)
  5. Android 修改 TextView 的全局默认颜色。
  6. windows10个性化设置
  7. ScreenCapturePro2 for Joomla_3.4.7-ckeditor4x
  8. html中怎么样让div并排显示
  9. MongoDB默认配置
  10. 减少C盘空间占用的技巧