网站首页 > 技术文章 正文
《大数据和人工智能交流》头条号向广大初学者新增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-09-06 MyBatis的10种用法_mybatis中$的用法
- 2025-09-06 PostgreSQL系列(二):布尔类型和数值类型操作
你 发表评论:
欢迎- 09-0613.通过Excel导出数据库中的维值_数据库exp导入导出数据
- 09-06做数据分析时,SQL需要达到以下水平
- 09-06Java开发指南:JDK21下载、安装及目录解析,轻松开启编程之旅
- 09-06hive存储过程_hive存储过程环境变量
- 09-06Maven常用命令_maven常用命令有哪些
- 09-06JDK从8升级到21的问题集_jdk更新到几了
- 09-06Oracle狂刷存在感 NRF展会惊艳四座
- 09-06哪些软件支持UDI标签的生成与验证
- 最近发表
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端路由 (61)
- 前端数组 (73)
- 前端js面试题 (50)
- 前端定时器 (59)
- Oracle RAC (76)
- oracle恢复 (77)
- oracle 删除表 (52)
- oracle 用户名 (80)
- oracle 工具 (55)
- oracle 内存 (55)
- oracle 导出表 (62)
- oracle约束 (54)
- oracle 中文 (51)
- oracle链接 (54)
- oracle的函数 (58)
- oracle面试 (55)
- 前端调试 (52)
本文暂时没有评论,来添加一个吧(●'◡'●)