Myeclipse 10.0 + Hibernate 4.3.5 连接 Oracle 11G

来源:互联网 发布:淘宝韩国第一美女模特 编辑:程序博客网 时间:2024/06/05 23:42
今天从下午开始按照J2EE企业应用实战整合Myeclipse 10.0 + Hibernate 4.3.5 连接 Oracle 11G,在网上找了很多资料,到晚上才搞定。有几个地方要特别注意,我在代码中会详细讲解。
现在上完整的代码:
先按照书里的要求引入%\hibernate-release-4.3.5.Final\lib\required下面所有的jar包。
文件1(News.java):
package org.crazyit.app.domain;
public class News {
private Integer id;
private String title;
private String content;
public void setId(Integer id){
this.id = id;
}
public Integer getId(){
return id;
}
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return title;
}
public void setContent(String content){
this.content = content;
}
public String getContent(){
return content;
}
}

这个不用讲了,和书里写的一样,hibernate直接采用POJO代替PO,不要求PO继承任何父类或者调用任何接口。

文件2(hibernate.cfg.xml):
添加jar包:
%\hibernate-release-4.3.5.Final\lib\optional\c3p0\c3p0-0.9.2.1.jar,
%\hibernate-release-4.3.5.Final\lib\optional\c3p0\hibernate-c3p0-4.3.5.Final.jar,
%\hibernate-release-4.3.5.Final\lib\optional\ehcache\slf4j-api-1.6.1.jar,
%\hibernate-release-4.3.5.Final\lib\optional\ehcache\slf4j-api-1.6.1.jar,
%\slf4j-1.7.7\slf4j-nop-1.7.7.jar(www.slf4j.org/download.html下载),
%\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar(数据库里的包)
<?xml version="1.0" encoding="GBK"?>
//注意DTD和文件3中的DTD是不一样的
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

    <session-factory>
      <!-- 指定数据库所用到的驱动 -->
     <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
     <!-- 指定数据库链接的url,hibernate链接的数据库名,我这里的数据库名是share,你们修改为自己的数据库名称即可 -->
     <property name="connection.url">jdbc:oracle:thin:@localhost:1521:share</property>
     <!-- 指定连接数据库的用户名 -->
     <property name="connection.username">scott</property>
     <!-- 指定连接数据库的用户口令 -->
     <property name="connection.password">123</property>
     <!-- 指定连接池里的最大连接数 -->
     <property name="hibernate.c3p0.maxsize">20</property>
     <!-- 指定连接池里最小连接数 -->
     <property name="hibernate.cp30.minsize">1</property>
     <!-- 指定连接池里的超时时常 -->
     <property name="hibernate.cp30.timeout">5000</property>
     <!-- 指定连接池里最大缓存多少个Statement对象 -->
     <property name="hibernate.cp30.max_statements">100</property>
     <property name="hibernate.cp30.idle_test_period">3000</property>
     <property name="hibernate.cp30.acquire_increment">2</property>
     <property name="hibernate.cp30.validate">true</property>
     <!-- 指定数据库方言 -->
     <property name="dialect">org.hibernate.dialect.OracleDialect</property>
     <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
     <!-- 根据需要自动创建数据库表 -->
     <property name="hbm2ddl.auto">update</property>
     <property name="show_sql">true</property>
     <!-- 罗列所有映射文件 -->
      <mapping resource="org/crazyit/app/domain/News.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

文件3(hibernate.hbm.xml):
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="org.crazyit.app.domain">
<class name="News" table="news_table">
<id name="id">
<generator class="sequence">
<param name="sequence">sequence_id</param>
</generator>
</id>
<property name="title"></property>
<property name="content"></property>
    </class>
</hibernate-mapping>

文件4(NewsManager.java):
//注意引用这里的包
import org.crazyit.app.domain.News;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;


public class NewsManager {
public static void main(String[] args) throws Exception{
//实例化Configuration
Configuration conf = new Configuration()
//下面的方法默认加载hibernate,cfg.xml文件
.configure();
//这里要特别注意:
  //书中的bulidSessionFactory()方法在hibernate4.3.5中已经deprecated
//在这里引入StandardServiceRegistryBuilder ()方法
//以Configuration创建SessionFactory
StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder().applySettings(
            conf.getProperties());
//创建session
StandardServiceRegistry sr = srb.build();
SessionFactory sf = conf.buildSessionFactory(sr);
Session sess = sf.openSession();
//开始事物
Transaction tx = sess.beginTransaction();
//创建消息实例
News n = new News();
//设置消息标题和消息内容
n.setTitle("疯狂java联盟成立了");
n.setContent("疯狂java联盟成立了,"
+ "网站地址http://www.crazyit.org");
//保存消息
sess.save(n);
//提交事物
tx.commit();
//关闭事物
sess.close();
sf.close();
}

}

文件配置好了之后,直接在NewsManager.java文件里run as java application就可以了
在console框里可以看到
Hibernate: select sequence_id.nextval from dual
Hibernate: insert into news_table (title, content, id) values (?, ?, ?)
就已经往oracle里插入了语句
然后用sql plus登陆oracle,查出来的结果如图
0 0
原创粉丝点击