网站首页 > 数据库 / 正文
《大数据和人工智能交流》头条号向广大初学者新增C 、Java 、Python 、Scala、javascript 等目前流行的计算机、大数据编程语言,希望大家以后关注本头条号更多的内容。
一、实体类
package com.po;
import java.math.BigDecimal;
/**
* Goods entity. @author MyEclipse Persistence Tools
*/
public class Goods implements java.io.Serializable {
// Fields
private String gid;
private String gname;
private BigDecimal gprice;
// Constructors
/** default constructor */
public Goods() {
}
/** minimal constructor */
public Goods(String gid) {
this.gid = gid;
}
/** full constructor */
public Goods(String gid, String gname, BigDecimal gprice) {
this.gid = gid;
this.gname = gname;
this.gprice = gprice;
}
// Property accessors
public String getGid() {
return this.gid;
}
public void setGid(String gid) {
this.gid = gid;
}
public String getGname() {
return this.gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public BigDecimal getGprice() {
return this.gprice;
}
public void setGprice(BigDecimal gprice) {
this.gprice = gprice;
}
}
二、映射文件
关键看映射文件如何配置
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.po.Goods" table="GOODS" schema="LILY">
<id name="gid" type="java.lang.String">
<column name="GID" length="30" />
<generator class="sequence" >
<param name="sequence">goods_seq</param>
</generator>
</id>
<property name="gname" type="java.lang.String">
<column name="GNAME" length="30" />
</property>
<property name="gprice" type="java.math.BigDecimal">
<column name="GPRICE" precision="22" scale="0" />
</property>
</class>
</hibernate-mapping>
三、配置文件
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.url">
jdbc:oracle:thin:@127.0.0.1:1521:orcl
</property>
<property name="connection.username">lily</property>
<property name="connection.password">m123</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="myeclipse.connection.profile">
oracle.jdbc.driver.OracleDriver
</property>
<property name="show_sql">true</property>
<mapping resource="com/po/Goods.hbm.xml" />
</session-factory>
</hibernate-configuration>
四、hibernate自动生成的获取Session工具类
package com.test;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
*session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
五、测试
package com.test;
import java.math.BigDecimal;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.po.Goods;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Session s=HibernateSessionFactory.getSession();
Transaction tx = s.beginTransaction();
tx.begin();
Goods g=new Goods();
g.setGname("bbb");
g.setGprice(new BigDecimal(2000));
s.save(g);
tx.commit();
}
}
Tags:jdbc for oracle
猜你喜欢
- 2024-11-26 Spring boot管理系统,架构简单易懂,二次开发便捷
- 2024-11-26 Spring-Spring整合MyBatis详解
- 2024-11-26 如何使用JDBC操作数据库?一文带你吃透JDBC规范
- 2024-11-26 葵花宝典之数据库类面试真题10道(二)
- 2024-11-26 Mybatis的两个全局配置
- 2024-11-26 ShardingSphere-JDBC的功能
- 2024-11-26 解析-我们来看看Java应用中如何去使用Proxool
- 2024-11-26 开发应用程序:React、GraphQL、Spring Data JPA、UCP,Oracle
- 2024-11-26 Spring系列面试题
- 2024-11-26 「软帝学院」28道java基础面试题-下