Hibernate学习之类级别注解
Posted 禁忌夜色153
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate学习之类级别注解相关的知识,希望对你有一定的参考价值。
© 版权声明:本文为博主原创文章,转载请注明出处
类级别注解:
1. @Entity 实体:表示映射实体类,使用@Entity时必须指定实体类的主键属性
@Entity(name="")
name:可选,对应数据库中的一张表。若表名与实体名相同则可省略
2. @Table 表:表示实体类对应的数据库表的信息
@Table(name="", catalog="", schema="")
name:可选,映射表的名称,默认表名与实体名称一致,只有在不一致的情况下才需指定表名
catalog:可选,表示目录名称,默认为Catalog("")
schema:可选,表示模式名称,默认为Schema("")
3. @Embeddable 嵌入类:表示一个非Entity类可以嵌入到一个Entity类中作为属性而存在
实例
1.项目结构
2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.hibernate</groupId> <artifactId>Hibernate-ClassAnnotation</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <hibernate.version>5.1.7.Final</hibernate.version> </properties> <dependencies> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.42</version> </dependency> </dependencies> </project>
3.Address.java
package org.hibernate.model; import javax.persistence.Embeddable; @Embeddable public class Address { private String postcode;// 邮编 private String address;// 地址 private String telephone;// 电话 public String getPostcode() { return postcode; } public void setPostcode(String postcode) { this.postcode = postcode; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } }
4.Student.java
package org.hibernate.model; import java.util.Date; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="t_student") public class Student { private long id;// 学号 private String name;// 姓名 private Date birthday;// 出生日志 private String sex;// 性别 private Address address;// 地址 public Student() { } public Student(long id, String name, Date birthday, String sex, Address address) { this.id = id; this.name = name; this.birthday = birthday; this.sex = sex; this.address = address; } @Id public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
5.hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <!-- SessionFactory配置 --> <session-factory> <!-- 数据库常用配置 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">***</property> <property name="connection.url"> jdbc:mysql:///hibernate?useSSL=true&characterEncoding=UTF-8 </property> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 开发常用配置 --> <property name="show_sql">true</property>
<property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <!-- 引入映射类 --> <mapping class="org.hibernate.model.Student"/> </session-factory> </hibernate-configuration>
6.TestClassAnnotation.java
package org.hibernate.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestClassAnnotation { private SessionFactory sessionFactory; private Session session; private Transaction transaction; @Before public void before() { sessionFactory = new Configuration().configure().buildSessionFactory();// 创建SessionFactory对象 session = sessionFactory.openSession();// 获取Session对象 transaction = session.beginTransaction();// 开启事务 } @After public void after() { transaction.commit();// 提交事务 session.close();// 关闭Session sessionFactory.close();// 关闭SessionFactory } @Test public void testAnnotation() { System.out.println("执行成功..."); } }
7.效果预览
7.1 执行testAnnotation()方法-控制台
7.2 表结构
以上是关于Hibernate学习之类级别注解的主要内容,如果未能解决你的问题,请参考以下文章