找人做网站怎么知道归属人,创意做网站公司,注册域名后怎么做网站,深圳注册公司网上申请入口Spring Framework 是一个功能强大、功能丰富且设计精良的 Java 平台框架。它提供了一系列编程和配置模型#xff0c;旨在简化和精简 Java 中健壮且可测试的应用程序的开发过程。
人们常说 Java 太复杂了#xff0c;构建简单的应用程序需要很长时间。尽管如此#xff0c;Jav…Spring Framework 是一个功能强大、功能丰富且设计精良的 Java 平台框架。它提供了一系列编程和配置模型旨在简化和精简 Java 中健壮且可测试的应用程序的开发过程。
人们常说 Java 太复杂了构建简单的应用程序需要很长时间。尽管如此Java 提供了一个稳定的平台周围有一个非常成熟的生态系统这使其成为开发强大软件的绝佳选择。
Spring Framework 是 Java 生态系统中众多强大的框架之一它附带了一系列编程和配置模型旨在简化 Java 中高性能和可测试应用程序的开发。 在本教程中我们将接受构建一个简单的应用程序的挑战该应用程序将SpringMVC轻松掌握。
Spring Framework 教程入门
要构建基于 Spring 的应用程序我们需要使用以下构建工具之一
Maven 相关Gradle 相关
在 Spring Tool Suite 中我们通过从“File New”菜单下选择“Spring Starter Project”来创建一个新项目。 创建新项目后我们需要编辑 Maven 配置文件 “pom.xml” 并添加以下依赖项
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-jpa/artifactId
/dependency
dependencygroupIdcom.h2database/groupIdartifactIdh2/artifactId
/dependency
dependencygroupIdorg.springframework.data/groupIdartifactIdspring-data-commons/artifactId
/dependency这些列出的依赖项将加载 Spring Boot Web、Thymeleaf、JPA 和 H2将用作我们的内存数据库。所有必要的库都将自动拉取。
实体类
了能够存储有关开发人员及其技能的信息我们需要定义两个实体类“Developer”和“Skill”。
这两个类都被定义为带有一些注解的普通 Java 类。通过在类之前添加Entity我们可以将它们的实例提供给 JPA。这将使在需要时从持久性数据存储中存储和检索实例变得更加容易。此外Id和GeneratedValue注释允许我们指示实体的唯一 ID 字段并在存储在数据库中时自动生成其值。
由于开发人员可以拥有许多技能因此我们可以使用 “ManyToMany” 注解定义一个简单的多对多关系。
Developer 开发 人员
Entity
public class Developer {IdGeneratedValue(strategyGenerationType.AUTO)private long id;private String firstName;private String lastName;private String email;ManyToManyprivate ListSkill skills;public Developer() {super();}public Developer(String firstName, String lastName, String email,ListSkill 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 ListSkill getSkills() {return skills;}public void setSkills(ListSkill 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 {IdGeneratedValue(strategyGenerationType.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我们可以定义一个非常有用的 Developer Repository 接口和 SkillRepository 接口它们允许简单的 CRUD 操作。这些接口将允许我们通过简单的方法调用访问存储的开发人员和技能例如 respository.findAll返回所有开发人员repository.findOneid返回具有给定 ID 的开发人员 要创建这些接口我们需要做的就是扩展 CrudRepository 接口。
Developer Repository 开发人员存储库
public interface DeveloperRepository extends CrudRepositoryDeveloper, Long {}Skill Repository 技能仓库
public interface SkillRepository extends CrudRepositorySkill, Long {public ListSkill findByLabel(String label);
}此处声明的附加方法 findByLabel 的功能将由 JPA 自动提供。
Controller 控制器
接下来我们可以为这个应用程序开发控制器。控制器将映射请求 URI 以查看模板并在两者之间执行所有必要的处理。
Controller
public class DevelopersController {AutowiredDeveloperRepository repository;AutowiredSkillRepository 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,methodRequestMethod.GET)public String developersList(Model model) {model.addAttribute(developers, repository.findAll());return developers;}RequestMapping(value/developers,methodRequestMethod.POST)public String developersAdd(RequestParam String email, RequestParam String firstName, RequestParam String lastName, Model model) {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, methodRequestMethod.POST)public String developersAddSkill(PathVariable Long id, RequestParam Long skillId, Model model) {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 注解完成的。在这种情况下控制器的每个方法都映射到一个 URI。
这些方法的 model 参数允许将数据传递到视图。从本质上讲这些是键到值的简单映射。
每个控制器方法要么返回要用作视图的 Thymeleaf 模板的名称要么返回要重定向到的特定模式redirect的 URL。例如方法 developer 和 _developersList_ 返回模板的名称而 developersAdd 和 developersAddSkill 返回要重定向到的 URL。
在控制器中Autowired注释会自动在相应字段中分配我们定义的存储库的有效实例。这允许从控制器内部访问相关数据而无需处理大量样板代码。
Views 视图
最后我们需要为要生成的视图定义一些模板。为此我们使用了 Thymeleaf一个简单的模板引擎。我们在控制器方法中使用的模型可以直接在模板中使用即当我们在模型的 contract” 键中输入合约时我们将能够从模板中以 “contract.name” 的形式访问 name 字段。
Thymeleaf 包含一些控制 HTML 生成的特殊元素和属性。他们非常直观和直接。例如要使用技能名称填充 span 元素的内容您只需定义以下属性假设在模型中定义了键“skill”
span th:text${skill.label}/span与设置锚点元素的 href 属性类似可以使用特殊属性 *thhref。
在我们的应用程序中我们需要两个简单的模板。为清楚起见我们将在嵌入式模板代码中跳过所有 style 和 class 属性即 Bootstrap 属性。
Developer List 开发者名单 !DOCTYPE HTML
html xmlns:thhttp://www.thymeleaf.org
head titleDevelopers database/title meta http-equivContent-Type contenttext/html; charsetUTF-8 /
/head
bodyh1Developers/h1tabletrthName/ththSkills/thth/th/trtr th:eachdeveloper : ${developers}td th:text${developer.firstName developer.lastName}/tdtdspan th:eachskill,iterStat : ${developer.skills}span th:text${skill.label}/th:block th:if${!iterStat.last},/th:block/span/tdtda th:href{/developer/{id}(id${developer.id})}view/a/td/tr/tablehr/form th:action{/developers} methodpost enctypemultipart/form-datadivFirst name: input namefirstName //divdivLast name: input namelastName //divdivEmail: input nameemail //divdivinput typesubmit valueCreate developer namebutton//div/form
/body
/htmlDeveloper Details 开发者详情 !DOCTYPE HTML
html xmlns:thhttp://www.thymeleaf.org
headtitleDeveloper/titlemeta http-equivContent-Type contenttext/html; charsetUTF-8 /
/head
bodyh1Developer/h1Name: b th:text${developer.firstName} / b th:text${developer.lastName} /br/Email: span th:text${developer.email} /br/Skills:span th:eachskill : ${developer.skills}br/nbsp;nbsp;span th:text${skill.label} / - span th:text${skill.description} //spanform th:action{/developer/{id}/skills(id${developer.id})} methodpost enctypemultipart/form-data select nameskillIdoption th:eachskill : ${skills} th:value${skill.id} th:text${skill.description}Skill/option/selectinput typesubmit valueAdd skill//form
/body
/html启动服务器
Spring 包含一个 boot 模块。这允许我们轻松地从命令行作为命令行 Java 应用程序启动服务器
SpringBootApplication
public class Application implements CommandLineRunner {AutowiredDeveloperRepository developerRepository;AutowiredSkillRepository skillRepository;public static void main(String[] args) {SpringApplication.run(Application.class, args);}}由于我们使用的是内存中数据库因此在启动时使用一些预定义数据引导数据库是有意义的。这样当服务器启动并运行时数据库中至少会有一些数据。
Override
public void run(String... args) throws Exception {Skill javascript new Skill(javascript, Javascript language skill);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);ListDeveloper developers new LinkedListDeveloper();developers.add(new Developer(John, Smith, john.smithexample.com, Arrays.asList(new Skill[] { javascript, ruby })));developers.add(new Developer(Mark, Johnson, mjohnsonexample.com, Arrays.asList(new Skill[] { emberjs, ruby })));developers.add(new Developer(Michael, Williams, michael.williamsexample.com, Arrays.asList(new Skill[] { angularjs, ruby })));developers.add(new Developer(Fred, Miller, f.millerexample.com, Arrays.asList(new Skill[] { emberjs, angularjs, javascript })));developers.add(new Developer(Bob, Brown, brownexample.com, Arrays.asList(new Skill[] { emberjs })));developerRepository.save(developers);
}总结
Spring 是一个多功能框架允许构建 MVC 应用程序。使用 Spring 构建一个简单的应用程序既快速又透明。该应用程序还可以使用 JPA 轻松与数据库集成。
GitHub 该篇文章中使用到Demo源码整个项目的源代码。
推荐阅读
1、在 Spring 中使用 EhCache 注解作为缓存 2、有缺陷的 Java 代码Java 开发人员最常犯的 10 大错误 3、如何理解应用 Java 多线程与并发编程 4、Java Spring 中常用的 PostConstruct 注解使用总结 5、线程 vs 虚拟线程深入理解及区别 6、深度解读 JDK 8、JDK 11、JDK 17 和 JDK 21 的区别 7、10大程序员提升代码优雅度的必杀技瞬间让你成为团队宠儿 8、“打破重复代码的魔咒使用 Function 接口在 Java 8 中实现优雅重构” 9、Java 中消除 If-else 技巧总结 10、线程池的核心参数配置(仅供参考) 11【人工智能】聊聊Transformer深度学习的一股清流(13)