玩转Web之SSH--Heibernate (一)---第一个demo

最近在学heibernate,是看马士兵老师的视频学的,在这里总结一下,做点笔记。关于heibernate的优点,大家可以在网上

百度,这里不做赘述,直接讲怎么使用heibernate


步骤一:新建项目,导入jar包

    在这里讲一种新的导入jar包的方法,首先在myEclipse中点击windows>>preferences>>java>>Buid Path>>User Libraries  在右侧点击New ,在打开的窗口中输入"hibernate",点击OK,选择hibernate,点击Add jars,导入所需的jar包,我这里导入的比较多,大家按实际情况导入,然后点击OK;

   新建一个java项目,右键BuidPath>>Add Libraries>>User Library 选择hibernate,点击OK,这样就导入hibernate的包了。

   接下来还要导入数据库驱动包,根据大家所用数据库的种类自行导入即可


步骤二:新建数据库/数据表

     我用的是sqlserver2008,这里贴出我的代码:

CREATE TABLE STUDENT
(
STUID INT PRIMARY KEY,
STUNAME VARCHAR(10),
STUSEX VARCHAR(5),
STUGRADE VARCHAR(10),
);
需要注意的是一定要设置主键,这关系到后面配置文件去连接数据表


步骤三:写javabean

在项目下建包hibernate,建类Student

package hibernate;

public class Student {
	private int stuId;
	private String stuName;
	private String stuSex;
	private String stuGrade;
	public int getStuId() {
		return stuId;
	}
	public void setStuId(int stuId) {
		this.stuId = stuId;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public String getStuSex() {
		return stuSex;
	}
	public void setStuSex(String stuSex) {
		this.stuSex = stuSex;
	}
	public String getStuGrade() {
		return stuGrade;
	}
	public void setStuGrade(String stuGrade) {
		this.stuGrade = stuGrade;
	}

}

步骤四:写Student.hbm.xml配置文件

    ,右键包 hibernate,点击new >>file>>输入Student.hbm.xml,点击OK,下面是我配置文件中的代码:

<?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="hibernate.Student">    
        <id name="stuId"></id>    
        <property name="stuName"/>    
        <property name="stuSex"/>    
        <property name="stuGrade"/>    
    </class>    

</hibernate-mapping>
<class name="hibernate.Student">     name为javabean的路径

<id name="stuId"></id>   id中name的值对应主键

步骤五:hibernate.cfg.xml

    右键项目,new>>file>>输入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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=Study</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">aaaaaa</property>

        <!-- JDBC connection pool (use the built-in) -->
       <!--  <property name="connection.pool_size">1</property> -->

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

        <!-- Echo all executed SQL to stdout -->
         <property name="show_sql">true</property> 

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <mapping resource="hibernate/Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

对应文件修改driver,url,username,password等等


最后一步:测试类

   新建一个包Test,新建类StudentTest:

package test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import hibernate.Student;

public class StudentTest {

	public static void main(String[] args) {
		Student s=new Student();
		s.setStuId(111);
		s.setStuName("张三");
		s.setStuSex("男");
		s.setStuGrade("大二");
		Configuration cfg=new Configuration().configure();
		SessionFactory sf=cfg.buildSessionFactory();
		Session session=sf.openSession();
		session.beginTransaction();
		session.save(s);
		session.getTransaction().commit();
		session.close();
		sf.close();
	}
}

然后运行就可以啦,这样不需要sql语句就可以将数据插入数据表中


你可能感兴趣的:(玩转Web之SSH--Heibernate (一)---第一个demo)