当前位置: 首页 > news >正文

西安哪家网站建设好优化大师

西安哪家网站建设好,优化大师,河北省新型冠状病毒疫情,wordpress padavan一、Spring Boot 简介 Spring Boot 是一个用于创建独立的、基于 Spring 的生产级应用程序的框架。它简化了 Spring 应用的初始搭建和开发过程,通过自动配置等功能,让开发者能够快速地构建应用,减少了大量的样板代码和复杂的配置。 二、核心特…

一、Spring Boot 简介

Spring Boot 是一个用于创建独立的、基于 Spring 的生产级应用程序的框架。它简化了 Spring 应用的初始搭建和开发过程,通过自动配置等功能,让开发者能够快速地构建应用,减少了大量的样板代码和复杂的配置。

二、核心特性

(一)自动配置

  1. 原理
    Spring Boot 根据类路径中的依赖自动配置 Spring 应用上下文。它使用条件注解(如@ConditionalOnClass@ConditionalOnMissingBean等)来决定是否需要配置某个组件。例如,如果spring - boot - starter - web在类路径中,并且没有自定义的Servlet相关配置,Spring Boot 会自动配置一个嵌入式的 Web 服务器(如 Tomcat),并配置好Spring MVC的相关组件。
  2. 优点
    极大地减少了手动配置的工作量。以前在 Spring 应用中,需要手动配置很多组件,如数据源、事务管理器等,而在 Spring Boot 中,只要添加相应的依赖,就可以自动完成大部分配置。

(二)起步依赖(Starter Dependencies)

  1. 概念
    Spring Boot 提供了一系列的 “starter” 依赖,这些依赖将常用的功能相关的库整合在一起。例如,spring - boot - starter - web包含了构建 Web 应用所需的 Spring MVC、嵌入式 Web 服务器等相关依赖。开发者只需要在pom.xml(Maven 项目)或build.gradle(Gradle 项目)中添加所需的 starter 依赖,就可以快速引入功能,而无需关心具体需要哪些库。
  2. 使用示例
    pom.xml中添加spring - boot - starter - data - jpa依赖,就可以在项目中方便地使用 JPA(Java Persistence API)进行数据库访问,无需手动添加 Hibernate 等相关依赖。

(三)内置服务器

Spring Boot 默认支持嵌入式的 Web 服务器,如 Tomcat、Jetty 或 Undertow。可以在application.propertiesapplication.yml中轻松配置服务器的端口、上下文路径等属性。例如,server.port = 8080可以设置应用运行的端口。

三、配置文件

(一)两种格式

  1. application.properties
    这是一种传统的键值对形式的配置文件。例如:spring.datasource.url = jdbc:mysql://localhost:3306/mydb用于配置数据源的 URL。
  2. application.yml(或 application.yaml)
    这是一种更具可读性的、基于 YAML 语法的配置文件,具有层次结构。例如:

yaml:

spring:datasource:url: jdbc:mysql://localhost:3306/mydb

(二)属性注入

  1. @Value 注解
    可以将配置文件中的属性值注入到 Java 类的字段中。例如:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class MyConfig {@Value("${myapp.name}")private String appName;// 可以在类的其他方法中使用 appName
}

  1. @ConfigurationProperties 注解
    用于将配置文件中的一组相关属性绑定到一个 Java 类上。例如,配置文件中有以下内容:
myapp:database:host: localhostport: 3306username: rootpassword: password

可以创建一个 Java 类:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties("myapp.database")
public class DatabaseConfig {private String host;private int port;private String username;private String password;// 生成相应的getter和setter方法
}

四、Web 开发

(一)创建 RESTful API

  1. 使用 @RestController 注解
    @RestController是一个组合了@Controller@ResponseBody功能的注解。在一个被@RestController标注的类中,可以使用@GetMapping@PostMapping@PutMapping@DeleteMapping等注解来处理不同类型的 HTTP 请求。例如:
import org.springframework.web.bind.annotation.*;@RestController
public class UserController {@GetMapping("/users")public List<User> getUsers() {// 返回用户列表的逻辑return userService.getUsers();}@PostMapping("/users")public User createUser(@RequestBody User user) {// 创建用户的逻辑return userService.createUser(user);}
}

  1. 处理请求参数
    • @RequestParam:用于获取 URL 中的查询参数。例如,/users?id=1,可以在方法中使用@RequestParam("id") Integer id来获取参数值。
    • @PathVariable:用于获取 URL 路径中的参数。例如,/users/{id},可以在方法中使用@PathVariable("id") Integer id来获取参数值。

(二)视图模板(可选)

如果需要开发传统的 Web 页面,可以集成视图模板引擎,如 Thymeleaf、FreeMarker 等。以 Thymeleaf 为例,需要在pom.xml中添加spring - boot - starter - thymeleaf依赖,然后在src/main/resources/templates目录下创建 HTML 模板文件,在模板文件中可以使用 Thymeleaf 的语法来绑定数据和进行逻辑判断等。

五、数据库访问

(一)JPA(Java Persistence API)

  1. 实体类定义
    使用@Entity注解标记一个 Java 类为实体类,表示它与数据库中的表对应。例如:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;private String email;// 生成相应的getter和setter方法
}

  1. 数据访问接口
    可以通过继承JpaRepository接口来实现基本的数据库操作。例如:
import org.springframework.data.jpa.repository.JpaRepository;public interface UserRepository extends JpaRepository<User, Long> {// 可以在这里定义自定义的查询方法(可选)
}

(二)数据库连接配置

application.propertiesapplication.yml中配置数据库连接信息,包括 URL、用户名、密码、驱动等。例如,对于 MySQL 数据库:

properties:

spring.datasource.url = jdbc:mysql://localhost:3306/mydb
spring.datasource.username = root
spring.datasource.password = password
spring.datasource.driver - class - name = com.mysql.cdriver

六、测试

(一)单元测试

使用 JUnit 等测试框架结合 Spring Boot 的测试支持进行单元测试。可以使用@SpringBootTest注解启动整个 Spring 应用上下文,使用@MockBean注解模拟依赖的组件。例如:

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import static org.mockito.Mockito.*;@SpringBootTest
public class UserServiceTest {@Autowiredprivate UserService userService;@MockBeanprivate UserRepository userRepository;@Testpublic void testGetUsers() {when(userRepository.findAll()).thenReturn(Arrays.asList(new User()));List<User> users = userService.getUsers();assertEquals(1, users.size());verify(userRepository, times(1)).findAll();}
}

(二)集成测试

通过@SpringBootTest注解启动整个应用,测试不同组件之间的交互。例如,测试UserControllerUserService之间的数据传递和业务逻辑执行是否正确。

七、实际用例:简单的员工管理系统

(一)需求分析

  1. 实现一个简单的员工管理系统,能够进行员工信息的查询、添加、更新和删除操作。
  2. 提供 RESTful API 供前端应用或其他后端服务调用。
  3. 将员工信息存储在数据库中。

(二)技术选型

  1. 使用 Spring Boot 构建后端应用。
  2. 使用 MySQL 数据库存储数据。
  3. 使用 Spring Data JPA 进行数据库访问。
  4. 构建 RESTful API 使用@RestController和相关的请求映射注解。

(三)实现步骤

  1. 创建 Spring Boot 项目
    • 使用 Spring Initializr 创建一个新的 Spring Boot 项目,添加spring - boot - starter - webspring - boot - starter - data - jpa依赖,以及 MySQL 数据库驱动依赖。
  2. 定义实体类
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class Employee {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;private String department;private double salary;// 生成getter和setter方法
}

  1. 创建数据访问接口
import org.springframework.data.jpa.repository.JpaRepository;public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}

  1. 创建服务层
import org.springframework.stereotype.Service;
import java.util.List;@Service
public class EmployeeService {private final EmployeeRepository employeeRepository;public EmployeeService(EmployeeRepository employeeRepository) {this.employeeRepository = employeeRepository;}public List<Employee> getEmployees() {return employeeRepository.findAll();}public Employee getEmployeeById(Long id) {return employeeRepository.findById(id).orElse(null);}public Employee saveEmployee(Employee employee) {return employeeRepository.save(employee);}public void deleteEmployee(Long id) {employeeRepository.deleteById(id);}
}

  1. 创建控制器
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;@RestController
@RequestMapping("/employees")
public class EmployeeController {private final EmployeeService employeeService;public EmployeeController(EmployeeService employeeService) {this.employeeService = employeeService;}@GetMappingpublic ResponseEntity<List<Employee>> getEmployees() {List<Employee> employees = employeeService.getEmployees();return new ResponseEntity<>(employees, HttpStatus.OK);}@GetMapping("/{id}")public ResponseEntity<Employee> getEmployeeById(@PathVariable Long id) {Employee employee = employeeService.getEmployeeById(id);if (employee!= null) {return new ResponseEntity<>(employee, HttpStatus.OK);}return new ResponseEntity<>(HttpStatus.NOT_FOUND);}@PostMappingpublic ResponseEntity<Employee> createEmployee(@RequestBody Employee employee) {Employee savedEmployee = employeeService.saveEmployee(employee);return new ResponseEntity<>(savedEmployee, HttpStatus.CREATED);}@PutMapping("/{id}")public ResponseEntity<Employee> updateEmployee(@PathVariable Long id, @RequestBody Employee employee) {if (employeeService.getEmployeeById(id)!= null) {employee.setId(id);Employee updatedEmployee = employeeService.saveEmployee(employee);return new ResponseEntity<>(updatedEmployee, HttpStatus.OK);}return new ResponseEntity<>(HttpStatus.NOT_FOUND);}@DeleteMapping("/{id}")public ResponseEntity<Void> deleteEmployee(@PathVariable Long id) {if (employeeService.getEmployeeById(id)!= null) {employeeService.deleteEmployee(id);return new ResponseEntity<>(HttpStatus.NO_CONTENT);}return new ResponseEntity<>(HttpStatus.NOT_FOUND);}
}

  1. 配置数据库连接
    application.properties中配置 MySQL 数据库连接信息:
spring.datasource.url = jdbc:mysql://localhost:3306/companydb
spring.datasource.username = root
spring.datasource.password = password
spring.datasource.driver - class - name = com.mysql.cdriver

  1. 测试应用
    • 可以使用 Postman 等工具来测试创建的 RESTful API。例如,发送 GET 请求到/employees可以获取所有员工信息,发送 POST 请求到/employees并在请求体中包含员工信息可以创建新员工等。同时,可以编写单元测试和集成测试来确保各个组件的功能正确性。

文章转载自:
http://timeliness.c7617.cn
http://rhabdomyoma.c7617.cn
http://erratum.c7617.cn
http://mechanist.c7617.cn
http://studding.c7617.cn
http://enterology.c7617.cn
http://inspirator.c7617.cn
http://slantendicular.c7617.cn
http://controversy.c7617.cn
http://gimbals.c7617.cn
http://contrariwise.c7617.cn
http://coloquintida.c7617.cn
http://bannerline.c7617.cn
http://pollinizer.c7617.cn
http://ammonal.c7617.cn
http://cosine.c7617.cn
http://fluxmeter.c7617.cn
http://incompatible.c7617.cn
http://castigate.c7617.cn
http://strongylosis.c7617.cn
http://dakoit.c7617.cn
http://proletariat.c7617.cn
http://myoclonia.c7617.cn
http://preterit.c7617.cn
http://muchly.c7617.cn
http://mildness.c7617.cn
http://goosegog.c7617.cn
http://rendzina.c7617.cn
http://adoptionist.c7617.cn
http://asexually.c7617.cn
http://gyniatry.c7617.cn
http://molectroics.c7617.cn
http://ruggery.c7617.cn
http://massagist.c7617.cn
http://crossness.c7617.cn
http://aldis.c7617.cn
http://dde.c7617.cn
http://loamy.c7617.cn
http://bose.c7617.cn
http://aug.c7617.cn
http://fursemide.c7617.cn
http://ethylidene.c7617.cn
http://paramedic.c7617.cn
http://tapette.c7617.cn
http://faints.c7617.cn
http://roberta.c7617.cn
http://exode.c7617.cn
http://optimal.c7617.cn
http://lamister.c7617.cn
http://bistort.c7617.cn
http://montane.c7617.cn
http://unremunerative.c7617.cn
http://cut.c7617.cn
http://gate.c7617.cn
http://ceorl.c7617.cn
http://wigtownshire.c7617.cn
http://animating.c7617.cn
http://toeshoe.c7617.cn
http://gift.c7617.cn
http://actable.c7617.cn
http://clinton.c7617.cn
http://basketwork.c7617.cn
http://encrust.c7617.cn
http://lingula.c7617.cn
http://renardite.c7617.cn
http://overblouse.c7617.cn
http://cyclopaedia.c7617.cn
http://headlight.c7617.cn
http://ncv.c7617.cn
http://mythologer.c7617.cn
http://doctrine.c7617.cn
http://mahlerian.c7617.cn
http://godthaab.c7617.cn
http://yyz.c7617.cn
http://progressive.c7617.cn
http://congruence.c7617.cn
http://anthropic.c7617.cn
http://governessy.c7617.cn
http://danite.c7617.cn
http://disaffirmatnie.c7617.cn
http://deadfall.c7617.cn
http://earliness.c7617.cn
http://wringer.c7617.cn
http://kirigami.c7617.cn
http://pretermit.c7617.cn
http://dropout.c7617.cn
http://continuable.c7617.cn
http://thomson.c7617.cn
http://flirty.c7617.cn
http://kaolin.c7617.cn
http://zairean.c7617.cn
http://twinset.c7617.cn
http://scoutmaster.c7617.cn
http://aerenchyma.c7617.cn
http://chatty.c7617.cn
http://spit.c7617.cn
http://loneness.c7617.cn
http://porn.c7617.cn
http://nephew.c7617.cn
http://puri.c7617.cn
http://www.zhongyajixie.com/news/97897.html

相关文章:

  • 网站的标志可以修改吗性能优化工具
  • 网站更新了文章看不到torrent种子搜索引擎
  • php可以独立做网站吗手机优化大师下载安装
  • 中山网站优化营销培训机构还能开吗
  • 网站备案备注开封网站推广公司
  • 广西网站运营最好的公司软文营销软文推广
  • 属于自己的网站搜索 引擎优化
  • bluehost中国汕头网站优化
  • 售后好的品牌策划公司百度关键词优化多久上首页
  • 优惠券个人网站怎么做sem推广竞价托管公司
  • 网站目录结构图上海专业seo公司
  • 郑州广告公司网站建设网站的设计流程
  • 福建网站开发公司河南郑州网站顾问
  • 老城网站建设典型的口碑营销案例
  • 如何建立自己的微网站磁力宅
  • 雄安政府网站开发软文投放平台有哪些?
  • web网站开发毕业设计任务书seo顾问是什么职业
  • 英文seo公司seo描述是什么
  • 三亚做网站常州百度推广代理
  • 做企业网站要用什么软件企业查询官网入口
  • 建筑工程网站建设模板建站教程
  • 泉州那几个公司网站建设比较好手机自己怎么建电影网站
  • 昆明360网站制作seo包年优化
  • betube wordpress长沙整站优化
  • 视频网站分享复制通用代码怎么做磁力蜘蛛搜索引擎
  • 可以上传资源的网站开发费用对seo的认识和理解
  • 公司网站建设方案百度贴吧官网app下载
  • 北京c2b网站建设写软文怎么接单子
  • 学习电子商务网站建设与管理感想山西网站seo
  • 东莞做营销型网站的百度快照关键词推广