首先 ,编写实体类 。
package main.cn.happy.entity; /** * Created by lenovo on 2017/9/16. */ public class Dog { private Integer id; private String name; private Integer age; 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
此处 ,我创建了一个Dog 实体类 。
给大家看一下 结构图 :
接下来 ,编写大配置文件 (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> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin://localhost:1521/orcl <![CDATA[?useUnicode=true&characterEncoding=utf82]]></property> <property name="connection.username">scott</property> <property name="connection.password">orcl</property> <!-- JDBC connection pool (use the built-in) --> <!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect - Sql 方言 --> <!-- <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>--> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- 和 线程绑定 session--> <!-- 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 是否在控制台打印Sql --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <!--小配置路径--> <mapping resource="main/cn/happy/entity/Dog.hibernate.xml"></mapping> </session-factory> </hibernate-configuration>最后 , 我们来进行最后的测试步骤 :
package cn.happy.test; import main.cn.happy.entity.Dog; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; import org.springframework.transaction.annotation.Transactional; /** * Created by lenovo on 2017/9/17. */ public class FirstTest { public static void main(String[] args) { //创建配置 对象 Configuration cfg = new Configuration().configure("hibernate.cfg.xml"); SessionFactory sessionFactory = cfg.buildSessionFactory(); //创建sessions Session session = sessionFactory.openSession(); //创建事务 开启 Transaction transaction = session.beginTransaction(); //01 添加 Dog dog= new Dog(); dog.setName("郭华"); dog.setAge(15); session.save(dog); //提交事务 transaction.commit(); //关闭session session.close(); } /* * 修改记录 * */ @Test public void update() { Configuration cfg = new Configuration().configure(); SessionFactory sessionFactory=cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); //事务 Dog dog = new Dog(); dog.setId(2); dog.setAge(20); dog.setName("小黄"); session.update(dog); transaction.commit(); session.close(); System.out.println(" 修改成功 !!"); } //删除 @Test public void delete(){ Configuration cfg= new Configuration().configure(); SessionFactory sessionFactory = cfg.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Dog dog= new Dog();; dog.setId(21); session.delete(dog); tx.commit(); session.close(); System.out.println("删除 OK ! "); } }