第二十天 慵懒的投射在JDBC上的暖阳 —Hibernate的使用(二)

           5月30日,多云。“端午临中夏,时清人复长”。

        hibernate实现有两种配置,xml配置与注释配置。 Hibernate 3.2和Java 5以上版本支持注解。本篇把上一篇改为注解配置。配置更能体现ORM的精髓。

        把上一篇需要改写的步骤罗列,其余不变。

        4、创建POJO类UserInfo —添加注解

package edu.eurasia.hib;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity //注册在类头上,将一个类声明为一个实体bean(即一个持久化POJO类) 
public class UserInfo {
	private int id;
	private String username;
	private String password;
	@Id          //用来注册主属性
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

        5、删除对象关系映射文件UserInfo.hbm.xml

        6、改写hibernate.cfg.xml配置文件

<?xml version='1.0' encoding='gb2312'?>
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
	<!--配置数据库网络连接的url-->
	<property name="hibernate.connection.url">jdbc:mysql://localhost/hib</property>
	<!--配置数据库网络连接的用户名,默认一般为root-->
	<property name="hibernate.connection.username">root</property>
	<!--配置数据库网络连接的密码-->
	<property name="hibernate.connection.password">root</property>
	<!--配置数据库网络连接池的大小-->
	<property name="hibernate.connection.pool.size">20</property>
	<!--后台是否显示sql,开发时为true,运行时为false-->
	<property name="hibernate.show_sql">true</property>
	<!-- 设置JDBC的隔离级别-->
	<property name="hibernate.connection.isolation">2</property>
	<property name="hibernate.format_sql">true</property>
	<property name="jdbc.fetch_size">50</property>
	<property name="jdbc.batch_size">25</property>
	<property name="jdbc.use_scrollable_resultset">false</property>
	<property name="connection.useUnicode">true</property>
	<!--编码方式,最好是utf-8,用gb2312有的字符不全-->
	<property name="connection.characterEncoding">UTF-8</property>
	<!--数据库方言,每个数据库都有方言,hibernate已经为大多数数据库指明了方言-->
	<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	<!-- <mapping resource="edu/eurasia/hib/UserInfo.hbm.xml" /> -->
        <mapping class="edu.eurasia.hib.UserInfo"/>
</session-factory>
</hibernate-configuration>
        只用改写一句:把<mapping resource="edu/eurasia/hib/UserInfo.hbm.xml" />删除,增加 <mapping class="edu.eurasia.hib.UserInfo"/>即把引用:xxx.hbm.xml改为引用实体类

        7、修改测试类HibTest.java

package edu.eurasia.hib;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibTest {

	public static void main(String[] args) {
		/*SessionFactory sessions = new Configuration().configure()
				.buildSessionFactory();*/
		SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sf.openSession();		
		Transaction tx = null;
		try {
			// 开始事务
			tx = session.beginTransaction();
			// 给对象设定值
			UserInfo u = new UserInfo();
			u.setUsername("张三毛");
			u.setPassword("123456");
			System.out.println("开始插入数据到数据库……");
			// 保存数据到数据库
			session.save(u);
			// 结束事务
			tx.commit();
			tx = null;
			System.out.println("恭喜你,第一个hibernate程序运行成功!");
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			session.close();
		}

	}
}
               获取SessionFactory方式发生了变化:即:由SessionFactory sf = new Configuration().configure().buildSessionFactory()      改为:SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory()
                    8、运行结果xml配置方式是一样的。


             

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值