原来在hibernate中如果要用注解的时候,需要在获取configration对象的时候new一个AnnotationConfigration(),但是在hibernate5.0.7中可以直接用
<span style="font-size:18px;">Configuration configuration = new Configuration();</span>
而可以不用
<span style="font-size:18px;">Configuration configuration = new AnnotationConfiguration();</span>
测试结果也是正确的,应该是hibernate官方将jar包做了整合把(我猜测的)
顺便求问:在hibernate官网怎么才可以找到hibernate的完整包,谢谢
下面这个就是用注解写的
Teacher.java
<span style="font-size:18px;">package com.dqpi.eonline.model;
import javax.persistence.Entity;
import javax.persistence.Id;
<span style="color:#ff0000;">@Entity</span>
public class Teacher {
private int id;
private String name;
private String title;
<span style="color:#ff0000;">@Id</span>
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 String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
</span>
hibernate.cfg.xml
<span style="font-size:18px;"><?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>
<!-- 连接数据库的信息配置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- 数据库连接池 -->
<!-- <property name="connection.pool_size">1</property> -->
<!--数据库方言-->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</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> -->
<!--做实体类与mysql数据库之间的映射 -->
<mapping resource="com/dqpi/eonline/model/Student.hbm.xml"/>
<span style="color:#ff0000;"> <!-- 通过注解annotation来进行文件映射 -->
<mapping class="com.dqpi.eonline.model.Teacher"/></span>
</session-factory>
</hibernate-configuration></span>
TeacherTest.java
<span style="font-size:18px;">import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.dqpi.eonline.model.Teacher;
public class TeacherTest {
public static void main(String[] args) {
Teacher teacher = new Teacher();
teacher.setId(2);
teacher.setName("张三");
teacher.setTitle("教授");
//1.获取Configuration对象
Configuration configuration = new Configuration();
//2.通过configuration来获取SessionFactory对象
SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
//3.通过SessionFactory来获取session对象
Session session = sessionFactory.openSession();
//开启事务保存session
session.beginTransaction();
//把student对象s保存到数据库中
session.save(teacher);
//提交事务
session.getTransaction().commit();
//关闭session
session.close();
//关闭sessionFactory
sessionFactory.close();
}
}
</span>