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配置方式是一样的。