【发布时间】:2023-04-08 07:12:01
【问题描述】:
我正在编写一个 spring webapp,它使用带有 jdbc 和 jpa/hibernate 的 spring 安全性(我是 spring 的 100% 新手)。我通过在“dataSource”bean 中配置数据库以实现 spring 安全性并在 persistence.xml 中重复它来使其工作。我尝试在持久性中使用数据源,但我没有运气。如何在persistence.xml 中重用我的数据源配置?我的工作配置是:
datasource.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://host:3306/db"/>
<property name="username" value="user"/>
<property name="password" value="pass"/>
</bean>
persistence.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="GCGastosPersistence">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.mypackage.MyFirstClass</class>
<class>com.mypackage.MyAnotherClass</class>
<properties>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="javax.persistence.jdbc.password" value="pass"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://host:3306/db"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.schema-generation.database.action" value="none"/>
<property name="hibernate.connection.charset" value="utf8"/>
<property name="hibernate.connection.charsetEncoding" value="utf8"/>
<property name="hibernate.connection.useUnicode" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
</properties>
</persistence-unit>
感谢任何提示。
【问题讨论】:
标签: java spring hibernate jpa spring-security