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

做科学小制作的视频网站找装修公司的网站

做科学小制作的视频网站,找装修公司的网站,成都私家花园设计公司哪家好,站长工具2023最新国产一、引言 1. 项目背景与目标 在现代Web开发中#xff0c;CRUD#xff08;创建、读取、更新、删除#xff09;操作是几乎所有应用程序的核心功能。本项目旨在通过Spring Boot、MyBatis和MySQL技术栈#xff0c;快速搭建一个高效、简洁的CRUD应用。我们将从零开始#xff…一、引言 1. 项目背景与目标 在现代Web开发中CRUD创建、读取、更新、删除操作是几乎所有应用程序的核心功能。本项目旨在通过Spring Boot、MyBatis和MySQL技术栈快速搭建一个高效、简洁的CRUD应用。我们将从零开始逐步实现一个用户管理系统的增删改查功能。 2. 技术选型与适用场景 Spring Boot简化了基于Spring的应用开发提供了自动配置、嵌入式服务器等特性。MyBatis作为持久层框架支持自定义SQL、存储过程和高级映射灵活性高。MySQL广泛使用的开源关系型数据库性能稳定社区活跃。 二、开发环境准备 1. 开发工具与依赖安装 JDK确保已安装Java Development Kit建议版本8及以上。Maven/Gradle用于项目构建和依赖管理。IDE推荐使用IntelliJ IDEA或Eclipse。MySQL下载并安装MySQL数据库配置好数据库连接信息。 2. Spring Boot项目初始化 使用 Spring Initializr 创建项目 Project: Maven ProjectLanguage: JavaSpring Boot: 最新稳定版本Dependencies: Spring Web, MyBatis Framework, MySQL Driver 三、数据库设计与初始化 1. MySQL数据库表设计 创建一个简单的用户表user包含以下字段 CREATE TABLE user (id INT AUTO_INCREMENT PRIMARY KEY,username VARCHAR(50) NOT NULL UNIQUE,password VARCHAR(100) NOT NULL,email VARCHAR(100),created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );2. 数据初始化脚本 在src/main/resources目录下创建data.sql文件插入一些测试数据 INSERT INTO user (username, password, email) VALUES (admin, password123, adminexample.com), (user1, password123, user1example.com);配置application.yml中的数据库连接信息 spring:datasource:url: jdbc:mysql://localhost:3306/mydb?useSSLfalseserverTimezoneUTCusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driver四、Spring Boot与MyBatis集成 1. MyBatis基础配置 在application.yml中添加MyBatis配置 mybatis:mapper-locations: classpath:mapper/*.xmltype-aliases-package: com.example.demo.entity2. 实体类与Mapper接口开发 实体类创建User类表示用户信息。 package com.example.demo.entity;public class User {private Integer id;private String username;private String password;private String email;private Timestamp createdAt;// Getters and Setters }Mapper接口创建UserMapper接口及对应的XML映射文件。 package com.example.demo.mapper;import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select;Mapper public interface UserMapper {Select(SELECT * FROM user WHERE id #{id})User findById(Integer id); }在src/main/resources/mapper目录下创建UserMapper.xml ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.example.demo.mapper.UserMapperselect idfindById resultTypecom.example.demo.entity.UserSELECT * FROM user WHERE id #{id}/select /mapper3. Service层与Controller层实现 Service层封装业务逻辑。 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;Service public class UserService {Autowiredprivate UserMapper userMapper;public User getUserById(Integer id) {return userMapper.findById(id);} }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.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController;RestController public class UserController {Autowiredprivate UserService userService;GetMapping(/users/{id})public User getUser(PathVariable Integer id) {return userService.getUserById(id);} }五、CRUD功能实现 1. 创建Create功能 Mapper接口添加插入数据的方法。 Insert(INSERT INTO user (username, password, email) VALUES (#{username}, #{password}, #{email})) void insertUser(User user);Service层实现新增用户逻辑。 public void createUser(User user) {userMapper.insertUser(user); }Controller层提供新增用户的API接口。 PostMapping(/users) public void createUser(RequestBody User user) {userService.createUser(user); }2. 读取Read功能 分页查询使用MyBatis分页插件PageHelper。select idfindAllUsers resultTypecom.example.demo.entity.UserSELECT * FROM user /selectPageHelper.startPage(pageNum, pageSize);ListUser users userMapper.findAllUsers();PageInfoUser pageInfo new PageInfo(users);动态条件查询按用户名模糊搜索。select idfindUsersByUsername resultTypecom.example.demo.entity.UserSELECT * FROM user WHERE username LIKE CONCAT(%, #{username}, %) /select3. 更新Update功能 Mapper接口添加更新数据的方法。 Update(UPDATE user SET username#{username}, password#{password}, email#{email} WHERE id#{id}) void updateUser(User user);Service层实现更新用户逻辑。 public void updateUser(User user) {userMapper.updateUser(user); }Controller层提供更新用户的API接口。 PutMapping(/users/{id}) public void updateUser(PathVariable Integer id, RequestBody User user) {user.setId(id);userService.updateUser(user); }4. 删除Delete功能 硬删除直接从数据库中删除记录。 Delete(DELETE FROM user WHERE id#{id}) void deleteUserById(Integer id);软删除添加is_deleted字段标记删除状态。 ALTER TABLE user ADD COLUMN is_deleted TINYINT DEFAULT 0;Update(UPDATE user SET is_deleted1 WHERE id#{id})void softDeleteUserById(Integer id);六、功能扩展与优化 1. 事务管理 Transactional注解确保多个操作在同一事务中执行。Service Transactional public class UserService {// CRUD方法 }2. 分页与排序 PageHelper分页插件实现分页查询。 dependencygroupIdcom.github.pagehelper/groupIdartifactIdpagehelper-spring-boot-starter/artifactIdversion1.4.0/version /dependency排序参数动态传入根据请求参数进行排序。 PageHelper.startPage(pageNum, pageSize).setOrderBy(orderBy);3. 异常处理 全局异常捕获使用ControllerAdvice处理全局异常。ControllerAdvice public class GlobalExceptionHandler {ExceptionHandler(Exception.class)public ResponseEntityString handleException(Exception e) {return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());} }七、测试与部署 1. 单元测试与集成测试 单元测试编写Mapper接口测试用例。 RunWith(SpringRunner.class) SpringBootTest public class UserMapperTest {Autowiredprivate UserMapper userMapper;Testpublic void testFindById() {User user userMapper.findById(1);assertNotNull(user);} }集成测试验证CRUD功能完整性。 RunWith(SpringRunner.class) SpringBootTest(webEnvironment SpringBootTest.WebEnvironment.RANDOM_PORT) public class UserControllerIT {Autowiredprivate TestRestTemplate restTemplate;Testpublic void testGetUser() {ResponseEntityUser response restTemplate.getForEntity(/users/1, User.class);assertEquals(HttpStatus.OK, response.getStatusCode());} }2. 项目打包与部署 打包为可执行JAR文件 mvn clean package部署到本地Tomcat或云服务器 将生成的JAR文件上传至服务器并通过命令启动java -jar myapp.jar
http://www.dnsts.com.cn/news/52833.html

相关文章:

  • 如何seo网站外贸如何推广公司网站
  • 长春seo技术seo关键词优化排名外包
  • 微信端网站开发流程图网站建设申请报告怎么写
  • 北京网站定制流程怎么样开发小程序
  • 深圳宝安高端网站建设报价网站如何防止别人抄袭
  • 北京网站优化步骤建筑模型设计网站建设
  • 一级a做爰片在线看免播放器网站什么是自建站
  • 一个网站建设的组成长沙人才招聘网最新
  • c网站开发广州建筑装饰集团有限公司
  • 制作网页框架太原网站优化怎么做
  • 织梦房产网站模板wordpress手机访问慢
  • 企业网络推广做网站推广公司京东网上商城官网
  • 芜湖网站建设哪家好安徽网新科技怎么建设网站
  • 网站栏目内链怎么做wordpress伪静
  • 网站开发用什么软件编程asp网站用什么数据库
  • 做海关授权的网站做网络推广一个月的收入
  • 宁波优化推广选哪家东莞网络排名优化
  • 公司网站推广如何做建平台网站费用
  • 2021年度关键词有哪些seo网站程序
  • 用织梦做的网站好还是cms网站服务器怎么做安全防护
  • 网站开发技术说明文档山西响应式网站建设哪家有
  • 微建站官网公众号开发教程视频
  • 网站建设心得.doc可以搜索国外网站的搜索引擎
  • 做暖暖XO网站秦皇岛seo排名
  • 站长之家最新域名查询免费域名注册网中国万网
  • 网站备案时 首页wordpress百家号模版
  • 企业网站 制作wordpress修改功能小工具
  • 招聘销售员网站建设网络推广手袋 东莞网站建设
  • 庆阳做网站公司郑州睿网站建设
  • 网站空间集装箱网站制作 北京网站建设公司