Hibernate HelloWorld

   Hibernate是ORM 框架,采用面向对象的方式来操作关系数据库,消除冗长的 SQL代码。

   使用Hibernate4版本和MySQL数据库做测试

1.导jar包

  • antlr-2.7.7.jar
  • dom4j-1.6.1.jar
  • hibernate-c3p0-4.2.4.Final.jar
  • hibernate-commons-annotations-4.0.2.Final.jar
  • hibernate-core-4.2.4.Final.jar
  • hibernate-jpa-2.0-api-1.0.1.Final.jar
  • javassist-3.15.0-GA.jar
  • jboss-logging-3.1.0.GA.jar
  • jboss-transaction-api_1.1_spec-1.0.1.Final.jar
  • mysql-connector-java-5.1.7-bin.jar

2.配置hibernate.cfg.xml




	
    
		
		root
		000111
		com.mysql.jdbc.Driver
		jdbc:mysql://127.0.0.1:3306/test
		
		
		org.hibernate.dialect.MySQLInnoDBDialect	
		
		true
		
		true
		
		update
		
		
		
	
	

3.编写Bean与映射文件

package com.demo.hibernate.test;

import java.sql.Blob;
import java.util.Date;


public class News {
	
	private Integer id;
	private String title;
	private String author;
	
	private Date date;

	//该属性值为: title: author
	private String desc;
	
	//大文本
	private String content;
	//二进制数据
	private Blob image;
	
	
	
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Blob getImage() {
		return image;
	}

	public void setImage(Blob image) {
		this.image = image;
	}

	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 String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	} 

	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  






    
    	
        
            
            
            
        
    
        
        
        
        
            
        
        
        
            
        
        
        
        	
		
        
		
			
		
		
		
		
    
    

4.测试

package com.demo.hibernate.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestNews {
	
	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;

	/**
	 * 测试方法之前执行
	 */
	@Before
	public void init() {
		Configuration configuration = new Configuration().configure();
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
				.buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
	}

	/**
	 * 测试方法之后执行
	 */
	@After
	public void destroy() {
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
	
	/**
	 * 删除对象记录
	 */
	@Test
	public void testDelete() { 
		News news = (News) session.get(News.class, 1);
		session.delete(news); 
		System.out.println(news);
	}
	
	/**
	 * 修改对象记录
	 */
	@Test
	public void testUpdate() { 
		News news1 = (News) session.get(News.class, 1);
		news1.setContent("test");
		session.update(news1);
	}
	
	/**
	 * 获取对象记录
	 */
	@Test
	public void testGet() {
		News news = (News) session.get(News.class, 1); 
		System.out.println(news);
	}
	
	
	/**
	 * 添加一条记录,主键由数据库负责
	 */
	@Test
	public void testSave() {
		News news = new News();
		news.setTitle("CC");
		news.setAuthor("cc");
		news.setDate(new Date()); 
		System.out.println(news); 
		session.save(news); 
		System.out.println(news); 
	}
}

 

你可能感兴趣的:(Hibernate HelloWorld)