Student类
package com.wangning.bean;View Code
public class Student {
private Integer id;
private String name;
private int age;
private double score;
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", score="
+ score + "]";
}
public Student() {
super();
}
public Student(String name, int age, double score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.wangning.bean">
<!-- 映射文件的作用:
1、完成类到表的映射 2、属性到字段的映射 -->
<class name="Student" table="t_student">
<id name="id" column="tid">
<generator class="native"></generator>
</id>
<property name="name" column="tname"></property>
<property name="age" column="tage"></property>
<property name="score" column="tscore"></property>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- DB链接4要素 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///world</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">huadiyatou1314</property>
<!-- 方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 数据源 :数据连接池-->
<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
<!-- 当前session上下文:保证统一线程获取到的session是同一个-->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 显示SQL -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL -->
<property name="hibernate.format_sql">true</property>
<mapping resource="com/wangning/bean/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
分析:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
DTD----->这段代码可以从
hibernate-core-5.1.9.Final.jar/org/hibernate/hibernate-configuration-3.0.dtd
路径下copy。同理映射文件中的dtd也相应加入。
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.wangning.bean.Student;
public class Test1 {
@Test
public void mytest() {
//加载主配置文件
Configuration config=new Configuration().configure();
//创建Session工厂
SessionFactory sessionFactory=config.buildSessionFactory();
//获取Session
Session session=sessionFactory.getCurrentSession();
session.getTransaction().begin();
//执行操作
Student student =new Student("王五",21,100);
session.save(student);
session.getTransaction().commit();
}
}
1、默认名字:hibernate.cfg.xml 是因为 new Configuration().configure()方法。如下定义:
public static final String DEFAULT_CFG_RESOURCE_NAME = "hibernate.cfg.xml";
public Configuration configure() throws HibernateException {
return configure( StandardServiceRegistryBuilder.DEFAULT_CFG_RESOURCE_NAME );
}
我们也可以更换名字为 wangwu.cfg.xml。只需在方法调用时 变更为
//加载主配置文件
Configuration config=new Configuration().configure("wangwu.cfg.xml");