JPA-Hibernate persistence.xml配置

本文详细介绍了如何在Java持久化API (JPA) 中配置与MySQL数据库的连接设置,包括数据库驱动、用户名、密码及URL等关键参数,并展示了如何启用SQL日志输出。
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/myerp?useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>

转载于:https://www.cnblogs.com/mituxiaogaoyang/p/8393079.html

JPA 中,`persistence.xml` 配置文件可以用来生成 SQL 和详细日志。以下是一个示例配置,展示了如何配置 `persistence.xml` 以实现这一目的: ```xml <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL"> <properties> <!-- 数据库连接配置 --> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.password" value=""/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/myerp?useUnicode=true&characterEncoding=UTF-8"/> <!-- 显示 SQL 语句 --> <property name="hibernate.show_sql" value="true" /> <!-- 格式化 SQL 语句 --> <property name="hibernate.format_sql" value="true" /> <!-- 输出 SQL 参数值 --> <property name="eclipselink.logging.parameters" value="true"/> <!-- 日志级别配置 --> <property name="eclipselink.logging.level" value="FINE" /> <!-- 日志类别配置,可根据需要调整 --> <property name="eclipselink.logging.level.sql" value="FINE" /> </properties> </persistence-unit> </persistence> ``` 上述配置中,`hibernate.show_sql` 和 `hibernate.format_sql` 用于显示和格式化 SQL 语句,`eclipselink.logging.parameters` 用于输出 SQL 参数值。`eclipselink.logging.level` 和 `eclipselink.logging.level.sql` 用于配置日志级别,`FINE` 级别可以提供较为详细的日志信息。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值