1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > hibernate4 mysql配置文件_Hibernate的数据库连接信息配置文件hibernate.properties

hibernate4 mysql配置文件_Hibernate的数据库连接信息配置文件hibernate.properties

时间:2019-07-04 21:51:51

相关推荐

hibernate4 mysql配置文件_Hibernate的数据库连接信息配置文件hibernate.properties

Hibernate的数据库连接信息配置文件hibernate.properties与hibernate.cfg.xml

区别

一、问题提出

测试运行一个“MyEclipse、Struts2.1、Hibernate4.1”工程时,发现“hibernate.properties

not found”,但程序仍然能正常运行!

*******************************************************

#####@@@@@MySql

十月 17, 8:16:13 上午org.mon.Version

INFO: HCANN000001:

Hibernate Commons Annotations {4.0.1.Final}

十月 17, 8:16:13 上午org.hibernate.Version logVersion

INFO: HHH000412:

Hibernate Core {4.1.4.Final}

十月 17, 8:16:13 上午org.hibernate.cfg.Environment

INFO: HHH000206:

hibernate.properties not found

十月 17, 8:16:13 上午org.hibernate.cfg.Environment

buildBytecodeProvider

INFO: HHH000021:

Bytecode provider name : javassist

十月 17, 8:16:13 上午org.hibernate.cfg.Configuration configure

INFO: HHH000043:

Configuring from resource:

/hibernate.cfg.xml

十月 17, 8:16:13 上午org.hibernate.cfg.Configuration

getConfigurationInputStream

INFO: HHH000040:

Configuration resource: /hibernate.cfg.xml

十月 17, 8:16:14 上午org.hibernate.cfg.Configuration

addResource

INFO: HHH000221:

Reading mappings from resource:

com/hibernate/model/Person.hbm.xml

十月 17, 8:16:14 上午org.hibernate.cfg.Configuration

doConfigure

INFO: HHH000041:

Configured SessionFactory: null

十月 17, 8:16:15 上午org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl

configure

INFO: HHH000402:

Using Hibernate built-in connection pool (not for production

use!)

十月 17, 8:16:15 上午org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl

configure

INFO: HHH000115:

Hibernate connection pool size: 20

十月 17, 8:16:15 上午org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl

configure

INFO: HHH000006:

Autocommit mode: false

十月 17, 8:16:15 上午org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl

configure

INFO: HHH000401:

using driver [com.mysql.jdbc.Driver] at URL

[jdbc:mysql://localhost:3306/my_sshtest]

十月 17, 8:16:15 上午org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl

configure

INFO: HHH000046:

Connection properties: {user=root, password=****}

十月 17, 8:16:15 上午org.hibernate.dialect.Dialect

INFO: HHH000400:

Using dialect: org.hibernate.dialect.MySQLDialect

十月 17, 8:16:16 上午

org.hibernate.engine.transaction.internal.TransactionFactoryInitiator

initiateService

INFO: HHH000399:

Using default transaction strategy (direct JDBC

transactions)

十月 17, 8:16:16 上午org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory

INFO: HHH000397:

Using ASTQueryTranslatorFactory

十月 17, 8:16:20 上午org.piler.TldLocationsCache

tldScanJar

信息: At least one JAR was scanned for TLDs yet

contained no TLDs. Enable debug logging for this logger for a

complete list of JARs that were scanned but no TLDs were found in

them. Skipping unneeded JARs during scanning can improve startup

time and JSP compilation time.

******************************************************

Hibernate的数据库连接信息是从配置文件中加载的。

Hibernate的配置文件有两种形式:一种是XML格式的文件,一种是properties属性文件。properties形式的配置文件和XML格式的配置文件可以同时使用。当同时使用两种类型的配置文件时,XML配置文件中的设置会覆盖properties配置文件的相同的属性。

二、hibernate.cfg.xml

XML格式的配置文件中,除了基本的Hibernate配置信息,还可以指定具体的持久化类的映射文件,这可以避免将持久化类的配置文件硬编码在程序中。XML格式的配置文件的默认文件名为hibernate.cfg.xml。位置:src/hibernate.cfg.xml。

示例如下所示:

<?xml version='1.0'

encoding='UTF-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD

3.0//EN"

"/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!--显示执行的SQL语句-->

<property name="show_sql">true</property>

<!--连接字符串-->

<property name="connection.url">jdbc:mysql://localhost:3306/STU</property>

<!--连接数据库的用户名-->

<property

name="connection.username">root</property>

<!--数据库用户密码-->

<property

name="connection.password">root</property>

<!--数据库驱动-->

<property

name="connection.driver_class">com.mysql.jdbc.Driver</property>

<!--选择使用的方言-->

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!--映射文件

-->

<mapping resource="com/stuman/domain/Admin.hbm.xml"

/>

<!--映射文件-->

<mapping resource="com/stuman/domain/Student.hbm.xml"

/>

</session-factory>

</hibernate-configuration>

三、hibernate.properties

properties形式的配置文件默认文件名是hibernate.properties,在配置文件中包含了一系列属性的配置,Hibernate将根据这些属性来连接数据库。位置:src/hibernate.properties。

配置文件内容如下所示:

#指定数据库使用的驱动类

hibernate.connection.driver_class = com.mysql.jdbc.Driver

r

#指定数据库连接串

hibernate.connection.url =

jdbc:mysql://localhost:3306/db

#指定数据库连接的用户名

hibernate.connection.username = user

#指定数据库连接的密码

hibernate.connection.password = password

#指定数据库使用的方言

hibernate.dialect =

net.sf.hibernate.dialect.MySQLDialect

#指定是否打印SQL语句

hibernate.show_sql=true

解决乱码

1、首先需要修改mysql数据库的配置文件my.ini,此文件放在mysql根目录下。在此文件下查找default-character-set属性,并将其值更改为utf8(注意:不是utf-8,也要注意大小写),这里需要将default-character-set属性全部属性的值修改为utf8。

示例:

default-character-set = utf8

提示:default-character-set属性有两个,一个在[mysql]下面,另外一个在[mysqld]下面。

2、同时创建hibernate数据库时需要显示设置数据库的编码方式为utf8。示例:

create database daycode default

charset=utf8;

3、做完这两步还是不行,需要修改hibernate的配置文件hibernate.cfg.xml,在配置文件配置hibernate.connection.url属性。示例:

jdbc:mysql://localhost:3306/daycode?useUnicode=true&characterEncoding=utf8

注意:此字符串不能写为jdbc:mysql://localhost:3306/daycode?useUnicode=true&characterEncoding=utf8,不然会出现编译错误,错误提示为将&连接符改为;。

设置这些之后乱码问题就解决了。

hibernate4 mysql配置文件_Hibernate的数据库连接信息配置文件hibernate.properties与hibernate.cfg.xml区别...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。