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

龙岗中心城网站建设在北京哪家公司建网站合适

龙岗中心城网站建设,在北京哪家公司建网站合适,建网站程序,wordpress继续阅读后端开发#xff1a;Spring Boot 快速开发实战 引言 在现代后端开发中#xff0c;Spring Boot 因其轻量级、快速开发的特性而备受开发者青睐。本文将带你从零开始#xff0c;使用 Spring Boot MyBatis 实现一个完整的 RESTful API#xff0c;并深入探讨如何优雅地处理异… 后端开发Spring Boot 快速开发实战 引言 在现代后端开发中Spring Boot 因其轻量级、快速开发的特性而备受开发者青睐。本文将带你从零开始使用 Spring Boot MyBatis 实现一个完整的 RESTful API并深入探讨如何优雅地处理异常和日志记录。无论你是初学者还是有一定经验的开发者这篇笔记都能为你提供实用的知识点。 一、环境准备 1. 安装依赖工具 确保你已经安装了以下工具 JDK 8 或更高版本Maven构建工具IDE如 IntelliJ IDEA 或 VS Code 2. 创建 Spring Boot 项目 使用 Spring Initializr 快速生成项目骨架 访问 Spring Initializr 网站。配置项目信息 Project: Maven ProjectLanguage: JavaSpring Boot: 最新稳定版本Dependencies: 添加 Spring Web, MyBatis Framework, MySQL Driver 下载并解压项目导入到 IDE 中。 二、Spring Boot MyBatis 实现 RESTful API 的完整流程 1. 数据库设计 假设我们要开发一个简单的用户管理系统包含以下字段 id (主键)name (用户名)email (邮箱) SQL 脚本 CREATE DATABASE user_management;USE user_management;CREATE TABLE users (id BIGINT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(100) NOT NULL,email VARCHAR(100) NOT NULL UNIQUE );2. 配置数据库连接 在 application.properties 文件中配置 MySQL 数据库连接信息 spring.datasource.urljdbc:mysql://localhost:3306/user_management?useSSLfalseserverTimezoneUTC spring.datasource.usernameroot spring.datasource.passwordyour_password spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver# MyBatis 配置 mybatis.mapper-locationsclasspath:mapper/*.xml3. 创建实体类 创建一个 User 实体类与数据库表对应 package com.example.demo.entity;public class User {private Long id;private String name;private String email;// Getters and Setters }4. 创建 Mapper 接口 使用 MyBatis 的注解或 XML 配置方式定义数据访问层接口 package com.example.demo.mapper;import com.example.demo.entity.User; import org.apache.ibatis.annotations.*;import java.util.List;Mapper public interface UserMapper {Select(SELECT * FROM users)ListUser findAll();Select(SELECT * FROM users WHERE id #{id})User findById(Long id);Insert(INSERT INTO users(name, email) VALUES(#{name}, #{email}))Options(useGeneratedKeys true, keyProperty id)void insert(User user);Update(UPDATE users SET name#{name}, email#{email} WHERE id#{id})void update(User user);Delete(DELETE FROM users WHERE id#{id})void delete(Long id); }5. 创建 Service 层 封装业务逻辑调用 Mapper 接口 package com.example.demo.service;import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.util.List;Service public class UserService {Autowiredprivate UserMapper userMapper;public ListUser getAllUsers() {return userMapper.findAll();}public User getUserById(Long id) {return userMapper.findById(id);}public void createUser(User user) {userMapper.insert(user);}public void updateUser(User user) {userMapper.update(user);}public void deleteUser(Long id) {userMapper.delete(id);} }6. 创建 Controller 层 暴露 RESTful API 接口 package com.example.demo.controller;import com.example.demo.entity.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;import java.util.List;RestController RequestMapping(/api/users) public class UserController {Autowiredprivate UserService userService;GetMappingpublic ListUser getAllUsers() {return userService.getAllUsers();}GetMapping(/{id})public User getUserById(PathVariable Long id) {return userService.getUserById(id);}PostMappingpublic void createUser(RequestBody User user) {userService.createUser(user);}PutMapping(/{id})public void updateUser(PathVariable Long id, RequestBody User user) {user.setId(id);userService.updateUser(user);}DeleteMapping(/{id})public void deleteUser(PathVariable Long id) {userService.deleteUser(id);} }三、如何优雅地处理异常和日志记录 1. 全局异常处理 使用 ControllerAdvice 注解实现全局异常处理避免重复代码 package com.example.demo.exception;import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler;ControllerAdvice public class GlobalExceptionHandler {ExceptionHandler(Exception.class)public ResponseEntityString handleException(Exception ex) {return new ResponseEntity(An error occurred: ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);}ExceptionHandler(ResourceNotFoundException.class)public ResponseEntityString handleResourceNotFoundException(ResourceNotFoundException ex) {return new ResponseEntity(ex.getMessage(), HttpStatus.NOT_FOUND);} }自定义异常类 package com.example.demo.exception;public class ResourceNotFoundException extends RuntimeException {public ResourceNotFoundException(String message) {super(message);} }2. 日志记录 使用 SLF4J 和 Logback 记录日志便于调试和问题追踪 package com.example.demo.controller;import com.example.demo.entity.User; import com.example.demo.service.UserService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;import java.util.List;RestController RequestMapping(/api/users) public class UserController {private static final Logger logger LoggerFactory.getLogger(UserController.class);Autowiredprivate UserService userService;GetMappingpublic ListUser getAllUsers() {logger.info(Fetching all users);return userService.getAllUsers();}PostMappingpublic void createUser(RequestBody User user) {logger.info(Creating user: {}, user);userService.createUser(user);} }四、总结 通过本文我们完成了以下内容 使用 Spring Boot 和 MyBatis 实现了一个完整的 RESTful API。学习了如何优雅地处理异常和记录日志。 这些技能是后端开发的核心能力能够帮助你在实际项目中快速构建高效、稳定的系统。希望这篇文章能为你提供实用的指导并助力你在以后的只有我道路上有目标向钱进 参考链接 Spring Boot 官方文档MyBatis 官方文档RESTful API 设计指南
http://www.hkea.cn/news/14389538/

相关文章:

  • 有做外国人的零售网站吗网站系统建设方案
  • 坑梓网站建设信息网站数据丢失怎么办
  • 中国建设信息港网站南京注册公司
  • 杭州公司网站域名续费域名没过期 网站打不开怎么办
  • 番禺网站开发公司电话网页历史记录保存天数
  • 岳阳手机网站建设网络系统管理学什么
  • 别的网站做相关链接怎么做网站建设制作临沂网站建设选盛誉
  • 东莞品牌网站设计公司计公司网站建设项目运营岗
  • 做电影视频网站赚钱嘛搭建单位网站
  • 网站建设模板漏洞做网站领券收佣金
  • 宁夏网站推广商城微信网站开发
  • 如何学习网站制作杭州建设厅特种作业证
  • 计算机专业学做网站吗青州做网站
  • 完整网站开发微信小程序会员卡管理系统
  • 建设美食电子商务网站四川成都建设网
  • 新网站收录多少关键词免费咨询皮肤科医生
  • 北京快速建站模板企业 怎么建交互网站
  • 做平面素材比较好的网站局域网建设网站工具
  • 网站系统环境的搭建水印wordpress
  • 品牌网站建设3小蝌蚪官方网站建设 招标公告
  • 自动跳转手机网站代码wordpress插件申请软著
  • 永信南昌网站建设wordpress会员付费
  • 网站开发用px还是rem网站建设维护工作经验
  • 南昌网站建设方案开发wordpress自动跳转相近链接
  • 石材网站建设seo搜索引擎优化方案
  • 网站群建设管理规定济南地铁建设
  • 深圳个性化建网站公司展厅设计收费标准
  • php网站开发技术论文互联网创业项目的效果
  • 温州企业建站系统wordpress图片优化插件
  • 网站建设构造学习wordpress中文相册插件