一、hibernate简单搭建小实例

来源:互联网 发布:suse linux 配置ip 编辑:程序博客网 时间:2024/05/20 03:42

1.导入jar包

1)hibernate core 下载

2)mysql connector 下载

3)Junit 下载

4)实操图:

(1)这里为了以后导入jar包方便,创建了几个用户  Libraris:

点击Window->Preferences,出现如下界面:

(2)然后右键点击项目,选择build path->add Libraris:

       好,jar包导入完成。

2.创建配置文件:hibernate.cfg.xml 这里利用hibernate tools方便有快捷hibernate tools 下载

1)右键项目,New->Other->找到如图所示

next->next找到你的工程 然后Finish

2)编写hibernate.mfg.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">111111</property>    <property name="cache.provider_class">com.mysql.jdbc.Driver</property>    <property name="connection.url">jdbc:mysql://localhost/test?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="Students.hbm.xml"/>    </session-factory></hibernate-configuration>

3.创建实体类(以Students为例):Students.java

import java.util.Date;public class Students {private int sid;//学生IDprivate 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;}@Overridepublic String toString() {return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday+ ", address=" + address + "]";}}

4.创建对象关系映射文件 :Students.hbm.xml

1)右键项目,new->other,选中如图框中内容,

next->next:选中框中内容,finishin




2)代码如下:

<?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-8-12 14:16:35 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping>    <class name="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>


5.到配置文件“hibernate.cfg.xml”中添加关系映射文件“Students.hbm.xml”路径



6.创建Junit测试类StudentsTest.java 进行测试

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;@Beforepublic void init() {//创建配置对象,用于读取配置文档hibernate.cfg.xmlConfiguration  config = new Configuration().configure();//创建服务注册对象ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();//创建会话工厂sessionFactory = config.buildSessionFactory(serviceRegistry);//创建会话session = sessionFactory.openSession();//开启事物transaction = session.beginTransaction();}@Afterpublic void destroy() {//提交事物transaction.commit();//关闭会话session.close();//关闭会话工厂sessionFactory.close();}@Testpublic void testSaveStudents() {Students s = new Students(1,"张三丰","男",new Date(),"武当山");session.save(s);}}

这里用的是mysql数据库,数据库名:test,下面就可以测试了



原创粉丝点击