作者都是各自领域经过审查的专家,并撰写他们有经验的主题. 我们所有的内容都经过同行评审,并由同一领域的Toptal专家验证.
Stefan Varga's profile image

Stefan Varga

Stefan是一名合格的软件开发人员,拥有计算机科学学位,在科技行业有近十年的经验.

Expertise

Years of Experience

17

Share

人们常说Java太复杂,构建简单的应用程序需要花费太长时间. Nonetheless, Java提供了一个稳定的平台和一个非常成熟的生态系统, which makes it a wonderful option for developing robust software.

The Spring Framework, one of the many powerful frameworks in the Java ecosystem, 提供了一系列编程和配置模型,旨在简化Java中高性能和可测试应用程序的开发.

Spring Framework

In this tutorial, 我们将尝试构建一个简单的应用程序,作为数据库 software developers using Spring Framework and the Java Persistence API (JPA).

The application follows a standard MVC architecture. It will have a controller (ContractsController class), views (based on Thymeleaf templates), and a model (a Java map object). For the sake of simplicity, 在应用程序运行时,我们将使用JPA后面的内存数据库来持久化数据.

Getting Started with the Spring Framework Tutorial

要构建一个基于Spring的应用程序,我们需要使用以下构建工具之一:

  • Maven
  • Gradle

In this tutorial, we will use Maven. 如果您对这两个工具中的任何一个都不熟悉,一个简单的入门方法是下载 Spring Tool Suite. The suite is dedicated for Spring Framework, and comes with its own Eclipse based IDE.

In Spring Tool Suite, we create a new project by selecting “Spring Starter Project” from under the “File > New” menu.

Spring Framework Tutorial

一旦创建了新项目,我们将需要编辑Maven配置文件。”pom.xml”, and add the following dependencies:


	org.springframework.boot
	spring-boot-starter-web


	org.springframework.boot
	spring-boot-starter-thymeleaf


	org.springframework.boot
	spring-boot-starter-data-jpa


	com.h2database
	h2


	org.springframework.data
	spring-data-commons

These listed dependencies will load Spring Boot Web, Thymeleaf, JPA, and H2 (which will serve as our in-memory database). All necessary libraries will be pulled automatically.

Entity Classes

To be able to store information about developers and their skills, we will need to define two entity classes: “Developer” and “Skill”.

Both of these are defined as plain Java classes with some annotations. 通过在类之前添加“@Entity”,我们使它们的实例对JPA可用. 这将使在需要时更容易从持久数据存储中存储和检索实例. Additionally, “@Id”和“@GeneratedValue”注释允许我们为实体指定唯一的ID字段,并在存储在数据库中时自动生成其值.

As a developer can have many skills, 我们可以使用“@ manymany”注释定义一个简单的多对多关系.

Developer

@Entity
public class Developer {

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private long id;
	private String firstName;
	private String lastName;
	private String email;
	@ManyToMany
	private List skills;

	public Developer() {
		super();
	}

	public Developer(String firstName, String lastName, String email,
			List skills) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
		this.skills = skills;
	}

	public long getId() {
		return id;
	}

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

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public List getSkills() {
		return skills;
	}

	public void setSkills(List skills) {
		this.skills = skills;
	}

	public boolean hasSkill(Skill skill) {
		for (Skill containedSkill: getSkills()) {
			if (containedSkill.getId() == skill.getId()) {
				return true;
			}
		}
		return false;
	}

}

Skill

@Entity
public class Skill {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    private String label;
    private String description;

    public Skill() {
		super();
    }

    public Skill(String label, String description) {
		super();
		this.label = label;
		this.description = description;
	}

	public long getId() {
		return id;
	}

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

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
    
}

Repositories

使用JPA,我们可以定义一个非常有用的DeveloperRepository接口和SkillRepository接口, which allow for easy CRUD operations. 这些接口将允许我们通过简单的方法调用访问存储的开发人员和技能, such as:

  • “respository.findAll()”: returns all developers
  • “repository.findOne(id)”: returns developer with given ID

要创建这些接口,我们所需要做的就是扩展CrudRepository接口.

Developer Repository

public interface DeveloperRepository extends CrudRepository {

}

Skill Repository

public interface SkillRepository extends CrudRepository {
	public List findByLabel(String label);
}

Functionality for the the additional method “findByLabel “declared here will be provided automatically by JPA.

Controller

Next, we can work on the controller for this application. 控制器将映射请求uri以查看模板,并在两者之间执行所有必要的处理.

@Controller
public class DevelopersController {

	@Autowired
	DeveloperRepository repository;

	@Autowired
	SkillRepository skillRepository;

	@RequestMapping("/developer/{id}")
	public String developer(@PathVariable Long id, Model model) {
		model.addAttribute("developer", repository.findOne(id));
		model.addAttribute("skills", skillRepository.findAll());
		return "developer";
	}

	@RequestMapping(value="/developers",method=RequestMethod.GET)
	public String developersList(Model model) {
		model.addAttribute("developers", repository.findAll());
		return "developers";
	}

	@RequestMapping(value="/developers",method=RequestMethod.POST)
	public String developersAdd(@RequestParam String email, 
						@RequestParam String firstName, @RequestParam String lastName,模型模型){
		Developer newDeveloper = new Developer();
		newDeveloper.setEmail(email);
		newDeveloper.setFirstName(firstName);
		newDeveloper.setLastName(lastName);
		repository.save(newDeveloper);

		model.addAttribute("developer", newDeveloper);
		model.addAttribute("skills", skillRepository.findAll());
		return "redirect:/developer/" + newDeveloper.getId();
	}

	@RequestMapping(value="/developer/{id}/skills", method=RequestMethod.POST)
	公共字符串developersAddSkill(@PathVariable长id, @RequestParam长skilid,模型模型){
		Skill skill = skillRepository.findOne(skillId);
		Developer developer = repository.findOne(id);

		if (developer != null) {
			if (!developer.hasSkill(skill)) {
				developer.getSkills().add(skill);
			}
			repository.save(developer);
			model.addAttribute("developer", repository.findOne(id));
			model.addAttribute("skills", skillRepository.findAll());
			return "redirect:/developer/" + developer.getId();
		}

		model.addAttribute("developers", repository.findAll());
		return "redirect:/developers";
	}

}

uri到方法的映射是通过简单的“@RequestMapping”注释完成的. In this case, every method of the controller is mapped to a URI.

这些方法的模型参数允许将数据传递给视图. In essence, these are simple maps of keys to values.

每个控制器方法返回要用作视图的thymleaf模板的名称, or a URL in a specific pattern (“redirect:”) to redirect to. For example, 方法" developer "和" _developersList_ "返回模板的名称, 而“developersAdd”和“developersAddSkill”返回要重定向到的url.

Within the controller, “@Autowired”注释会在相应的字段中自动分配我们定义的存储库的有效实例. 这允许从控制器内部访问相关数据,而无需处理大量样板代码.

Views

最后,我们需要为要生成的视图定义一些模板. For this we are using Thymeleaf, a simple templating engine. 我们在控制器方法中使用的模型可以直接在模板中使用.e. when we enter a contract into “contract的关键字,我们将能够访问名称字段为“合同.name” from within the template.

Thymeleaf包含一些控制HTML生成的特殊元素和属性. They are very intuitive and straightforward. For example, to populate the contents of a span element with the name of a skill, 您所需要做的就是定义以下属性(假设键“skill” is defined in the model):


Similarly to set the “href” attribute of an anchor element, the special attribute “th:href” can be used.

In our application, we will need two simple templates. For clarity, 我们将跳过嵌入模板代码中的所有样式和类属性(即Bootstrap属性).

Developer List



 
	Developers database 
	


	

Developers

Name Skills
, view

First name:
Last name:
Email:

Developer Details




	Developer
	


	

Developer

Name:
Email:
Skills:
   -

Running the Server

Spring contains a boot module. This allows us to start the server easily from command line as a command line Java application:

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    DeveloperRepository developerRepository;

    @Autowired
    SkillRepository skillRepository;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

Since we are using an in-memory database, 在启动时用一些预定义的数据引导数据库是有意义的. 这样,当服务器启动并运行时,数据库中至少会有一些数据.

@Override
public void run(String... args) throws Exception {
	技能javascript =新技能(“javascript”,“javascript语言技能”);
	Skill ruby = new Skill("ruby", "Ruby language skill");
	Skill emberjs = new Skill("emberjs", "Emberjs framework");
	Skill angularjs = new Skill("angularjs", "Angularjs framework");

	skillRepository.save(javascript);
	skillRepository.save(ruby);
	skillRepository.save(emberjs);
	skillRepository.save(angularjs);

	List developers = new LinkedList();
	developers.add(new Developer("John", "Smith", "john.smith@example.com", 
			Arrays.asList(new Skill[] { javascript, ruby })));
	developers.add(new Developer("Mark", "Johnson", "mjohnson@example.com", 
			Arrays.asList(new Skill[] { emberjs, ruby })));
	developers.add(new Developer("Michael", "Williams", "michael.williams@example.com", 
			Arrays.asList(new Skill[] { angularjs, ruby })));
	developers.add(new Developer("Fred", "Miller", "f.miller@example.com", 
			Arrays.asList(new Skill[] { emberjs, angularjs, javascript })));
	developers.add(new Developer("Bob", "Brown", "brown@example.com", 
			Arrays.asList(new Skill[] { emberjs })));
	developerRepository.save(developers);
}

Conclusion

Spring is a versatile framework that allows building MVC applications. Building a simple application with Spring is quick and transparent. 应用程序还可以使用JPA轻松地与数据库集成.

The source code of this entire project is available on GitHub.

Hire a Toptal expert on this topic.
Hire Now
Stefan Varga's profile image
Stefan Varga

Located in Bratislava, Bratislava Region, Slovakia

Member since March 5, 2015

About the author

Stefan是一名合格的软件开发人员,拥有计算机科学学位,在科技行业有近十年的经验.

Toptal作者都是各自领域经过审查的专家,并撰写他们有经验的主题. 我们所有的内容都经过同行评审,并由同一领域的Toptal专家验证.

Expertise

Years of Experience

17

World-class articles, delivered weekly.

Subscription implies consent to our privacy policy

World-class articles, delivered weekly.

Subscription implies consent to our privacy policy

Toptal Developers

Join the Toptal® community.