3.2 测试

第一步:添加必要的hibernate jar包

第二步:添加数据库驱动

第三步:

项目完整目录结构


代码:

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">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/hibernate?characterEncoding=UTF-8</property>
		<property name="connection.username">root</property>
		<property name="connection.password">admin</property>
		<!-- SQL dialect -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="current_session_context_class">thread</property>
		<property name="format_sql">true</property>
		<property name="show_sql">true</property>
		<property name="hbm2ddl.auto">update</property>
		
		<!-- 设置hibernate的事务隔离级别 -->
		<property name="connection.isolation">2</property>
		
		<mapping resource="com/hibernate/News.hbm.xml" />
	</session-factory>

</hibernate-configuration>

持久化类News.java

package com.hibernate;

import java.util.Date;

public class News {

	private Integer id;
	private String title;
	private String author;
	private Date date;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public News(String title, String author, Date date) {
		super();
		this.title = title;
		this.author = author;
		this.date = date;
	}
	public News() {}
	@Override
	public String toString() {
		return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]";
	}
}

News.hbm.xml

<?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="com.hibernate">
    <class name="News" table="news2">
        <id name="id" column="id">
            <generator class="native"></generator>
        </id>
     		<property name="title"></property>
     		<property name="author"></property>
     		<property name="date"></property>
    </class>
     
</hibernate-mapping>

测试类HibernateTest.java

测试的方法有:

session缓存

flush测试

refresh测试

save测试

clear测试

package com.hibernate;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.hibernate.News;
 
public class HibernateTest {
	private SessionFactory sessionfactory;
	private Session session;
	private Transaction transaction;
 
	/**先使用以下代码看能否正常运行,然后再测试各种方法
	public static void main(String[] args) {
    	
        SessionFactory sf = new Configuration().configure().buildSessionFactory();//获取SessionFactory
 
        Session s = sf.openSession();//通过SessionFactory 获取一个Session
        s.beginTransaction();//在Session基础上开启一个事务
 
        News p = new News();
        p.setAuthor("yiguang");
        p.setTitle("tianshilikai");
        p.setDate(null);
        s.save(p);//通过调用Session的save方法把对象保存到数据库
         
        s.getTransaction().commit();//提交事务
        s.close();//关闭Session
        sf.close();//关闭SessionFactory
    	
    	}
   **/
	
	@Before
	public void init() {
		 sessionfactory=new Configuration().configure().buildSessionFactory();
		 session= sessionfactory.openSession();//通过SessionFactory 获取一个Session
		 transaction=session.beginTransaction();
		
	}
	
	@After
	public void destroy() {
		transaction.commit();
		session.close();
		sessionfactory.close();
		
	}

	//测试clear
	/*
	 * clear():清理缓存
	 */
	@Test
	public void testClear() {
		News news5=(News) session.get(News.class,1);
		System.out.println(news5);
		
		session.clear();
		
		News news6=(News) session.get(News.class,1);
		System.out.println(news6);
	}
	
	//测试refresh 
	/*
	 * refresh():会强制发送select语句,以使session缓存中对象的状态和数据表中的数据保持一致。
	 */
	@Test
	public void refresh() {
		News news4=(News) session.get(News.class, 1);
		System.out.println(news4);
		
		session.refresh(news4);//修改Author
		System.out.println(news4);
	}
	
	//测试flush
	/**
	 * flush:使数据表中的记录和Session缓存中的对象的状态保持一致,为了保持一致,则可能会发送对应的sql语句。
		1.在Transaction的commit()方法中:先调用session的flush方法,再提交事务
		2.flush()方法可能会发送SQL语句,但不会提交事务。
		3.注意:在未提交事务或显示的调用session.flush()方法之前,也有可能会进行flush()操作。
			1.执行HQL或QBC查询,会先进行flushz()操作,以得到数据包的最新记录。因为save方法后,必须保证对象的id是存在的。

	 */
	@Test
	public void testSessionFlush() {
		News news=(News) session.get(News.class, 1);
		System.out.println(news);
		
		news.setAuthor("oracle");//开始Author为mysql
		session.flush();
		System.out.println(news);
	}
	
	@Test
	public void testSessionFlush2() {
		News news3=new News("fanren1","wangyu", new Date());
		session.save(news3);
	}
	
	//session缓存测试
	@Test
	public void testSessionCache() {
		//两条sql语句,但是只向数据库发送一条请求
		News news=(News) session.get(News.class, 1);
		System.out.println(news);
		
		News news2=(News) session.get(News.class, 1);
		System.out.println(news2);
		
    }
	
	//添加一条数据
	@Test
	public void testInsert() {
		News news1=new News("fanren2","wangyu",new Date());
		session.save(news1);
	}
 	
    	
}

先测试testInsert方法,控制台会显示:

Hibernate: 
    insert 
    into
        news2
        (title, author, date) 
    values
        (?, ?, ?)

对应数据库显示:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值