Spring with Database

Spring with Database


Questions

  • What is spring data?
  • How to configure the specific database?

Spring Data

Spring Data’s mission is to provide a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store.

The most important thing of spring data is repository.

Repository

The central interface in the Spring Data repository abstraction is Repository.
Two important subinterfaces:
- CrudRepository: provide standard CRUD methods.
- PagingAndSortingRepository: provide findAll() methods.

One way is extending them immediately:

interface UserRepository extends CrudRepository<User, Long> {

  long countByLastname(String lastname);
}

Another way is selectively exposing CRUD methods:

@NoRepositoryBean
interface MyBaseRepository<T, ID extends Serializable> extends Repository<T, ID> {

  Optional<T> findById(ID id);

  <S extends T> S save(S entity);
}

interface UserRepository extends MyBaseRepository<User, Long> {
  User findByEmailAddress(EmailAddress emailAddress);
}

Configure a DataSource

See the “Data Access” how-to.

Access Data with MongoDB

Before ship the “Access Data with MongoDB”, you need:

  1. Install the mongodb
  2. Open cmd.exe and input:
# run the sever on localhost
mongod

# enter the mongo shell
mongo

Reference

The “mongodb manual”
REST API: Java Spring Boot and MongoDB

猜你喜欢

转载自blog.csdn.net/m0_37867653/article/details/82503341
今日推荐