网站首页 > 技术文章 正文
《大数据和人工智能交流》头条号向广大初学者新增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();
}
}
猜你喜欢
- 2025-07-17 PageHelper - 最方便的 MyBatis 分页插件
- 2025-07-17 面试二:pagehelper是怎么实现分页的,
- 2025-07-17 MyBatis如何实现分页查询?(mybatis-plus分页查询)
- 2025-07-17 SpringBoot 各种分页查询方式详解(全网最全)
- 2025-07-17 如何在Linux上运行exe文件,怎么用linux运行windows软件
- 2025-07-17 快速了解hive(快速了解美国50个州)
- 2025-07-17 Python 中的 pyodbc 库(pydbclib)
- 2025-07-17 Linux搭建Weblogic集群(linux weblogic部署项目步骤)
- 2025-07-17 「DM专栏」DMDSC共享集群之部署(一)——共享存储配置
- 2025-07-17 故障分析 | MySQL 派生表优化(mysql pipe)
你 发表评论:
欢迎- 615℃几个Oracle空值处理函数 oracle处理null值的函数
- 608℃Oracle分析函数之Lag和Lead()使用
- 595℃0497-如何将Kerberos的CDH6.1从Oracle JDK 1.8迁移至OpenJDK 1.8
- 592℃Oracle数据库的单、多行函数 oracle执行多个sql语句
- 586℃Oracle 12c PDB迁移(一) oracle迁移到oceanbase
- 580℃【数据统计分析】详解Oracle分组函数之CUBE
- 569℃最佳实践 | 提效 47 倍,制造业生产 Oracle 迁移替换
- 560℃Oracle有哪些常见的函数? oracle中常用的函数
- 最近发表
-
- PageHelper - 最方便的 MyBatis 分页插件
- 面试二:pagehelper是怎么实现分页的,
- MyBatis如何实现分页查询?(mybatis-plus分页查询)
- SpringBoot 各种分页查询方式详解(全网最全)
- 如何在Linux上运行exe文件,怎么用linux运行windows软件
- 快速了解hive(快速了解美国50个州)
- Python 中的 pyodbc 库(pydbclib)
- Linux搭建Weblogic集群(linux weblogic部署项目步骤)
- 「DM专栏」DMDSC共享集群之部署(一)——共享存储配置
- 故障分析 | MySQL 派生表优化(mysql pipe)
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端路由 (61)
- 前端数组 (73)
- 前端js面试题 (50)
- 前端定时器 (59)
- 前端获取当前时间 (50)
- Oracle RAC (76)
- oracle恢复 (77)
- oracle 删除表 (52)
- oracle 用户名 (80)
- oracle 工具 (55)
- oracle 内存 (55)
- oracle 导出表 (62)
- oracle约束 (54)
- oracle 中文 (51)
- oracle链接 (54)
- oracle的函数 (58)
- 前端调试 (52)
本文暂时没有评论,来添加一个吧(●'◡'●)