Hibernate 配制

[align=center][size=large][color=green]Hibernate 配制简介(一对多)[/color][/size][/align]
一、配制步骤
a.首到http://www.hibernate.org/ 下载hb最新版本,与数据库驱动包一起另载到项目下;
b.在项目目录下创建hibernate.cfg.xml文件--这是hibernate控制的核心;
c.在数据库中建两个表user 与 blog;
d.两个表对应的pojo类;
e.编写每个pojo类对应到数据库中每个表的映射文件;
f.编写hb中的Session对象的工具类
g.编写user与blog的Dao类;
h.编写测试类进行测试;

二、具体的代码实现
1、hibernate.cfg.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate
Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- hibernate 配置 -->
<hibernate-configuration>

<session-factory>

<!--让hb在运行时显示实际执行的sql语句 -->
<property name="show_sql">true</property>
<!-- 使显示的sql格式化 -->
<property name="format_sql">true</property>
<!-- 设定sql方法,使用的是mySQL -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- JDBC驱动类的名字 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--数据库连结串配置 -->
<property name="connection.url">jdbc:mysql://localhost:3306/bbs</property>
<!--数据库用户名配置 -->
<property name="connection.username">root</property>
<!-- 数据库密码配置 -->
<property name="connection.password">netjava</property>

<!-- user类的映射文件路径 -->
<mapping resource="cn/netjava/pojo/user.hb.xml"/>
<!-- blog类的映射文件路径 -->
<mapping resource="cn/netjava/pojo/blog.hb.xml"/>
</session-factory>
</hibernate-configuration>


2、两个对应的pojo类

public class User {
private int id;
private String name;
private String pwd;
private List<Blog> blogs;
// 对应的set,get方法;
.........
}
public class Blog {
private int id;
private User user;// 多对一关联到的父对象
private String title;
private String content;
// 对应的set,get方法;
.........
}



3、相应的映射文件
user.hb.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 指定表与类的映射 -->
<hibernate-mapping>
<class name="cn.netjava.pojo.User" table="user">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" column="name" />
<property name="pwd" column="pwd" />
<!-- 映射Userinfo对象与Bloginfo对象的一对多关系,以bag方式映射,按id顺排序,并延迟加载 -->

<bag name="blogs" table="blog" order-by="id asc" lazy="false"
cascade="save-update,persist">
<key column="id_user"/>
<one-to-many class="cn.netjava.pojo.Blog"/>
</bag>
</class>
</hibernate-mapping>

blog.hb.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 指定表与类的映射 -->
<hibernate-mapping>
<class name="cn.netjava.pojo.Blog" table="blog">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="title" column="title" />
<property name="content" column="content" />

<!--设定Blog对象与User对象的多对一关系 -->
<many-to-one name="user"
column="id_user"
class="cn.netjava.pojo.User"
cascade="all"
outer-join="true" />
</class>
</hibernate-mapping>

4、获取hb中的Session对象的工具类
public class HBUtils {
// 取得根据hibernate.cfg.xml中配置的数据库的一个Session对象;
// 这个Session对象类似与java.sql.connection对象;
public static Session getSession() {
return sessionFactory.openSession();
}
//静态块,用于从hibernate.cfg.xml配置中初始化sessionFactory对象
static {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
private static SessionFactory sessionFactory;
}


5、两个Dao类

public class Daouser {
// 保存一个用户对象到数据库,保存成功后,返回id
public int saveUserInfo(User user) {
org.hibernate.Session session = HBUtils.getSession();// 得到session对象
org.hibernate.Transaction tx = session.beginTransaction();
tx.begin();// 取得一个事务对象,开启事务
session.save(user);// 保存对象到数据库
tx.commit();// 提交事务。
session.close();
return user.getId();
}

// 从数据库中取得指定id的用户对象
public User getUserinfoById(int id) {
org.hibernate.Session session = HBUtils.getSession();
Object obj = session.get(cn.netjava.pojo.User.class, id);
session.close();
return (User) obj;
}
}
(下一个Dao类类似)

6、Test

public class Test {
public static void main(String args[]) {
User user = new User();// 新建一个User对象
user.setPwd("撒旦法");
user.setName("周");
Blog bl = new Blog();// 新建一个Blog对象
bl.setTitle("userBlog");
bl.setContent("blog");
bl.setUser(user);// 所属用户
Daoblog da = new Daoblog();// 保存Blog对象到数据库
int id = da.saveBlog(bl);
System.out.println("日志对象连同用户对象保存成功,id: " + bl.getId());

}

[quote]Hibernate中文参考文档[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值