大配置文件
<?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.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- 指定数据库链接的url,hibernate链接的数据库名 -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<!-- 指定连接数据库的用户名 -->
<property name="connection.username">root</property>
<!-- 指定连接数据库的用户口令 -->
<property name="connection.password">root</property>
<!--格式化sql -->
<property name="format_sql ">true</property>
<!-- 指定数据库方言 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 根据需要自动创建数据库表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 打印sql 控制台-->
<property name="show_sql">true</property>
<!--关联小配置文件-->
<mapping resource="cn/sjl/entity/School.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
实体类
public class School {
private int schoolid;
private String schoolname;
public int getSchoolid() {
return schoolid;
}
public void setSchoolid(int schoolid) {this.schoolid = schoolid;}
public String getSchoolname() {
return schoolname;
}
public void setSchoolname(String schoolname) {
this.schoolname = schoolname;
}
}
小配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 映射文件开始 -->
<hibernate-mapping package="cn.sjl.entity">
<class name="School" table="School">
<id name="schoolid" column="schoolid">
<generator class="native"></generator>
</id>
<property name="schoolname" column="schoolname"></property>
</class>
</hibernate-mapping>
测试类
//get方法
@Test
public void test02(){
//构造Configuration对象,读取大配置文件
Configuration cfg=new Configuration().configure();
//构造工厂
SessionFactory factory=cfg.buildSessionFactory();
//Session对象 openSession()
Session session = factory.openSession();
School school1 = session.get(School.class, 2);
System.out.println("学校名称:"+school1.getSchoolname());
System.out.println(school1);
//关闭session
session.close();
}