使用 Spring @Repository 注释来表示该类提供了存储、检索、搜索、更新和删除对象操作的机制。
春天 @Repository 注释
Spring Repository 注释是由 @Component注释的专业化,因此 Spring Repository 类通过 Classpath 扫描自动检测到 spring framework 以便在数据库表上进行 CRUD 操作。但是,如果你正在使用 Spring Data 来管理数据库操作,那么你应该使用 Spring Data Repository 接口。
春季仓库示例
让我们来看看一个简单的例子,在那里我们将创建一个 Spring Repository 类. 我们不会使用数据库操作,而是为一个 Object 提供一个存储库. 在 Eclipse 或您使用的任何其他 IDE 中创建一个 Maven 项目,然后添加 Spring Core 依赖性。
1<dependency>
2 <groupId>org.springframework</groupId>
3 <artifactId>spring-context</artifactId>
4 <version>5.0.6.RELEASE</version>
5</dependency>
Below image shows our final project structure in Eclipse. Let's create the model class for which we will implement a spring repository.
1package com.journaldev.spring.model;
2
3public class Employee {
4
5 private int id;
6 private String name;
7 private String jobTitle;
8
9 public Employee() {
10 }
11
12 public Employee(int i, String n, String jt) {
13 this.id = i;
14 this.name = n;
15 this.jobTitle = jt;
16 }
17
18 public int getId() {
19 return id;
20 }
21
22 public void setId(int id) {
23 this.id = id;
24 }
25
26 public String getName() {
27 return name;
28 }
29
30 public void setName(String name) {
31 this.name = name;
32 }
33
34 public String getJobTitle() {
35 return jobTitle;
36 }
37
38 public void setJobTitle(String jobTitle) {
39 this.jobTitle = jobTitle;
40 }
41
42 @Override
43 public String toString() {
44 return id + "," + name + "," + jobTitle;
45 }
46}
在我们实施 Repository 类之前,我创建了一种通用ObjectRepository
接口,以提供我们要实施的 repository 类的合同。
1package com.journaldev.spring.repository;
2
3public interface ObjectRepository<T> {
4
5 public void store(T t);
6
7 public T retrieve(int id);
8
9 public T search(String name);
10
11 public T delete(int id);
12}
我在这里使用 Generics,这是一个强大的技术,可以为应用程序提供松散的合并合约,现在让我们看看我们的仓库类实现。
1package com.journaldev.spring.repository;
2
3import java.util.Collection;
4import java.util.HashMap;
5import java.util.Map;
6
7import org.springframework.stereotype.Repository;
8
9import com.journaldev.spring.model.Employee;
10
11@Repository
12public class EmployeeRepository implements ObjectRepository<Employee> {
13
14 private Map<Integer, Employee> repository;
15
16 public EmployeeRepository() {
17 this.repository = new HashMap<>();
18 }
19
20 @Override
21 public void store(Employee emp) {
22 repository.put(emp.getId(), emp);
23 }
24
25 @Override
26 public Employee retrieve(int id) {
27 return repository.get(id);
28 }
29
30 @Override
31 public Employee search(String name) {
32 Collection<Employee> emps = repository.values();
33 for (Employee emp : emps) {
34 if (emp.getName().equalsIgnoreCase(name))
35 return emp;
36 }
37 return null;
38 }
39
40 @Override
41 public Employee delete(int id) {
42 Employee e = repository.get(id);
43 this.repository.remove(id);
44 return e;
45 }
46
47}
请注意,我正在使用内存 地图来存储对象数据,您也可以使用任何其他机制。
春季检测实验室
我们的春季仓库已经准备好了,让我们创建一个主类,并测试它。
1package com.journaldev.spring;
2
3import java.sql.SQLException;
4
5import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6
7import com.journaldev.spring.model.Employee;
8import com.journaldev.spring.repository.EmployeeRepository;
9
10public class SpringMainClass {
11
12 public static void main(String[] args) throws SQLException {
13 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
14 context.scan("com.journaldev.spring");
15 context.refresh();
16
17 EmployeeRepository repository = context.getBean(EmployeeRepository.class);
18
19 // store
20 repository.store(new Employee(1, "Pankaj", "CEO"));
21 repository.store(new Employee(2, "Anupam", "Editor"));
22 repository.store(new Employee(3, "Meghna", "CFO"));
23
24 // retrieve
25 Employee emp = repository.retrieve(1);
26 System.out.println(emp);
27
28 // search
29 Employee cfo = repository.search("Meghna");
30 System.out.println(cfo);
31
32 // delete
33 Employee editor = repository.delete(2);
34 System.out.println(editor);
35
36 // close the spring context
37 context.close();
38 }
39
40}
只需运行类作为Java应用程序,你应该得到以下输出。
11,Pankaj,CEO
23,Meghna,CFO
32,Anupam,Editor
您可以从我们的 GitHub 存储库下载示例代码。
引用: API Doc