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?那是不可能滴!)

--附上目录结构

---