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依赖在后续集成时,可添加以下配置。

java 复制代码
# 应用服务 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类

java 复制代码
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

java 复制代码
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类实体类,需要对应相应数据库

java 复制代码
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文件

javascript 复制代码
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配置文件

java 复制代码
<?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类,提供项目业务逻辑层

java 复制代码
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;
    }
}

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

java 复制代码
@MapperScan("com.example.springboot.mapper")

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

五、代码仓库

具体代码仓库

200K/SpringBoothttps://gitee.com/tanzero/spring-boot.git

相关推荐
canonical_entropy7 分钟前
最小变更成本 vs 最小信息表达:第一性原理的比较
后端
渣哥7 分钟前
代理选错,性能和功能全翻车!Spring AOP 的默认技术别再搞混
javascript·后端·面试
间彧23 分钟前
Java泛型详解与项目实战
后端
间彧33 分钟前
PECS原则在Java集合框架中的具体实现有哪些?举例说明
后端
间彧35 分钟前
Java 泛型擦除详解和项目实战
后端
间彧39 分钟前
在自定义泛型类时,如何正确应用PECS原则来设计API?
后端
间彧39 分钟前
能否详细解释PECS原则及其在项目中的实际应用场景?
后端
武子康1 小时前
大数据-132 Flink SQL 实战入门 | 3 分钟跑通 Table API + SQL 含 toChangelogStream 新写法
大数据·后端·flink
李辰洋1 小时前
go tools安装
开发语言·后端·golang
wanfeng_091 小时前
go lang
开发语言·后端·golang