标签:
一般在实际的开发过程中,优先考虑使用JPA注解,这样更有利于程序的移植和扩展。
类级别注解
属性级别注解
映射关系注解
<!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.MySQLDialect</property> <!-- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate_struts_stumanager</property> --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mypage</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.show_sql">false</property> <property name="hibernate.hbm2ddl.auto">create</property> <property name="hibernate_current_session_context_class">thread</property> <mapping class="com.entity.Students"/> </session-factory> </hibernate-configuration>
package com.entity;
import javax.persistence.Entity;//JPA注解
import javax.persistence.Id;
import javax.persistence.Table;
/*
* 学生实体类
*/
//@Entity//
//@Entity(name="t_students")//如果不添加名字,则默认与实体类名字相同,如果想要自行设置表明,就需要自己进行添加
@Entity
@Table(name="t_students1",schema="mypage")
public class Students {
private String sid; //学号
private String sname;//姓名
private String gender;//性别
private String birthday;//出生日期
private String major;//专业
private Address add;
public Students(){
}
public Students(String sid, String sname, String gender,
String birthday, String major,Address add) {
// super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.major = major;
this.add = add;
}
@Id
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public Address getAdd() {
return add;
}
public void setAdd(Address add) {
this.add = add;
}
} package com.entity;
import javax.persistence.Embeddable;
// 地址类
@Embeddable //表示是一个嵌入类,这个类的对象在另一个实体类中充当属性
public class Address {
private String postCode;//邮编
private String address;//地址
private String phone;//联系电话
public Address(){
}
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 getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
测试类:
package com.entity;
import java.util.EnumSet;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
public class TestStudents {
@Test
public void testShemaExport(){
//创建hibernate配置对象
Configuration config = new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//生成SessionFactory
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
SchemaExport export = new SchemaExport(config);
export.create(true,true);
}
}标签:
原文地址:http://blog.csdn.net/wojiaohuangyu/article/details/51767718