spring中配置MySql数据源,怎样配置数据库信息

spring中配置MySql数据源,怎样配置数据库信息,第1张

1:这是整合hibernate4是配置的数据源等。

<bean id="dataSource"

class="org.springframework.jndi.JndiObjectFactoryBean"

p:jndiName="java:comp/env/jdbc/estore"/>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"

p:dataSource-ref="dataSource"

p:mappingLocations="classpath:cn/**/entity/*.hbm.xml">

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

<prop key="hibernate.show_sql">true</prop>

<prop key="hibernate.format_sql">true</prop>

</props>

</property>

</bean>

2:这是spring自己的springData框架配置数据源

新建一个persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/persistence

http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name="myJPA" transaction-type="RESOURCE_LOCAL">

<provider>org.hibernate.ejb.HibernatePersistence</provider>

<properties>

<!--配置Hibernate方言 -->

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />

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

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

<!--配置数据库用户名 -->

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

<!--配置数据库密码 -->

<property name="hibernate.connection.password" value="mysql" />

<!--配置数据库url -->

<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/malajava?useUnicode=true&characterEncoding=UTF-8" />

<!--设置外连接抓取树的最大深度 -->

<property name="hibernate.max_fetch_depth" value="3" />

<property name="hibernate.show_sql" value="true" />

<property name="hibernate.format_sql" value="true" />

<property name="javax.persistence.validation.mode" value="none"/>

</properties>

</persistence-unit>

</persistence>

望采纳!谢谢!

以前使用hibernate的时候,只知道逆向使用,也就是只知道先建好表,然后由表生成映射文件和类。

经过上一次项目,从师父那知道了写好类和映射文件,然后建表。

这一过程其实蛮简单的,只要在hibernate.cfg.xml配置文件中或者是applicationContext.xml的sessionFactory Bean里面添加一个属性

<prop key="hibernate.hbm2ddl.auto">create</prop>

今天尝试自己建表,结果去数据库中查找的时候,居然一直没有建成功。

最后终于知道是因为在映射文件中添加了两个属性导致的,就是schema和catalog。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7287250.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-03
下一篇 2023-04-03

发表评论

登录后才能评论

评论列表(0条)

保存