Hibernate讲解(二)-Idea中手工创建Hibernate的开发环境及简单例子
例子中用到的JAR可以到这里下载:
http://u.115.com/file/f91639838Hibernate_Suit.zip
下载校验码:f91639838
IDEA新建工程,结构如图:
Hibernate.cfg.xml是hibernate的配置文件里面主要放置了数据库连接属性,数据库驱动的指定,以及各个JAVA类的映射文件,如下事例:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--为true表示将Hibernate发送给数据库的sql显示出来 -->
<property name="show_sql">true</property>
<!-- SQL方言,这边设定的是MySQL -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 一次读的数据库记录数 -->
<property name="jdbc.fetch_size">50</property>
<!-- 设定对数据库进行批量删除 -->
<property name="jdbc.batch_size">30</property>
<!--驱动程序-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- JDBC URL -->
<property name="connection.url">jdbc:mysql://localhost/myhrm?characterEncoding=gb2312</property>
<!-- 数据库用户名-->
<property name="connection.username">root</property>
<!-- 数据库密码-->
<property name="connection.password"></property>
<!--映射文件 -->
<mapping resource="com/pojo/Users.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Users.hmb.xml是JAVA类文件,里面主要记载了该类和表如何映射的关系,如下示例:
1 <?xml version='1.0' encoding='utf-8'?>
2 <!DOCTYPE hibernate-mapping
3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5 <hibernate-mapping>
6 <class name="com.pojo.Users">
7 <id name="id" type="java.lang.Integer">
8 <generator class="native"></generator>
9 </id>
10
11 <property name="name" length="20"/>
12
13 <property name="email"/>
14 </class>
15 </hibernate-mapping>
Users.java
package com.pojo;
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2010-10-21
* Time: 0:18:15
* To change this template use File | Settings | File Templates.
*/
public class Users {
private int id;
private String name;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Main方法
import com.pojo.Users;
import org.hibernate.*;
import org.hibernate.cfg.AnnotationConfiguration;
import java.util.Iterator;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2010-10-21
* Time: 0:06:27
* To change this template use File | Settings | File Templates.
*/
public class Main {
private static final SessionFactory ourSessionFactory;
static {
try {
ourSessionFactory = new AnnotationConfiguration().
configure("hibernate.cfg.xml").
buildSessionFactory();
}
catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException {
return ourSessionFactory.openSession();
}
public static void main(final String[] args) throws Exception {
final Session session = getSession();
try {
System.out.println("querying all the managed entities...");
Transaction tx = session.beginTransaction();
Session ss = session.getSessionFactory().openSession();
Query query = ss.createQuery("from Users ");
List<Users> list = query.list();
Iterator<Users> it = list.iterator();
System.out.println("append:" + list.size());
while (it.hasNext()) {
Users user = it.next();
System.out.println("ID:" + user.getId());
System.out.println("Name:" + user.getName());
System.out.println("Pass:" + user.getEmail());
}
tx.commit();
}
finally {
session.close();
}
}
}
MYSQL数据库文件:(之前先得把数据库myhrm创建好)
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `users`
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) default NULL,
`email` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES ('1', '超级管理员', 'admin@abc.com');
INSERT INTO `users` VALUES ('4', '用户1', 'abc1');
INSERT INTO `users` VALUES ('5', '用户2', '');
INSERT INTO `users` VALUES ('6', 'demo', 'demo@demo.com');
这样的一个例子就可以运行了。