Eclipse下Hibernate入门

本文介绍了如何在Eclipse环境中配置并使用Hibernate框架进行数据库操作。包括数据库连接设置、POJO类创建、映射文件配置及基本的增删查改测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Eclipse下Hibernate入门

最近boss让做项目,借机学习了一下Hibernate,小有收获。
hiberante是对数据库持久层访问的一种机制,hibernate的应用可以使程序员将重点放到业务逻辑的实现上。hibernate的原理是将数据库结构封装,使程序员可以像使用普通对象一样调用数据库的相关接口,从实现数据库的相关操作。

由于Exadel基于eclipse集成了Hibernate,并且方便易用所以我选用
Exadel+Mysql
还需要hibernate3.jar http://www.hibernate.org/
mysql-connector http://dev.mysql.com/downloads/

在Mysql中新建test数据库(Mysql其实有个空的test数据库),然后新建下面的Table

create table user (
id int(10) not null auto_increment primary key,
name varchar(20) not null,
password varchar(20) not null,
email varchar(50),
address varchar(100)
)type=innodb;


新建Java Project,将Mysql_Driver,Hibernate两个user library添加到该工程的java build path中。

新建与数据表对应的POJO类:User和Contact

/**
*
*
*/
package com.user;

/**
* @author lzy
*
*/
public class User{
private Integer id;
private String name;
private String password;
private Contact contact;


/**
* @return Returns the id.
*/
public Integer getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Returns the password.
*/
public String getPassword() {
return password;
}
/**
* @param password The password to set.
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return Returns the contact.
*/
public Contact getContact() {
return contact;
}
/**
* @param contact The contact to set.
*/
public void setContact(Contact contact) {
this.contact = contact;
}


}
/**
*
*/
package com.user;

/**
* @author lzy
*
*/
public class Contact {
private String email;
private String address;

/**
* @return Returns the address.
*/
public String getAddress() {
return address;
}
/**
* @param address The address to set.
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return Returns the email.
*/
public String getEmail() {
return email;
}
/**
* @param email The email to set.
*/
public void setEmail(String email) {
this.email = email;
}
}

添加Hibernate支持,这时系统会要求输入一些信息,按提示即可
完成后系统自动生成hibernate.cfg.xml,User.hbm.xml
映射文件必须稍作修改。

hibernate.cfg.xml
<?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 >

<!-- local connection properties -->
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username"></property>
<property name="hibernate.connection.password"></property>
<!-- property name="hibernate.connection.pool_size"></property -->

<!-- dialect for MySQL -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="hibernate.show_sql">True</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<mapping resource="User.hbm.xml"/>


</session-factory>
</hibernate-configuration>

User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.user">
<class
name="User"
table="user"
>
<id
name="Id"
type="integer"
column="id"
>
<generator class="native"/>
</id>

<property
name="Name"
column="name"
type="string"
not-null="true"
length="20"
/>
<property
name="Password"
column="password"
type="string"
not-null="true"
length="20"
/>
<component name="Contact" class="Contact">
<property
name="Email"
column="email"
type="string"
not-null="false"
length="50"
/>
<property
name="Address"
column="address"
type="string"
not-null="false"
length="100"
/>
</component>



</class>
</hibernate-mapping>



3.测试
添加一个测试类:HibernateTest

package com.user;

import java.util.List;
import java.util.ListIterator;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateTest {
public static void main(String[] args) throws HibernateException {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

//
//testInsert(sessionFactory);

//
testQuery(sessionFactory);


sessionFactory.close();

}
public static void testInsert( SessionFactory sessionFactory )throws HibernateException {

Session session = sessionFactory.openSession();
Transaction tx= session.beginTransaction();
User user = new User();
Contact contact=new Contact();
contact.setEmail("email");
contact.setAddress("address");

user.setName("caterpillar");
user.setPassword("password");
user.setContact(contact);

session.save(user);
tx.commit();
session.close();
System.out.println("OK!");
}

public static void testQuery( SessionFactory sessionFactory )throws HibernateException {

Session session = sessionFactory.openSession();
Transaction tx= session.beginTransaction();
User user = new User();
Contact contact=new Contact();

Query query=session.createQuery("from User as user");
//query.setCharacter(1, 'M');
List names =query.list();
for(ListIterator it=names.listIterator();it.hasNext();){
user= (User)it.next();
System.out.println("Id: " + user.getId());
System.out.println("name: " + user.getName());
System.out.println("password: " + user.getPassword());
if(user.getContact()!=null){

if(user.getContact().getEmail()!=null){
System.out.println("Email: " + user.getContact().getEmail());
}
if(user.getContact().getAddress()!=null){
System.out.println("Address: " + user.getContact().getAddress());

}
}



}


tx.commit();
session.close();

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值