@Embeddable注解Java类的作用

来源:互联网 发布:深圳淘宝大学培训 编辑:程序博客网 时间:2024/05/20 06:52
 1配置文件<?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>    <session-factory>        <!-- 配置连接数据库 -->        <property name="connection.username">root</property>        <property name="connection.password">root</property>        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>        <!-- 配置方言是非常重要!!!我这里配置的为MySQL5Dialect -->        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>        <!-- 配置C3P0连接池 -->        <property name="hibernate.c3p0.max_size">200</property>        <property name="hibernate.c3p0.min_size">2</property>        <property name="hibernate.c3p0.max_statements">50</property>        <property name="hibernate.c3p0.timeout"></property>        <property name="show_sql">true</property>        <property name="format_sql">true</property>        <property name="hbm2ddl.auto">create</property>        <!-- 配置映射文件 -->        <!--   <property name="hibernate.current_session_context_class">thread</property> -->        <mapping  class="cn.codekong.entity.Students"/>    </session-factory></hibernate-configuration>2.Address类package cn.codekong.entity;import javax.persistence.Embeddable;@Embeddable//嵌入类在其他类中充当属性(Embeddable:可嵌入)public class Address {    private String postCode;    private String address;    private String phone;    public Address(){}    public Address(String postCode, String address, String phone) {        super();        this.postCode = postCode;        this.address = address;        this.phone = phone;    }    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;    }}3.Students类package cn.codekong.entity;import java.util.Date;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity //表示该类是一个持久化类@Table(name="test" ,schema="hibernate") //表示Students类映射到数据库中的对应的表名为t_StuInfopublic class Students{    @Id //声明主键    @GeneratedValue(strategy=GenerationType.IDENTITY)    private int sid;    //@Column(name="姓名") //一般不使用,应为属性名对应的就是数据库中的字段名    private String sname;    private String gender;    private  Date birthday;    private String major;    // 在Studnts类中充当属性,@Embedded注解可加可不加效果一样    private Address address;    public Students() {    }    public Students(int sid, String sname, String gender, Date birthday, String major, Address address) {        super();        this.sid = sid;        this.sname = sname;        this.gender = gender;        this.birthday = birthday;        this.major = major;        this.address = address;    }    public int getSid() {        return sid;    }    public void setSid(int 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 Date getBirthday() {        return birthday;    }    public void setBirthday(Date birthday) {        this.birthday = birthday;    }    public String getMajor() {        return major;    }    public void setMajor(String major) {        this.major = major;    }    public Address getAddress() {        return address;    }    public void setAddress(Address address) {        this.address = address;    }}4.测试类testStudentsimport java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.service.ServiceRegistry;import org.junit.After;import org.junit.Before;import org.junit.Test;import cn.codekong.entity.Address;import cn.codekong.entity.Students;public class StudentsTest {    private SessionFactory sessionFactory;    private Session session;    private Transaction transaction;    @Before    public void init() {        // 创建服务注册对象        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();        // 创建会话工厂对象        sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();        // 会话对象        session = sessionFactory.openSession();        // 开启事物        transaction = session.beginTransaction();    }    @After    public void destory() {        // 提交事物        transaction.commit();        // 关闭会话        session.close();        // 关闭会话工厂        sessionFactory.close();    }    @Test    // 向数据库中添加数据    public void testSaveStudents() {         Address address=new Address("428000","长沙","1839099999");        // 生成学生对象        Students student = new Students(2, "战狼","男",new Date(),"计算机",address);        System.out.println(student);        session.save(student);        System.out.println(session);    }} 总结:@Embeddable注解一个类的作用是把该类嵌入到目标类中,并把该类的属性映射到数据库表中的相应的字段。

这里写图片描述

原创粉丝点击