Hibernate中配置文件中设置数据库信息

来源:互联网 发布:vim教程 知乎 编辑:程序博客网 时间:2024/06/12 23:44

Hibernate中配置文件中设置数据库信息

在对数据库进行操作时,经常会遇到乱码的问题,往数据库中存入汉字时会存不进去,乱码!所以当建立所有文件时一定要统一编码,和数据库以及表的编码要一致。这里以UTF-8为例,展示一下Hibernate配置文件中的乱码问题的处理方法。

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!-- 第一部分: 配置数据库信息 --><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><!-- 连接数据库,预防乱码,加参数第一种方式 --><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8</property><!--第二种方式<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=utf-8</property>--><!-- 第三种方法 <property name="connection.useUnicode">true</property>  <property name="connection.characterEncoding">UTF-8</property>--><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">123456</property><!-- 第二部分: 配置hibernate信息  可选的--><!-- 输出底层sql语句 --><property name="hibernate.show_sql">true</property><!-- 输出底层sql语句格式 --><property name="hibernate.format_sql">true</property><!-- hibernate帮创建表,需要配置之后 update: 如果已经有表,更新,如果没有,创建--><property name="hibernate.hbm2ddl.auto">update</property><!-- 配置数据库方言在mysql里面实现分页 关键字 limit,只能使用mysql里面在oracle数据库,实现分页rownum让hibernate框架识别不同数据库的自己特有的语句 --><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><!-- 第三部分: 把映射文件放到核心配置文件中 必须的--><mapping resource="com/dzu/student/entity/Student.hbm.xml"/></session-factory></hibernate-configuration>


0 0