hibernate helloworld(xml版本)

1 新建java项目,名为:hibernate_HelloWorld
2 学习建立user-library-hibernate,并加入相应的jar包
  a项目右键-build path-configure build path-add library
  b选择user-library,在其中新建library,命名为hibernate
  c 在该library中加入hibernate所需要的jar包
hibernate3.3.2
/hibernate3.jar
/lib/required目录下的所有包 6个(其中sl4j包的版本要与下面的sl4j-nop.jar包的版本相对应)
Sl4j-nop jar
或者windows-prefernces-java-build Path-user Libraries  在项目中直接引入libraries-user library-hibernate
或者 右击项目-build path-add External Archives—选择相应jar包
3 引入mysql的JDBC驱动包
4 在MYSQL中建数据库和相应的表student(id,name,age)
create database hibernate;
user hibernate;
create tables student(id int primary key,name varchar(20),age int);
5 建立hibernate配置文件hibernate.cfg.xml
a) 参考文档中COPY,
b) 修改对应的数据库连接,修改dialect内容,sql显示内容
c) 注释掉暂时用不上的内容
<?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.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://127.0.0.1/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <!-- JDBC connection pool (use the built-in) -->
       <!-- <property name="connection.pool_size">1</property> -->
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- Enable Hibernate's automatic session context management -->
        <!--  <property name="current_session_context_class">thread</property>-->
        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</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">update</property> -->
        <mapping resource="com/eagle/hibernate/model/Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
6 建立student类
package com.eagle.hibernate.model;
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}
7 建立映射文件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">
<hibernate-mapping package="com.eagle.hibernate.domain">
<class name="Student">
<id name="id"/>
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping>
8 将映射文件加到hibernate-cfg.xml中
修改hibernate-cfg.xml中<mapping resource="com/eagle/hibernate/model/Student.hbm.xml"/>
9 测试程序为
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.eagle.hibernate.model.Student;
public class StudentTest {
public static void main(String[] args){
Student s = new Student();
s.setId(2);
s.setName("s2");
s.setAge(23);
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}
}

你可能感兴趣的:(sql,Hibernate,xml,mysql,jdbc)