用hibernate.properties代替hibernate.cfg.xml配置常用的属性
我们使用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配置常用的属性的更多相关文章
- hibernate.properties与hibernate.cfg.xml 区别
Hibernate的数据库连接信息是从配置文件中加载的. Hibernate的配置文件有两种形式:一种是XML格式的文件,一种是properties属性文件. 一)hibernate.cfg.xml ...
- hibernate.properties和hibernate.cfg.xml
hibernate.properties和hibernate.cfg.xml 博客分类: 框架技术 HibernateXMLSQLOracleJDBC hibernate配置文件可以有两种方式 ...
- hibernate.cfg.xml 配置(摘录)
配置文件中映射元素详解 对象关系的映射是用一个XML文档来说明的.映射文档可以使用工具来生成,如XDoclet,Middlegen和AndroMDA等.下面从一个映射的例子开始讲解映射元素,映射文件的 ...
- hibernate.cfg.xml配置(Oracle+c3p0)
说明:数据库:Oracle10g:连接池:c3p0 结构: 一.配置hibernate.cfg.xml <?xml version="1.0" encoding=" ...
- hibernate.cfg.xml配置
hibernate.hbm2ddl.auto 配置: create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这 ...
- hibernate4配置文件hibernate.cfg.xml配置详解
<?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configurat ...
- Hibernate hibernate.cfg.xml配置
数据库连接<required>: <property name="hibernate.connection.driver_class"> com.mysql ...
- hibernate.cfg.xml 配置SQL server,MySQL,Oracle
1.连接SQL server <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hiberna ...
- 笔记:MyBatis XML配置-Settings 完整属性表
设置参数 描述 有效值 默认值 cacheEnabled 该配置影响的所有映射器中配置的缓存的全局开关. true | false true lazyLoadingEnabled 延迟加载的全局开关. ...
随机推荐
- exa命令详解
exa 是 ls 文件列表命令现代化替代品. 官网:https://the.exa.website/ GitHub:https://github.com/ogham/exa 后续整理中……
- python中字符串编码方式小结
Python2中字符串的类型有两种:str和unicode,其中unicode是统一编码方式,它使得字符跟二进制是一一对应的,因此所有其他编码的encode都从unicode开始,而其他编码方式按照相 ...
- ES6对象的扩展及新增方法
1.属性的简洁表示法 ES6允许直接写入变量和函数,作为对象的属性和方法.这样的书写更加简洁. const foo = 'bar'; const baz = {foo}; baz//{foo:'bar ...
- SqlServer2008/2005数据库日志收缩
1.SQL2008数据库USE [master]GOALTER DATABASE 数据库名称 SET RECOVERY SIMPLE WITH NO_WAITALTER DATABASE 数据库名称 ...
- php扩展开发-MINFO
我们在用PHPinfo函数或命令行的php -i命令查看php环境相关的信息,当我们开发完成一个自己的扩展,除非这个扩展就是你自己所使用,否则你就需要对扩展进行相关的介绍,或者显示扩展用到的ini配置 ...
- 安装python 第三方库遇到的安装问题 microsoft visual studio c++ 10.0 is required,Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
问题一: microsoft visual studio c++ 10.0 is required 安装scrapy时候出现需要vc c++ 10,有时安装其他也会有. 解决方法:安装vc 2010, ...
- Apache服务配置
Apache 1.安装Apache服务 第1步:把光盘设备中的系统镜像挂载到/media/cdrom目录. [root@zhangjh ~]# mkdir -p /media/cdrom/ [root ...
- 10,before_request 和 after_request
Flask我们已经学习很多基础知识了,现在有一个问题 我们现在有一个 Flask 程序其中有3个路由和视图函数,如下: from flask import Flask app = Flask(__na ...
- WPF制作的小时钟
原文:WPF制作的小时钟 周末无事, 看到WEB QQ上的小时钟挺可爱的, 于是寻思着用WPF模仿着做一个. 先看下WEB QQ的图: 打开VS, 开始动工. 建立好项目后, 面对一个空荡荡的页面, ...
- 用(bootstrap)Handsontable做表格,手动实现数据排序
商品graph帐票时,用(bootstrap)Handsontable做表格,手动实现数据排序待解决的问题: 若使用控件本身的排序,必须指定colHead,colHead不能被copy,若想表头被co ...