您的位置:首页 > 其它

【hibernate】我的第一个小例子

2016-03-13 23:41 330 查看
       学了一段时间的hibernate,但是总是感觉差那么点劲,俗话说的好理论与实践相结合,看再多的理论都是纸上谈兵,所以做了一个小例子.这个小Demo做完后,顿时发现hibernate其实很简单.闲话少叙.直接上干货.

 

Hibernate清单:

           

           1.*.cfg.xml  hibernate的配置文件.此文件用来连接数据库.

           2.实体类.

           3.*.hbm.xml  映射文件,此文件用来将实体类和数据库中的表进行关联

           4. 主函数。不解释

引入jar包

         导入hibernate必须的包
                Hibernate-release-4.2.4.Final\lib\required下的所有的包
         导入Mysql的JDBC驱动包。
               Mysql-connector-java-5.1.7-bin.jar
         导入Junit4包(用于单元测试的包,这个例子的实现是使用的单元测试)
              junit-4.10.jar 



Demo
        这个实例将要实现往数据库中添加一条数据的功能,在执行之前需要在mysql中创建一个名字为hibernate的数据库。

                

                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="connection.username">root</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>

<!-- 添入映射文件 -->
<mapping resource="hibernate/Students.hbm.xml"/>
</session-factory>
</hibernate-configuration>

          实体类

package hibernate;

import java.util.Date;

public class 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 Students() {
}

private int sid;
private String sname;
private String gender;
private Date birthday;
private String 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
+ "]";
}

}<strong>
</strong>
        student.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">
<!-- Generated 2016-3-10 20:53:38 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="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><strong>
</strong>
      单元测试类
package hibernate;

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 studentTest {

private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;

@Before
public void init()
{
//创建配置对象,
Configuration config=new Configuration().configure() ;
//创建服务器注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//创建会话工厂
sessionFactory=config.buildSessionFactory(serviceRegistry);
//创建会话对象
session=sessionFactory.openSession();
//打开事务
transaction=session.beginTransaction();
}

@Test
public void testSaveStudent()
{
//创建学生对象
Students s=new Students(1,"周星驰","男",new Date(),"香港");
session.save(s);

}

@After
public void destory()
{
transaction.commit();//提交事务
session.close();//关闭会话
sessionFactory.close();//关闭会话工厂
}
}<strong>
</strong>
总结

      读万卷书,不如行万里路。一个例子看起来不是很难,但是这一个例子,解开了我这么多天的疑惑。做了这一个例子,就好像发了一块新的大陆,有好多的地方等着我去探索。

        下一篇文章,咱们来看看这两个简单的配置文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hibernate J2EE jar包