欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Hibernate HelloWorld

程序员文章站 2022-04-08 23:03:53
Hibernate是ORM 框架,采用面向对象的方式来操作关系数据库,消除冗长的 SQL代码。 使用Hibernate4版本和MySQL数据库做测试 1.导jar包 antlr-2.7.7.jar dom4j-1.6.1.jar hibernate-c3p0-4.2.4.Final.jar hibe ......

   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

<?xml version="1.0" encoding="utf-8"?>
<!doctype hibernate-configuration public
		"-//hibernate/hibernate configuration dtd 3.0//en"
		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
    
		<!-- 配置连接数据库的基本信息 -->
		<property name="connection.username">root</property>
		<property name="connection.password">000111</property>
		<property name="connection.driver_class">com.mysql.jdbc.driver</property>
		<property name="connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
		<!-- 配置 hibernate 的基本信息 -->
		<!-- hibernate 所使用的数据库方言 -->
		<property name="dialect">org.hibernate.dialect.mysqlinnodbdialect</property>	
		<!-- 执行操作时是否在控制台打印 sql -->
		<property name="show_sql">true</property>
		<!-- 是否对 sql 进行格式化 -->
		<property name="format_sql">true</property>
		<!-- 指定自动生成数据表的策略 -->
		<property name="hbm2ddl.auto">update</property>
		
		<!-- 指定关联的 .hbm.xml 文件 -->
		<mapping resource="com/demo/hibernate/test/news.hbm.xml"/>
	
	</session-factory>
</hibernate-configuration>

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  

<?xml version="1.0"?>
<!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.demo.hibernate.test">

    <class name="news" table="news" dynamic-insert="true">
    	
        <id name="id" type="java.lang.integer">
            <column name="id" />
            <!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
            <generator class="native" />
        </id>
    
        <property name="title" not-null="true" unique="true"
        	index="news_index" length="50"
        	type="java.lang.string" column="title" >
        </property>
        
        <property name="author" type="java.lang.string"
        	index="news_index">
            <column name="author" />
        </property>
        
        <property name="date" type="date">
            <column name="date" />
        </property>
        
        <property name="desc" 
        	formula="(select concat(title, ',', author) from news n where n.id = id)"></property>
        	
		<!-- 映射大对象 -->
        <!-- 若希望精确映射 sql 类型, 可以使用 sql-type 属性. -->
		<property name="content">
			<column name="content" sql-type="text"></column>
		</property>
		
		<property name="image" column="picture" type="blob"></property>
		
    </class>
    
</hibernate-mapping>

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); 
	}
}