spring boot(三)數據訪問

spring boot的數據訪問redis

    spring data是spring用來解決數據訪問問題的的一攬子解決方案,spring data是一個傘形項目,包含了大量的關係型和非關係型數據庫的訪問解決方案。包含的子項目:   spring

    spring data JPA;sql

    spring data MongoDB數據庫

    spring data RESTjson

    spring data Elasticsearch等等等api

   

    spring data爲咱們使用統一的api對上述的存儲技術進行支持,這是spring經過spring data common項目實現的,它是上述各個項目的依賴。spring data common的一個重要概念:spring data repository抽象。抽象的根接口是repository接口。它的子接口crudRepository定義了CRUD的相關操做(如findAll,save,saveAndFlush等)。crudRepository的子接口PagingAndSortingRepository定義了排序與分頁的操做。ide

不一樣的數據訪問技術提供了不一樣的repository實現如spring data JPA是jpaRepository。spa


spring data JPAhibernate

    是對ORM(領域模型和數據庫表映射)技術提供的標準規範。所謂規範是隻定義標準規則(如註解,藉口),不提供實現。實現能夠由hibernate等軟件提供商實現。orm

    1.定義一個繼承jpaRepository的接口,這意味着默認已經有了findAll,save,saveAndFlush等這些方法;(如PersonRepository)

    2.經過@EnableJpaRepositories(「數據層所在包」)開啓Spring data JPA支持,

    3.定義查詢方法,能夠根據關鍵字(And,Or,OrderBy等)定義;也能夠@NamedQuery或@Query自定義sql語句;

    4.實體類加上@Entity註解,指明是一個和數據庫表映射的實體類(如Person)

    5.運行:Person p = PersonRepository.findByNameAndAddress(name,address);


非關係型數據庫

    MongoDB

    mongoDB是基於文檔(document)的存儲型數據庫。

    spring data mongoDB提供了 對象/文檔 註解:

        @Document:映射領域對象與mongoDB的一個文檔

        @Id:映射當前屬性是id

        @DbRef:當前屬性將參考其餘文檔

        @Field:爲文檔的屬性定義名稱

        @version:將當前屬性做爲版本

    eg. @Document//映射領域模型和mongoDB的文檔

            public class Person{

                    @Id//代表這個屬性爲文檔id

                    private String id;

                    private String name;

                    @Field(「locs」)//此屬性在文檔中的名稱爲locs

                    private Collection<Location> locations = new LinkedHashSet<Location>();

                }

            public interface PersonRepository extends MongoRepository<Person,String>{

            }


Redis

    基於鍵值對的內存數據存儲。

    spring Data redis爲咱們提供了reidsTemplate和StringReidsTemplate兩種模板來進行數據操做,其中StringReidsTemplate是隻針對鍵值都是字符型的數據進行操做。template提供的主要數據訪問方法:

         opsForValue()操做只有簡單屬性的數據

            opsForList()含list的數據

            opsForSet()含set的數據

            opsForZSet()含有zset(有序的set)數據

            opsForHash()含hash的數據

    當咱們的數據存到redis時,key和value都是經過spring提供的serializer序列化到數據庫的。

            ValueOperates<String,String> stringRedisTemplate = redisTemplate.opsForValue();

            stringRedisTemplate.set(「test」,」test」,10,TimeUnit.SECOND);

            String value = stringRedisTemplate.get(「test」);

            stringRedisTemplate.delete(「test」); //刪除某個鍵

            redisTemplate.opsForSet().add(「testSet」,」1」);

            Set<String> members = redisTemplate.opsForSet().members(「testSet」);]

            … … 


spring data REST

    spring data REST支持將Spring data JPA等的repository自動轉換爲REST服務。

    使用GET請求直接訪問:HTTP://localhost:8080/persons,獲得列表數據json格式;HTTP://localhost:8080/persons/7,獲得第7條數據。

    節點路徑爲HTTP://localhost:8080/persons,是spring data REST默認的規則,在實體類以後加s造成路徑。

    對映射域名進行修改:在Repository的實現類上經過@RepositoryRestResource註解實現。

    @RepositoryRestResource(path =  「people」)

    public interface PersonRepository extends jpaRepository{}

相關文章
相關標籤/搜索