Spring源碼,SpringData環境搭建代碼編寫

 2023-11-19 阅读 27 评论 0

摘要:首先我們在前面的兩節已經了解了SpringData是干什么用的?那我們從這節開始我們就開始編碼測試SpringData. Spring源碼?1:首先我們從配置文件開始,我們首先需要寫一個連接數據庫的文件db.properties: jdbc.user=root jdbc.password=root jdbc.driverCla

首先我們在前面的兩節已經了解了SpringData是干什么用的?那我們從這節開始我們就開始編碼測試SpringData.

Spring源碼?1:首先我們從配置文件開始,我們首先需要寫一個連接數據庫的文件db.properties:

jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc\:mysql\:///jpa

2:因為SpringData 是Spring的一個子項目,我們需要配置Spring的配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 配置自動掃描的包 --><context:component-scan base-package="com.fxr.springdata"></context:component-scan><!-- 1. 配置數據源 --><context:property-placeholder location="classpath:db.properties"/><bean id="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property>	<property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><!-- 配置其他屬性 --></bean><!-- 2. 配置 JPA 的 EntityManagerFactory --><bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="jpaVendorAdapter"><bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean></property><property name="packagesToScan" value="com.fxr.springdata"></property><property name="jpaProperties"><props><!-- 二級緩存相關 --><!--  <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop><prop key="net.sf.ehcache.configurationResourceName">ehcache-hibernate.xml</prop>--><!-- 生成的數據表的列的映射策略 --><prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop><!-- hibernate 基本屬性 --><prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.hbm2ddl.auto">update</prop></props></property></bean><!-- 3. 配置事務管理器 --><bean id="transactionManager"class="org.springframework.orm.jpa.JpaTransactionManager"><property name="entityManagerFactory" ref="entityManagerFactory"></property>	</bean><!-- 4. 配置支持注解的事務 --><tx:annotation-driven transaction-manager="transactionManager"/><!-- 5. 配置 SpringData --><!-- 加入  jpa 的命名空間 --><!-- base-package: 掃描 Repository Bean 所在的 package --><jpa:repositories base-package="com.fxr.springdata"entity-manager-factory-ref="entityManagerFactory"></jpa:repositories></beans>

jdk開發環境。3:首先我們需要測試配置的文件對不對,我們測試配置的數據源對不對

package com.fxr.test;import java.sql.SQLException;import javax.sql.DataSource;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.fxr.springdata.PersonRepsotory;public class SpringDataTest {private ApplicationContext ctx = null;private PersonRepsotory personRepsotory;{ctx = new ClassPathXmlApplicationContext("applicationContext.xml");personRepsotory = ctx.getBean(PersonRepsotory.class);}//測試配置的數據源成功沒有成功@Testpublic void testDataSource() throws SQLException{DataSource dataSource = ctx.getBean(DataSource.class);System.out.println(dataSource.getConnection());}}

4:如果運行過給我們自動創建了數據表,就說明我們的數據源已經配置成功。

轉載于:https://www.cnblogs.com/airycode/p/6535211.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/181825.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息