通过实体的映射文件创建表的示例,所需jar包如下
antlr-2.7.7.jar;
dom4j-1.6.1.jar
geronimo-jta_1.1_spec-1.1.1.jar;
hibernate-commons-annotations-5.0.1.Final.jar;
hibernate-core-5.0.12.Final.jar;
hibernate-jpa-2.1-api-1.0.0.Final.jar;
jandex-2.0.0.Final.jar;
javassist-3.18.1-GA.jar;
jboss-logging-3.3.0.Final.jar;
ejb3-persistence.jar;
hibernate-annotations.jar;
下载,密码:xi7s;官网下载可阅读Hibernate---开发环境搭建文章;其中ejb3-persistence.jar与hibernate-annotations.jar两个包可直接下载hibernate-annotations-3.4.0.GA压缩文件
创建一个Student.java注释类:该类需要一个无参的构造函数,以及属性的get/set方法,而其中的注释导入的则是javax.persistence.*包。具体注释简介见Hibernate---实例类注释。
@Entity@Table(name = "students")public class Students implements Serializable { ???private static final long serialVersionUID = 2257468553009291835L; ???@Id ???@GeneratedValue(strategy = GenerationType.IDENTITY) ?????@Column(name="id") ???private int id; ???private String name; ???private int age; ???private boolean shifou; ???????public Students() { } ???????public Students(String name, int age, boolean shifou) { ???????super(); ???????this.name = name; ???????this.age = age; ???????this.shifou = shifou; ???} ???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; ???} ???public boolean getShifou() { ???????return shifou; ???} ???public void setShifou(boolean shifou) { ???????this.shifou = shifou; ???} ???}
配置Hibernate配置文件hibernate.cfg.xml(数据库连接信息),将该文件放到src目录下。注意,与实体注释类关联的mapping标签属性是class
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: ????GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the ????lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. --><!DOCTYPE hibernate-configuration PUBLIC ???"-//Hibernate/Hibernate Configuration DTD 3.0//EN" ???"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> ???<session-factory> ???????<!-- 配置数据库的方言 --> ???????<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> ???????<!-- 驱动程序名 --> ???????<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> ???????<!-- 数据库名称 --> ???????<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateTest</property> ???????<!-- 用户名 --> ???????<property name="hibernate.connection.username">root</property> ???????<!-- 密码 --> ???????<property name="hibernate.connection.password">123456</property> ???????<!-- 显示SQL语句 --> ???????<property name="show_sql">true</property> ???????<!-- 格式化输出SQL语句 --> ???????<property name="format_sql">true</property> ???????????????<property name="hbm2ddl.auto">create</property> ???????<!-- 与实体注释类关联 --> ???????<mapping class="com.hibernate.study.entity.Students" /> ???????????</session-factory></hibernate-configuration>
一个测试类
public class StudyTest { ???private static SessionFactory sessionFactory; // 会话工厂对象 ???private static Session session; ???private static Transaction transcction; ???public static void main(String[] args) { ???????// 创建配置对象 ???????Configuration config = new Configuration().configure(); ???????// 创建会话工厂对象 ???????sessionFactory = config.buildSessionFactory(); ???????// 会话对象 ???????session = sessionFactory.openSession(); ???????// 开启事务 ???????transcction = session.beginTransaction(); ???????// 生成学生对象 ???????Student stu = new Student("疾风剑豪", 10, true); ???????session.save(stu); // 保存对象到数据库 ???????transcction.commit(); // 提交事务 ???????session.close(); // 关闭会话 ???????sessionFactory.close(); // 关闭会话工厂 ???}}
至此还需要一个log4j.properties配置文件
Hibernate---数据操作示例BY实体类注释
原文地址:http://www.cnblogs.com/xiaobaizhiqian/p/7931755.html