Hibernate单表操作——单一主键

阅读更多
一 单一主键
assigned:由java应用程序负责生成(手工赋值)。
native:由底层数据库自动生成标示符,如果是MySQL,就是increment,如果是Oracle,就是sequence,等等。
 
二 通过native为MySQL生成单一主键
 
Students
import java.util.Date;
 
//学生类
public class Students {
        /*
         * JavaBeans的四点原则
         * 1.必须是公有的类
         * 2.提供公有的不带参数的默认的构造方法
         * 3.属性私有
         * 4.属性setter/getter封装
         */
        private int sid; // 学号
        private String sname; // 姓名
        private String gender; // 性别
        private Date birthday; // 出生日期
        private String address; // 地址
 
        public Students() {
        }
 
        public Students(int sid, String sname, String gender, Date birthday,
                        String address) {
                // super();
                this.sid = sid;
                this.sname = sname;
                this.gender = gender;
                this.birthday = birthday;
                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 getAddress() {
                return address;
        }
 
        public void setAddress(String address) {
                this.address = address;
        }
 
        @Override
        public String toString() {
                return "Students [sid=" + sid + ", sname=" + sname + ", gender="
                                + gender + ", birthday=" + birthday + ", address=" + address
                                + "]";
        }
 
}
 
StudentsTest
 
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
//测试类
public class StudentsTest {
    
    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;
    @Before
    public void init(){
        // 创建配置对象   
        Configuration config = new Configuration().configure();
        //config.addClass(Students.class);
        // 创建服务注册对象
        ServiceRegistry serviceRegistery = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
        // 创建会话工厂对象
        sessionFactory = config.buildSessionFactory(serviceRegistery);
        // 创建会话对象
        session = sessionFactory.openSession();
        // 开启事务
        transaction = session.beginTransaction();
    }
    
    @After
    public void destory(){
        // 提交事务
        transaction.commit();
        // 关闭会话
        session.close();
        // 关闭会话工厂
        sessionFactory.close();
    }
    
    @Test
    public void testSaveStudents(){
        //Students s= new Students(1,"张三丰","男",new Date(),"武当山");
        Students s= new Students();
        s.setSname("张三丰");
        s.setGender("男");
        s.setBirthday(new Date());
        s.setAddress("武当山");
        session.save(s);
    }
}
 
Students.hbm.xml
xml version="1.0"?>
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd";>
<hibernate-mapping>
    <class name="Students" table="STUDENTS">
        <id name="sid" type="int">
            <column name="SID" />
            <generator class="native" />
            
        id>
        <property name="sname" type="java.lang.String">
            <column name="SNAME" />
        property>
        <property name="gender" type="java.lang.String">
            <column name="GENDER" />
        property>
        <property name="birthday" type="java.util.Date">
            <column name="BIRTHDAY" />
        property>
        <property name="address" type="java.lang.String">
            <column name="ADDRESS" />
        property>
    class>
hibernate-mapping>
 
hibernate.cfg.xml
 
xml version="1.0" encoding="UTF-8"?>
DOCTYPE hibernate-configuration PUBLIC
                "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd";>
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.username">rootproperty>
            <property name="hibernate.connection.password">waDY820828property>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
            <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/hibernate?useUnicode=true&characterEncoding=UTF-8property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialectproperty>
        <property name="show_sql">trueproperty>
            <property name="format_sql">trueproperty>
            <property name="hbm2ddl.auto">updateproperty>
            <mapping resource="Students.hbm.xml" />
    session-factory>
hibernate-configuration>
 
三 测试结果
连续执行两次测试用例

Hibernate单表操作——单一主键_第1张图片
 

Hibernate单表操作——单一主键_第2张图片
 
  • Hibernate单表操作——单一主键_第3张图片
  • 大小: 33.8 KB
  • Hibernate单表操作——单一主键_第4张图片
  • 大小: 26 KB
  • 查看图片附件

你可能感兴趣的:(Hibernate单表操作,单一主键)