您的位置:首页 > 其它

hibernate配置文件版本

2015-02-03 19:59 309 查看
最近闲来无事自己练习了一下搭建开发框架。找了很多资料,spring 3.0.1+hibernate3.2.5的jar包整合没有冲突。最终完成了测试。2大框架学的时间也不短了。推荐大家在学习的过程中多去看官网下载的压缩包中的内容。很多我们在百度上问的东西里面都有案例和相关的文档解释。看英文还能学点英语,最后祝大家学习顺利吧。

源码+jar包打包下载:
http://pan.baidu.com/share/link?shareid=217859&uk=1997312776
1、 首先先下载hibernate和spring的jar包。推荐在官网下载,本篇blog用的是hibernate3.2.5+spring3.0.1

2、 建立web project。POJO类和映射文件。

Pojo类没有什么困难,但是本篇仍旧贴出代码如下:

package cn.sprhib.model;

public class Users {

private Long id;
private String username;
private String password;

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;
}
}


建立映射文件Users.hbm.xml

说明:关于头文件的引用和其中的一些xml写法案例在hibernate包的hibernate-3.0\eg\org\hibernate\auction下可以找到相关的案例写法。Copy过去即可,本案例映射代码如下:

<?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.sprhib.model.Users" table="Users" catalog="Sprhib">
<id name="id" type="long" column="id" length="3">
<generator class="identity"/>
</id>
<property name="username" column="username" length="10" not-null="true" type="string"/>
<property name="password" column="password" length="20" not-null="true" type="string"/>
</class>

</hibernate-mapping>


3、导入hibernate3.2.5相关Jar包。为生成数据库表做准备。导入Jar包情况请看下图:



除此之外还要导入Mysql的驱动包



4、编写hibernate.cxf.xml文件。

参考文件位置: hibernate-3.0.1\hibernate-3.0\etc下得hibernate.cxf.xml这个文件把大致轮廓都已经写好。其中的一些property的key还有value可以去同目录下的hibernate.properties文件中去查阅,当然也可以去该类的源码去中查找.代码如下:

<?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-configuration>
<session-factory>
<!-- 是否显示sql执行 -->
<property name="show_sql">true</property>
<!-- 设置方言,即数据库的平台 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 设置数据库驱动 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 设置数据库url -->
<property name="connection.url">
jdbc:mysql://192.168.1.104:3306/sprhib?useUnicode=true&characterEncoding=utf8
</property>
<!-- 设置连接用户名 -->
<property name="connection.username">root</property>
<!-- 设置连接密码 -->
<property name="connection.password">root</property>
<!--Examda,是否使用数据库外连接-->
<property name="hibernate.use_out_join">True</property>

<!-- 设置映射的xml文件.注意格式为/ -->
<mapping resource="cn/sprhib/model/Users.hbm.xml"/>

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


5、编写类将POJO通过配置好的ORM关系映射生成表:代码如下

public class ClassToDB {

public static void main(String[] args) {
Configuration config = null;
Session session = null;
Transaction sa = null;

config = new Configuration().configure(new File("src/hibernate.cfg.xml"));
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
sa = session.beginTransaction();
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true, true);
sa.commit();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: