hibernate入门简单实现

hibernate属于ORM(object/relation mapping)对象关系映射数据库框架,具体解释搜索一下就知道了,orm框架还有OpenJPA,Mybatis等,但hibernate应该还是最主流的吧,毕竟做的也比较早,也是传说中SSH的一大成员之一,这么多年谈烂了还在谈的。废话就不说了,要做一个hibernate程序:

1、导jar包

发现不能传图片呀,我就打上吧

antlr.jar;c3p0.jar;commons-collections.jar;dom4j.jar;hibernate-jpa.jar;hibernate3.jar;javassist.jar;jta.jar;mysql-connector.jar;slf4j-api.jar

到这里http://www.hibernate.org/下载hibernate的jar包,以上很多jar都在里面

2、创建持久化类(pojo/bean)

对于bean对象,注意要有对应的get/set方法

要有无参数的默认的构造方法

不能用final修饰

这里代码如下

package com.wf.entity;



public class User {



	private long id;

	private String username;

	private String password;

	private String createtime;

	

	public long getId() {

		return id;

	}

	public void setId(long 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;

	}

	public String getCreatetime() {

		return createtime;

	}

	public void setCreatetime(String createtime) {

		this.createtime = createtime;

	}

	

}

3、创建映射文件(XXX.hbm.xml)

映射文件的作用就是将bean与数据库中的字段对应起来的,这里简单实现一个,还有就是这个配置文件必须与bean放到一个包中

名字为bean.hbm.xml;这里实现如下

View Code
 1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
5 <hibernate-mapping package="com.wf.entity">
6 <class name="User" table="t_user">
7 <id name="id" column="id">
8 <generator class="native"></generator>
9 </id>
10 <property name="username"></property>
11 <property name="password"></property>
12 <property name="createtime"></property>
13 </class>
14 </hibernate-mapping>

4、创建hibernate配置文件(hibernate.cfg.xml)

这一步的目的是连接数据库的,相当以前的JDBC连接数据库吧,这里在配置文件中实现了,这个文件要放到src下

View Code
 1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE hibernate-configuration PUBLIC
3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
5 <hibernate-configuration>
6 <session-factory>
7 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
8 <property name="connection.url">jdbc:mysql:///testdb</property>
9 <property name="connection.username">root</property>
10 <property name="connection.password">root</property>
11 <!-- hibernate方言(确定数据库)此处为mysql -->
12 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
13 <!-- 数据库连接池,我用的是c3p0连接池 -->
14 <property name="hibernate.c3p0.max_size">2</property>
15 <property name="hibernate.c3p0.min_size">2</property>
16 <property name="hibernate.c3p0.timeout">5000</property>
17 <property name="hibernate.c3p0.max_statements">100</property>
18 <property name="hibernate.c3p0.idle_test_period">3000</property>
19 <property name="hibernate.c3p0.acquire_increment">2</property>
20 <property name="hibernate.c3p0.validate">false</property>
21 <!-- 配置是否在控制台中显示sql语句 -->
22 <property name="hibernate.show_sql">true</property>
23 <!-- 绑定session -->
24 <property name="current_session_context_class">thread</property>
25 <!-- 配置映射文件,指定到自己所配置的映射文件 -->
26 <mapping resource="com/wf/entity/user.hbm.xml"/>
27 </session-factory>
28 </hibernate-configuration>

5、测试运行

呵呵,最后一步了,可以CRUD操作了

View Code
 1 package com.wf.test;
2
3 import org.hibernate.SessionFactory;
4 import org.hibernate.Transaction;
5 import org.hibernate.cfg.Configuration;
6 import org.hibernate.classic.Session;
7
8 import com.wf.entity.User;
9
10 public class test {
11
12 public static void main(String[] args) {
13 SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();;
14 Session session=sessionFactory.getCurrentSession();
15 Transaction tran = session.beginTransaction();
16 //save
17 User user=new User();
18 user.setUsername("wf");
19 user.setPassword("123");
20 tran.begin();//事物开始
21 session.save(user);
22 //update
23 /*User user2 = (User) session.load(User.class, 1);
24 user2.setUsername("wf2");
25 session.update(user2);*/
26 //delete
27 /*User user3=(User) session.load(User.class, 1);
28 session.delete(user3);*/
29 tran.commit();//事物提交
30 //query查询不用放入事物
31 /*List<User> userList = session.createQuery("from User").list();
32 for(User user4 : userList){
33 System.out.println(user4.getUsername());
34 }*/
35 }
36
37 }

现在很多参考书上好像都有session的打开与关闭,我也看到过,现在hibernate3.6之后获取的session可以不用手动关闭了

喀嚓,到此为止,睡觉
 



 

你可能感兴趣的:(Hibernate)