springdata 1-- 初学spring的笔记-读尚硅谷视频有感
来源:互联网 发布:垃圾邮件分类算法 编辑:程序博客网 时间:2024/05/29 03:26
---开头说明:仅为个人记录笔记所用,无商用意图。
下一节说明springdata的具体方法策略
Springdata
Spring的一个子项目。用于简化数据库访问,支持NOSQL 和 关系数据存储,其主要目标是使数据库的访问变得方便快捷。
项目支持的NoSQL存储:
-MongDB(文档数据库)
-Neo4j(图形数据库)
-Redis(键/值存储)
-Hbase(列族数据库)
项目支持的关系数据存储技术:jdbc,jpa
整合Spring Springdata jpa四个步骤
1.配置Spring整合JPA 《----》2.在Spring配置文件中配置Springdata《---》3.声明持久层的接口,继承自Repository《---》4.在接口中声明与需要的方法
项目创建:
1.创建java项目
2.加入包
使用了spring4+hibernate4+c3p0+jpa+mysql+springdata-commons+springdata-jpa+slf4j
3.创建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.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 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.atguigu.springdata"></property>
<!-- 2.1技术支持配置(hibernate) -->
<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 -->
<!-- 5.1加入jpa命名空间(好像得在spring下,我的就是一个普通xml,没有namespaces) -->
<!-- base-package:扫描RepositoryBean所在的package-->
<jpa:repositories base-package="com.atguigu.springdata"
entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>
</beans>
4.创建单元测试类,检测配置文件是否出错,推荐完成一项功能就进行测试,这样容易快速找到错误所在(没有bug?那是不可能滴!)
--附上目录结构
---
- springdata 1-- 初学spring的笔记-读尚硅谷视频有感
- 尚硅谷Spring学习笔记(一)
- javaweb 尚硅谷笔记
- 6.Spring学习笔记_Bean之间的关系 (by尚硅谷_佟刚)
- 7.Spring学习笔记_Bean的作用域(by尚硅谷_佟刚)
- 10.Spring学习笔记_管理Bean的生命周期(by尚硅谷_佟刚)
- 18.Spring学习笔记_切面的优先级(by尚硅谷_佟刚)
- 尚硅谷Webservice学习笔记
- 尚硅谷Java基础笔记
- 尚硅谷学习笔记----国际化
- 尚硅谷_JavaScript_学习笔记
- 尚硅谷_jQuery_学习笔记
- 尚硅谷_Ajax_学习笔记
- 尚硅谷-Maven学习笔记
- 20.Spring学习笔记_基于配置文件的方式来配置 AOP(by尚硅谷_佟刚)
- 尚硅谷学习笔记--Struts2 类型转换(1)
- 尚硅谷Java视频教程,全套java视频
- JDBC的学习--尚硅谷
- 如何快速判断一个数是不是2的幂、3的幂、4的幂
- [Leetcode] 535. Encode and Decode TinyURL 解题报告
- MySQL优化
- 代理推广小程序思路整理
- FL Studio 12基础教程+软件安装包
- springdata 1-- 初学spring的笔记-读尚硅谷视频有感
- SQLLDR 命令
- 如何将RDD或者MLLib矩阵zhuanzhi
- linux 创建连接命令 ln -s 软连接
- opencv_tutorial_code学习——图像滤波与风格化
- SparkMLlib的数据类型讲解
- Android转后台(一)——IntelliJ IDEA配置环境运行APP后台程序
- 使用<context:property-placeholder>标签导入多个properties文件
- 在java应用程序中计划重复执行的任务