Spring @Repository批注

Spring @Repository annotation is used to indicate that the class provides the mechanism for storage, retrieval, search, update and delete operation on objects.

Spring @Repository批注用于指示该类提供了对对象进行存储,检索,搜索,更新和删除操作的机制。

Spring @Repository批注 (Spring @Repository Annotation)

Spring Repository annotation is a specialization of @Component annotation, so Spring Repository classes are autodetected by spring framework through classpath scanning.

Spring Repository注释是@Component注释的一种特殊形式,因此Spring 框架会通过类路径扫描自动检测Spring Repository类。

Spring Repository is very close to DAO pattern where DAO classes are responsible for providing CRUD operations on database tables. However, if you are using Spring Data for managing database operations, then you should use Spring Data Repository interface.

Spring Repository非常接近DAO模式,其中DAO类负责对数据库表提供CRUD操作。 但是,如果您使用Spring Data来管理数据库操作,则应该使用Spring Data Repository接口。

Spring存储库示例 (Spring Repository Example)

Let’s look at a simple example where we will create a Spring Repository class. We will not use database operations, rather we will provide a repository for an Object.

让我们看一个简单的示例,在该示例中,我们将创建一个Spring Repository类。 我们将不使用数据库操作,而是为对象提供一个存储库。

Create a maven project in Eclipse or any other IDE you use, then add spring core dependency.

在Eclipse或您使用的任何其他IDE中创建一个maven项目,然后添加spring核心依赖项。

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>5.0.6.RELEASE</version>
</dependency>

Below image shows our final project structure in Eclipse.

下图显示了我们在Eclipse中的最终项目结构。

Let’s create the model class for which we will implement a spring repository.

让我们创建一个模型类,为其实现一个Spring存储库。

package com.journaldev.spring.model;

public class Employee {

	private int id;
	private String name;
	private String jobTitle;

	public Employee() {
	}

	public Employee(int i, String n, String jt) {
		this.id = i;
		this.name = n;
		this.jobTitle = jt;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getJobTitle() {
		return jobTitle;
	}

	public void setJobTitle(String jobTitle) {
		this.jobTitle = jobTitle;
	}

	@Override
	public String toString() {
		return id + "," + name + "," + jobTitle;
	}
}

Before we implement Repository class, I have created a generic ObjectRepository interface to provide the contract for our repository class to implement.

在实现Repository类之前,我已经创建了一个通用的ObjectRepository接口,以为我们的存储库类提供合同。

package com.journaldev.spring.repository;

public interface ObjectRepository<T> {

	public void store(T t);

	public T retrieve(int id);

	public T search(String name);

	public T delete(int id);
}

I am using Generics here, it’s a powerful technology to provide loosely coupled contract for the applications to implement.

我在这里使用泛型,这是一项强大的技术,可为应用程序提供松散耦合的合同。

Now let’s look at our Repository class implementation.

现在让我们看一下我们的Repository类的实现。

package com.journaldev.spring.repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.journaldev.spring.model.Employee;

@Repository
public class EmployeeRepository implements ObjectRepository<Employee> {

	private Map<Integer, Employee> repository;

	public EmployeeRepository() {
		this.repository = new HashMap<>();
	}

	@Override
	public void store(Employee emp) {
		repository.put(emp.getId(), emp);
	}

	@Override
	public Employee retrieve(int id) {
		return repository.get(id);
	}

	@Override
	public Employee search(String name) {
		Collection<Employee> emps = repository.values();
		for (Employee emp : emps) {
			if (emp.getName().equalsIgnoreCase(name))
				return emp;
		}
		return null;
	}

	@Override
	public Employee delete(int id) {
		Employee e = repository.get(id);
		this.repository.remove(id);
		return e;
	}

}

Note that I am using an in-memory Map to store the object data, you can use any other mechanisms too.

请注意,我使用内存中的Map来存储对象数据,您也可以使用任何其他机制。

Spring存储库测试 (Spring Repository Test)

Our Spring Repository is ready, let’s create a main class and test it out.

我们的Spring信息库已经准备好,让我们创建一个主类并进行测试。

package com.journaldev.spring;

import java.sql.SQLException;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.journaldev.spring.model.Employee;
import com.journaldev.spring.repository.EmployeeRepository;

public class SpringMainClass {

	public static void main(String[] args) throws SQLException {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.scan("com.journaldev.spring");
		context.refresh();

		EmployeeRepository repository = context.getBean(EmployeeRepository.class);

		// store
		repository.store(new Employee(1, "Pankaj", "CEO"));
		repository.store(new Employee(2, "Anupam", "Editor"));
		repository.store(new Employee(3, "Meghna", "CFO"));

		// retrieve
		Employee emp = repository.retrieve(1);
		System.out.println(emp);

		// search
		Employee cfo = repository.search("Meghna");
		System.out.println(cfo);

		// delete
		Employee editor = repository.delete(2);
		System.out.println(editor);

		// close the spring context
		context.close();
	}

}

Just run the class as Java Application and you should get following output.

只需将类作为Java应用程序运行,您将获得以下输出。

1,Pankaj,CEO
3,Meghna,CFO
2,Anupam,Editor
GitHub Repository. GitHub Repository下载示例代码。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/21460/spring-repository-annotation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值