欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > 2024最新版本idea SpringBoot创建web项目(详细介绍如何搭建和配置spring boot web,以及写出一个简单的前后端交互界面)

2024最新版本idea SpringBoot创建web项目(详细介绍如何搭建和配置spring boot web,以及写出一个简单的前后端交互界面)

2025/4/16 8:13:09 来源:https://blog.csdn.net/m0_74825656/article/details/145189318  浏览:    关键词:2024最新版本idea SpringBoot创建web项目(详细介绍如何搭建和配置spring boot web,以及写出一个简单的前后端交互界面)

1.创建springboot项目:

新建项目 -> Spring Boot ->自定义写你的项目名称、项目位置等、语言java、类型选择maven,最后选择JDK版本,这里推荐17以上,对应Java也一样,最后选jar包 -> next

首先选择springboot版本,我这里为3.3.5,接下来导入依赖,springboot功能强大, 能直接导入而不需要手敲,按图先导入这几种(可以直接搜索),下面为对这几种依赖的个人解释:
Lombok日志文件,springboot开发必不可少

Spring Web:添加web依赖,web开发必须

Thymeleaf:处理web环境

JDBC API:数据库驱动

MySQL Driver:用于与MySQL数据库交互

CycloneDX SBOM support:用于跟踪,管理依赖项

接下来点击创建,等待idea构建完毕

2.配置application.yml文件

开始时你只能看到applicaiton.propitious,右键 -> 重构 -> 改为application.yml

删除里面代码,复制为如下代码:

spring:
application:
name: xlxz

datasource:
url: jdbc:mysql://localhost:3306/【这里替换你建的数据库名】useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root #这里替换你的数据库用户名
password: 123456#这里替换你的数据库密码
driver-class-name: com.mysql.cj.jdbc.Driver

mybatis-plus:
mapper-locations: classpath:mapper/*.xml

3.接下来你就可以自己测试了!(可以自己写,也可以用文末我的简单示例)

4.如果测试不成功,如注解大量报错这些,则可能是最新的springboot版本太超前了,与mybatis-plus-boot-starter 不兼容,则需要自己手动导入一下依赖:

在pom.xml中,

里面导入以下依赖:

com.baomidou mybatis-plus-boot-starter 3.5.7 org.mybatis mybatis-spring org.mybatis mybatis-spring 3.0.3 mysql mysql-connector-java 8.0.33

这三个依赖大致作用就是解析和sql驱动。

然后右键 -> 点击maven ->重新加载项目

就可以解决报错问题(当然这是基础的构建问题)

5.简单测试:

首先建一个简单的数据库:

create database users1;

use users1;

CREATE TABLE `user` (
`userid` int NOT NULL AUTO_INCREMENT COMMENT ‘用户id’,
`username` varchar(255) DEFAULT NULL COMMENT ‘用户名字’,
`userPassword` varchar(255) DEFAULT NULL COMMENT ‘用户密码’,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

然后插入一个简单数据:例如下:

然后按下面这个格式建包建类:

各类的代码如下:(记得代码中自己替换成自己项目的路径,否则会报红)

UserController:

package com.xlxz.springboot3.controller;

import com.xlxz.springboot3.entiy.User;

import com.xlxz.springboot3.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Controller
public class UserController {
@Resource
UserService userService;

@GetMapping("/index")
public  String list(Model model){List<User> list = userService.list();model.addAttribute("list", list);return "index";
}

}

User:

package com.xlxz.springboot3.entiy;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(“user”)
public class User {
@TableField(“userid”)
private int userid;
@TableField(“username”)
private String username;
@TableField(“userPassword”)
private String userPassword;
}

UserMapper:

package com.xlxz.springboot3.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xlxz.springboot3.entiy.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper {
}

UserServiceImpl:

package com.xlxz.springboot3.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xlxz.springboot3.entiy.User;
import com.xlxz.springboot3.mapper.UserMapper;
import com.xlxz.springboot3.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

}

UserServiceI:

package com.xlxz.springboot3.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.xlxz.springboot3.entiy.User;

public interface UserService extends IService {
}

index.html:

用户列表

这是用户列表页面

ID用户名密码
1td> 用户名td> 密码td>

application.yml:这个已经有了,主要替换数据库信息

spring:
application:
name: xlxz

datasource:
url: jdbc:mysql://localhost:3306/users1useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver

mybatis-plus:
mapper-locations: classpath:mapper/*.xml

UserMapper.xml一般建好就有代码了,没有就复制如下:

启动:

没有爆红就OK,打开浏览器,输入:http://localhost:8080/index运行:

测试完成,实现了一个简单的前后端数据库交互

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词