初始Hibernate框架
首先我们需要先创建一个案例
构建一个Student 的实体类
private String name; private Integer age; private Integer id;
在SRC根目录下构建一个大配置
名称是:Hibernate.cfg.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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">oracle.jdbc.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">sb</property> <property name="connection.password">sb</property> <!-- 输出所有 SQL 语句到控制台。 --> <property name="hibernate.show_sql">true</property> <!-- 在 log 和 console 中打印出更漂亮的 SQL。 --> <property name="hibernate.format_sql">true</property> <!-- 方言 --> <property name="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect</property> <!-- 关联小配置 --> </session-factory> </hibernate-configuration>
继而在组建一个名为:Student.hbm.xml 的小配置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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="cn.happy.entity"> <class name="Student" table="Student"> <id name="id" type="int" column="id"> </id> <property name="name" type="string" column="name"/> <property name="age" type="int" column="age"/> </class> </hibernate-mapping>
编写一个测试类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.After; import org.junit.Before; import cn.happy.entity.Student; import cn.happy.until.Hibernate_until; public class Test { public static void main(String[] args) { //addAll(); //delete(); find(); } public static void addAll() { Student stu=new Student(); stu.setSid(1); stu.setAge(18); stu.setName("我是狗"); //获取session对象 Session session = Hibernate_until.getSession(); //开启事务 Transaction tran = session.beginTransaction(); //hibernate保存 session.save(stu); System.out.println("成功保存!"); tran.commit(); Hibernate_until.closeSession(); } public static void delete() { //打开session Session session = Hibernate_until.getSession(); //开始一个事务 Transaction tx=session.beginTransaction(); //获取部门的对象 Student stu=(Student)session.get(Student.class, new Integer(1)); //删除对象(持久化操作) if(stu!=null) { session.delete(stu); } try { //提交事务 tx.commit(); System.out.println("删除成功"); } catch (Exception e) { //回滚事务 tx.rollback(); System.out.println("删除回滚"); } Hibernate_until.closeSession(); } public static void update() { Session session = Hibernate_until.getSession(); //开始一个事务 Transaction tx=session.beginTransaction(); //获取部门的对象 Student stu=(Student)session.get(Student.class, new Integer(1)); //删除对象(持久化操作) if(stu!=null) { stu.setName("武松"); session.update(stu); } try { //提交事务 tx.commit(); System.out.println("删除成功"); } catch (Exception e) { //回滚事务 tx.rollback(); System.out.println("删除回滚"); } Hibernate_until.closeSession(); } public static void find() { Session session = Hibernate_until.getSession(); //获取部门的对象 Student stu=(Student)session.get(Student.class, new Integer(2)); System.out.println(stu); } }
上述就是在Hibernate 框架中,第一次实例的书写