我们使用hibernate时经常在hibernate.cfg.xml文件中配置数据库连接的相关属性,是否显示sql语句,数据库的方言等,这些配置其实也可以在.properties文件中配置。现在我把这把文件的名字起为:hibernate.properties.

思路:写一个domian对象,以及这个domain对象映射到数据库中的.hbm.xml文件。和一个测试类(这个测试类是更新数据库中的一条数据)

以及hibernate.properties文件(这个文件是放在src的下面即在classPath的根目录下)

一:domain Person的代码是:

package com.qls.domain;

import java.util.Date;

/**
* Created by 秦林森 on 2017/5/21.
*/
public class Person {
private Integer id;
private String name;
private Date enterCampusDate; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Date getEnterCampusDate() {
return enterCampusDate;
} public void setEnterCampusDate(Date enterCampusDate) {
this.enterCampusDate = enterCampusDate;
}
}

二:Person.hbm.xml文件的代码如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.qls.domain">
<class name="Person" table="person">
<id name="id" column="person_id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="enterCampusDate" type="timestamp"/>
</class>
</hibernate-mapping>

三:hibernate.properties文件的代码如下:

#the row end do not add semicolon;
hibernate.show_sql=true
hibernate.connection.driver_class=oracle.jdbc.OracleDriver
hibernate.connection.url=jdbc:oracle:thin:@localhost:1521:orcl
hibernate.connection.username=scott
hibernate.connection.password=a123456
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.connection.pool_size=10

四:测试类

package com.qls.test;

import com.qls.domain.Person;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.ImprovedNamingStrategy; import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; /**
* Created by ${秦林森} on 2017/5/22.
*/
public class Test12 {
public static void main(String[] args) {
Configuration configuration = new Configuration().addResource("/com/qls/domain/Person.hbm.xml");
InputStream in = Test12.class.getClassLoader().getResourceAsStream("hibernate.properties");
Properties properties = new Properties();
try {
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
configuration.setProperties(properties);
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();//一次会话
Transaction tx = session.beginTransaction();//开启事务
Person person=session.get(Person.class,24);
person.setName("人民万岁");
session.update(person);
tx.commit();//提交事务。
}
}

用hibernate.properties代替hibernate.cfg.xml配置常用的属性的更多相关文章

  1. hibernate.properties与hibernate.cfg.xml 区别

    Hibernate的数据库连接信息是从配置文件中加载的. Hibernate的配置文件有两种形式:一种是XML格式的文件,一种是properties属性文件. 一)hibernate.cfg.xml ...

  2. hibernate.properties和hibernate.cfg.xml

    hibernate.properties和hibernate.cfg.xml 博客分类: 框架技术 HibernateXMLSQLOracleJDBC     hibernate配置文件可以有两种方式 ...

  3. hibernate.cfg.xml 配置(摘录)

    配置文件中映射元素详解 对象关系的映射是用一个XML文档来说明的.映射文档可以使用工具来生成,如XDoclet,Middlegen和AndroMDA等.下面从一个映射的例子开始讲解映射元素,映射文件的 ...

  4. hibernate.cfg.xml配置(Oracle+c3p0)

    说明:数据库:Oracle10g:连接池:c3p0 结构: 一.配置hibernate.cfg.xml <?xml version="1.0" encoding=" ...

  5. hibernate.cfg.xml配置

    hibernate.hbm2ddl.auto 配置: create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这 ...

  6. hibernate4配置文件hibernate.cfg.xml配置详解

    <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configurat ...

  7. Hibernate hibernate.cfg.xml配置

    数据库连接<required>: <property name="hibernate.connection.driver_class"> com.mysql ...

  8. hibernate.cfg.xml 配置SQL server,MySQL,Oracle

    1.连接SQL server <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hiberna ...

  9. 笔记:MyBatis XML配置-Settings 完整属性表

    设置参数 描述 有效值 默认值 cacheEnabled 该配置影响的所有映射器中配置的缓存的全局开关. true | false true lazyLoadingEnabled 延迟加载的全局开关. ...

随机推荐

  1. GNU汇编 存储器访问指令

    .text .global  _start _start: mov r0,#0xff str r0,[r1] ldr r2,[r1]

  2. hive连接MySQL报错

    错误如下: [root@awen01 /usr/local/apache-hive-1.2.1-bin]#./bin/hive Logging initialized using configurat ...

  3. [Luogu3806]点分治

    询问树上是否存在距离为k[i]的点对 直接点分治把所有距离预处理出来,然后O(1)回答即可 Code #include <cstdio> #include <algorithm> ...

  4. python正则表达式02--findall()和search()方法区别,group()方法

    import re st = 'asxxixxsaefxxlovexxsdwdxxyouxxde' #search()和 findall()的区别 a = re.search('xx(.*?)xxsa ...

  5. FastJson 打Release 包解析失败

    debug 的时候,fastJson 解析数据正常.但是打了release 的时候,解析的List 总是null. 找了半天,发现,是fastJson 是对泛型有问题. 解决办法: -keepattr ...

  6. Win Server 8中的利器:微软在线备份服务

    微软在Windows Server 8中添加在线备份服务了?你一定以为我在开玩笑,是吧?但是微软确实这么做了.     微软在Windows Server 8中添加在线备份服务了?你一定以为我在开玩笑 ...

  7. <<程序猿健康指南>> 笔记

    序言: 长时间对着电脑工 作.一天下来基本不怎么走动的人,患高血压及 2 型糖尿病的风险远高于其他人群, 这两种疾病会对人体的健康产生长久的严重影响,还会增加心脏病及中风的几率. 前言: 阿米什人(A ...

  8. 剑指Offer - 九度1505 - 两个链表的第一个公共结点

    剑指Offer - 九度1505 - 两个链表的第一个公共结点2013-11-24 20:09 题目描述: 输入两个链表,找出它们的第一个公共结点. 输入: 输入可能包含多个测试样例.对于每个测试案例 ...

  9. CS/BS架构的特点

    CS架构 优点: 1.有独立的客户端,安全性高 2.大部分业务都在客户端实现,可以实现很复杂的业务 缺点: 1.对环境要求高,需要安装客户端,推广速度慢 2.需要专门前后台的开发团队,维护成本高 B/ ...

  10. ASP.NET Core 2.1 源码学习之 Options[3]:IOptionsMonitor 【转】

    原文链接:https://www.cnblogs.com/RainingNight/p/strongly-typed-options-ioptions-monitor-in-asp-net-core. ...