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

IDEA搭建SpringBoot,MyBatis,Mysql工程项目

目录

一、前言

二、项目结构

三、初始化项目

四、SpringBoot项目集成Mybatis编写接口

五、代码仓库


一、前言

构建一个基于Spring Boot框架的现代化Web应用程序,以满足[公司/组织名称]对于[业务需求描述]的需求。通过利用Spring Boot简化企业级应用开发的优势,实现一个高效、可扩展且易于维护的应用系统。

  • 提高效率:通过集成微服务架构模式,减少系统的复杂度,并加速开发周期。
  • 增强用户体验:提供一个响应迅速、界面友好且功能齐全的用户交互平台。
  • 确保数据安全:实施严格的数据保护措施,保障用户信息安全。
  • 支持跨平台访问:设计RESTful API接口,确保应用程序可以在多种设备上无缝运行。

二、项目结构

项目遵循Maven标准目录布局,主要分为以下几个模块:

  • core:包含业务逻辑层代码。

  • exception:包含各个异常捕捉类。

  • mapper:定义了数据访问对象,用于与数据库交互。

  • service:提供了对外的服务接口。

  • entity:项目实体类

  • controller:处理HTTP请求并调用相应的服务方法。

  • config:存放所有配置文件。

  • test:单元测试及集成测试代码。

  • common:公共类,统一包装。

三、初始化项目

点击新建项目

根据以下图片创建

选择框架依赖

设置文件编码统一为UTF-8

检查Maven构建项目的目录

以下pom.xml文件已经加载,如果没有出现xml文件则需右键点击构建Maven项目即可

application配置文件,因为框架中选择了数据库,使用需要配置数据库,mybatis依赖在后续集成时,可添加以下配置。

# 应用服务 WEB 访问端口 server: port: 9090 #数据库配置 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456//(本机数据库密码) url: jdbc:mysql://localhost:3306/userdb//(本机数据库) //#mybatis依赖 //# mybatis: //# MyBatis Configuration确保在 application.properties 或 application.yml 文件中指定了正确的映射文件路径 // mapper-locations: classpath:mapper/*.xml // configuration: // log-impl: org.apache.ibatis.logging.stdout.StdOutImpl // map-underscore-to-camel-case: true

到此项目配置成功!

四、SpringBoot项目集成Mybatis编写接口

接下来我们将实现一个简单的用户列表获取接口,以下是具体的分层结构

Result类

package com.example.springboot.common; //统一设置包装类,返回数据的类型 public class Result { private Integer code; // 状态码 private String message; // 消息 private Object data; // 数据 // 默认成功的构造方法 // 成功但没有数据时的静态方法 public static Result success() { Result result =new Result(); result.setCode(200); result.setMessage("请求成功"); return result; } public static Result success(Object data) { Result result =success(); result.setData(data); return result; } // 静态方法用于快速构建结果对象 public static Result error() { Result result =new Result(); result.setCode(500); result.setMessage("请求成功"); return result; } // Getters and Setters public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } // 打印结果对象信息的方法(可选) @Override public String toString() { return "Result{" + "code=" + code + ", message='" + message + ''' + ", data=" + (data != null ? data.toString() : "null") + '}'; } }

UserController控制层提供访问接口localhost:9090/user/selectAll

package com.example.springboot.controller; import com.example.springboot.common.Result; import com.example.springboot.service.UserService; import com.example.springboot.entity.user; import jakarta.annotation.Resource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/user") public class UserController { @Resource private UserService userservice; //查询所有用户 @GetMapping("/selectAll") public Result selectAll(){ List<user>list=userservice.selectAll(); return Result.success(list); } }

User类实体类,需要对应相应数据库

package com.example.springboot.entity; import org.apache.ibatis.annotations.Mapper; //entity包里面装着实体类 public class user { private String userID; private String userName; private String userPassword; private Integer userAge; public String getUserID() { return userID; } public void setUserID(String userID) { this.userID = userID; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public Integer getUserAge() { return userAge; } public void setUserAge(Integer userAge) { this.userAge = userAge; } }

UserMapper接口连接数据库操作,在对应的Mybastis映射文件下,上文中的application配置文件中的路径一致,添加UserMapper.xml文件

package com.example.springboot.mapper; import com.example.springboot.entity.user; import java.util.List; //数据库userBean层 public interface UserMapper { List<user> selectAll(); }

安装插件MyBatisX可快速查看映射方法

userMapper.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.springboot.mapper.UserMapper"> <!-- SQL 映射语句将在此处定义 --> <select id="selectAll" resultType="com.example.springboot.entity.user"> SELECT * FROM user_list </select> </mapper>

UserService类,提供项目业务逻辑层

package com.example.springboot.service; import com.example.springboot.mapper.UserMapper; import com.example.springboot.entity.user; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import java.util.List; //业务逻辑层 @Service public class UserService { @Resource private UserMapper userMapper; public List<user> selectAll() { List<user> list = userMapper.selectAll(); return list; } }

在启动文件中添加扫描路径

@MapperScan("com.example.springboot.mapper")

点击运行即可!欢迎大家一起学习讨论,批评指正~

五、代码仓库

具体代码仓库

200K/SpringBoot[这里是图片020]https://gitee.com/tanzero/spring-boot.git

http://www.cnnetsun.cn/news/485119.html

相关文章:

  • CSDN(中国软件开发网)作为中国领先的IT技术交流平台,每年都会基于社区活跃度、开发者关注热点
  • 深度学习毕设项目推荐-深度学习基于python的CNN训练识别吃的美食基于python的深度学习CNN训练识别吃的美食
  • Java 进阶:如何让线程主动让出 CPU
  • HDFS的架构优势与基本操作
  • 通信协议仿真:蓝牙协议仿真_(8).蓝牙仿真硬件平台
  • 基于 VSC 的 UPFC(统一潮流控制器)研究(Simulink)
  • jQuery 语法详解
  • BO-LSTM贝叶斯优化长短期记忆神经网络回归预测(多输入单输出)MATLAB代码
  • HeidiSQL导入与导出数据
  • 《AI元人文构想:风骨智能体的舞台实践》深度研究报告
  • 学长亲荐10个一键生成论文工具,专科生轻松搞定毕业论文!
  • python_flask求职招聘岗位信息分析系统的设计与实现_xz0yin70可视化大屏
  • python_flask重庆旅游推荐系统_4ojn0or1爬虫可视化
  • 【江湖科技公司】一文带你了解上门按摩系统软件公司怎么选
  • golang debug调试
  • 深度学习计算机毕设之基于python的玉米粒品质检测基于深度学习的玉米粒品质检测
  • 神州信息×陕西农信:AI驱动数据资产管理,打造数字金融创新案例
  • GO和kEGG富集分析
  • AI大模型对就业的影响——典型岗位盘点,非常详细收藏我这一篇就够了
  • Failed to restart nginx.service Unit nginx.service not found
  • error @achrinzanode-ipc@9.2.5 The engine “node“ is incompatible with this module. 解决node.js版本不兼容问题
  • ERROR 1524 (HY000) Plugin ‘mysql_native_password‘ is not loaded
  • 代码随想录刷题——二叉树篇(十二)
  • DeepSeek R1 简易指南:架构、本地部署和硬件要求
  • 终极降AI指南!这款能让你相见恨晚的论文降aigc神器,实测降ai效果立竿见影
  • 还在为AI率爆表发愁吗?这几款降ai率工具推荐,实测免费降低ai率只需三步,建议反复观看!
  • Flutter 持续数据流设计:为什么一定要用 BasicMessageChannel?
  • AI驱动的企业财务舞弊模式演化跟踪系统
  • CES 2026最酷笔记本电脑:可拆卸设计成为新趋势
  • CentOS7安装Mysql5.7(ARM64架构)