首页 > 代码库 > hibernate_02_session介绍

hibernate_02_session介绍

技术分享

写一个java类

 1 package com.imooc.hibernate;
 2 
 3 import java.util.Date;
 4 
 5 public class Students {
 6 
 7     private int sid;
 8     private String sname;
 9     private String gender;
10     private Date birthday;
11     private String address;
12 
13     public Students(int sid, String sname, String gender, Date birthday, String address) {
14         super();
15         this.sid = sid;
16         this.sname = sname;
17         this.gender = gender;
18         this.birthday = birthday;
19         this.address = address;
20     }
21 
22     @Override
23     public String toString() {
24         return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
25                 + ", address=" + address + "]";
26     }
27 
28     public int getSid() {
29         return sid;
30     }
31 
32     public void setSid(int sid) {
33         this.sid = sid;
34     }
35 
36     public String getSname() {
37         return sname;
38     }
39 
40     public void setSname(String sname) {
41         this.sname = sname;
42     }
43 
44     public String getGender() {
45         return gender;
46     }
47 
48     public void setGender(String gender) {
49         this.gender = gender;
50     }
51 
52     public Date getBirthday() {
53         return birthday;
54     }
55 
56     public void setBirthday(Date birthday) {
57         this.birthday = birthday;
58     }
59 
60     public String getAddress() {
61         return address;
62     }
63 
64     public void setAddress(String address) {
65         this.address = address;
66     }
67 }

生成类的配置文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-5-23 0:24:09 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.imooc.hibernate.Students" table="STUDENTS">
        <id name="sid" type="int">
            <column name="SID" />
            <generator class="assigned" />
        </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文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7         <property name="connection.username">root</property>
 8         <property name="connection.password">root</property>
 9         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
10         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>
11         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
12         <!-- 是否将sql语句打印到控制台,编码阶段便于测试,建议设为true -->
13         <property name="show_sql">true</property>
14         <!-- 是否将sql语句进行排版,建议设为true -->
15         <property name="format_sql">true</property>
16         <!-- 生成表结构的策略。create是如果之前有表结构,先删除表结构再重建;
17             update:原有的基础上更新;
18             create-drop:先删除表结构再重建
19             validate:与原表进行验证,如果和原表表结构不同,则不进行创建。
20             一般使用create和update
21          -->
22         <property name="hbm2ddl.auto">create</property>
23         
24         <mapping resource="com/imooc/hibernate/Students.hbm.xml"/>
25     </session-factory>
26 </hibernate-configuration>

如果不使用事务,hibernate不会在数据库生成相应的数据。当然,即使不使用事务,也不是没办法写入数据库。方法如下:

 1 package com.icoom.test;
 2 import static org.junit.Assert.fail;
 3 
 4 import java.sql.Connection;
 5 import java.sql.SQLException;
 6 import java.util.Date;
 7 
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.hibernate.Transaction;
11 import org.hibernate.cfg.Configuration;
12 import org.hibernate.jdbc.Work;
13 import org.hibernate.service.ServiceRegistry;
14 import org.hibernate.service.ServiceRegistryBuilder;
15 import org.junit.After;
16 import org.junit.Before;
17 import org.junit.Test;
18 
19 import com.imooc.hibernate.Students;
20 
21 public class StudentsTest {
22     
23     private SessionFactory sessionFactory;
24     private Session session;
25     private Transaction transaction;
26 
27     @Before
28     public void init() {
29         // 1.创建配置对象
30         Configuration config = new Configuration().configure();
31         // 2.创建服务注册对象
32         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
33         // 3.创建会话工厂对象
34         sessionFactory = config.buildSessionFactory(serviceRegistry);
35         // 会话对象
36         session = sessionFactory.openSession();
37         // 开启事务(注意:不开启事物就不能将对象保存在数据库中)
38 //        transaction = session.beginTransaction();
39     }
40     @After
41     public void destory() {
42 //        transaction.commit();
43         session.close();
44         sessionFactory.close();
45     }
46     @Test
47     public void testSaveStudents() {
48         // 生成学生对象
49         Students s = new Students(1, "老张", "男", new Date(), "山东");
50         //如果不使用事务,又想将数据像jdbc一样自动提交到数据库中,可以使用session.doWork方法
51         session.doWork(new Work(){
52 
53             @Override
54             public void execute(Connection connection) throws SQLException {
55                 connection.setAutoCommit(true);
56             }
57             
58         });
59         session.save(s);//保存对象进入数据库
60         session.flush();
61     }
62 
63 }

但是不推荐使用这种办法。

hibernate_02_session介绍