JR - 精品文章 - 利用weblogic的数据源作为hibernate的数据源的例子 From J2EE previous page next page 利用weblogic的数据源作为hibernate的数据源的例子 Jagie 原创 (参与分:90219,专家分:2595) 发表:2003-11-03 16:42 更新:2003-11-04 08:03 版本:1.0 阅读:7617次 在网上,我们可以看到许多关于hibernate入门的例子,但是好多是让hibernate自己管理连接池的。我在这里给出一个直接利用weblogic 发布的数据源的例子。步骤如下 1.写一个准备用于持久化的类 package com.jagie.business.organization; import java.io.Serializable; /** * <p>Title: </p> * <p>Description: 权限</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: www.jagie.com</p> * @author Jagie * @version 1.0 */ public class Permission implements Serializable { private String ID;//pk private String name;//名称 private String description;//描述 private String module;//模块id private String power;//权值,$分隔的操作id例如:browse$add$delete$change private int scope;//范围,0:本人,1:本单位,2:所有单位 public static void main(String[] args) { } public String getID() { return ID; } public void setID(String ID) { this.ID = ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getModule() { return module; } public void setModule(String module) { this.module = module; } public String getPower() { return power; } public void setPower(String power) { this.power = power; } public int getScope() { return scope; } public void setScope(int scope) { this.scope = scope; } } 2.编写一个xml文件,名称为Permission.hbm.xml,一定要确保在运行时该xml文件和Permission.class在一起[pre]<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping> <class name="com.jagie.business.organization.Permission" table="SYS_Permission"> <id name="ID"> <generator class="uuid.hex"/> </id> <property name="name"/> <property name="module"/> <property name="description"/> <property name="power"/> <property name="scope"/> </class></hibernate-mapping>[/pre] 3.在weblogic 上配置连接池和数据源,我的数据源的jndi名字为OilDS 4.修改classpath下的hibernate.properties文件,并保存a.添加一行:hibernate.dialect net.sf.hibernate.dialect.OracleDialectb.找到JNDI Datasource这一段,在下面设置hibernate.connection.datasource OilDSc.找到Plugin ConnectionProvider部分,去掉hibernate.connection.provider_class net.sf.hibernate.connection.DatasourceConnectionProvider一句的注释d.找到 Transaction API部分,去掉hibernate.transaction.manager_lookup_class net.sf.hibernate.transaction.WeblogicTransactionManagerLookup一句的注释e.保存修改 5.在类路径中编写一个jndi.properties文件,为了考虑灵活性,防止硬编码,该文件非常重要,内容如下java.naming.factory.initial=weblogic.jndi.WLInitialContextFactoryjava.naming.provider.url=t3://localhost:7001(我的weblogic服务器就在本机上,也许你的需要适当修改) 6.好啦,万事俱备,让我们写一个Test类来测试一下hibernate的威力好了,原码如下. package com.jagie.business.organization; import net.sf.hibernate.Session; import net.sf.hibernate.Transaction; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.cfg.Configuration; import net.sf.hibernate.tool.hbm2ddl.SchemaExport; import javax.naming.InitialContext; import javax.naming.Context; import javax.sql.*; import java.sql.*; import java.util.*; import com.jagie.utils.j2ee.*; public class Test { private static SessionFactory sessions; public static void main(String[] args) throws Exception { Configuration conf = new Configuration().addClass(Permission.class); sessions = conf.buildSessionFactory(); //生成并输出sql到文件(当前目录)和数据库 SchemaExport dbExport = new SchemaExport(conf); dbExport.setOutputFile("sql.txt"); dbExport.create(true, true); //start...... Session s = sessions.openSession(); Transaction t = s.beginTransaction(); //1.用普通使用方式建立对象,填充数据 Permission p1 = new Permission(); p1.setName("1111"); //2.持久化 s.save(p1); //此时p1已经可以在数据库中找到 t.commit(); s.close(); } } 7.运行该类,即可看到数据库已经建立了一个sys_permission的表,并且插入了一条数据。很简单吧! previous page start next page