基于springboot的用户管理系统

基于框架spring boot 2.7.18,mybatius,数据库MySQL,实现一个用户管理系统,系统里面有增删改查接口,分为三层控制层,服务层,数据层,把sql放入mapper文件中

好的,下面是一个基于Spring Boot 2.7.18、MyBatis和MySQL的用户管理系统的实现示例。该系统包含用户管理的增删改查(CRUD)接口,分为控制层(Controller)、服务层(Service)和数据层(Data Layer),并将SQL语句放在Mapper文件中。

项目结构

```

user-management/

├── src/

│ ├── main/

│ │ ├── java/com/example/usermanagement/

│ │ │ ├── controller/

│ │ │ │ └── UserController.java

│ │ │ ├── entity/

│ │ │ │ └── User.java

│ │ │ ├── mapper/

│ │ │ │ └── UserMapper.java

│ │ │ ├── service/

│ │ │ │ ├── UserService.java

│ │ │ │ └── impl/

│ │ │ │ └── UserServiceImpl.java

│ │ │ └── UserManagementApplication.java

│ │ └── resources/

│ │ ├── application.properties

│ │ └── mapper/

│ │ └── UserMapper.xml

│ └── test/

│ └── java/com/example/usermanagement/

│ └── UserManagementApplicationTests.java

├── pom.xml

```

1. 配置 `pom.xml`

确保你的 `pom.xml` 文件包含必要的依赖项:

```xml

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>

<artifactId>user-management</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>user-management</name>

<description>用户管理系统</description>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.7.18</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<properties>

<java.version>1.8</java.version>

</properties>

<dependencies>

<!-- Spring Boot Starter Web -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!-- MyBatis Spring Boot Starter -->

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>2.2.2</version>

</dependency>

<!-- MySQL Connector -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

</dependency>

<!-- Lombok (可选,用于简化代码) -->

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<version>1.18.24</version>

<scope>provided</scope>

</dependency>

<!-- Spring Boot DevTools (可选,用于开发时自动重启) -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

<scope>runtime</scope>

<optional>true</optional>

</dependency>

<!-- 测试依赖 -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

<build>

<plugins>

<!-- Spring Boot Maven Plugin -->

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<version>2.7.18</version>

</plugin>

</plugins>

</build>

</project>

```

2. 配置数据库连接

在 `src/main/resources/application.properties` 中配置数据库连接:

```properties

MySQL 配置

spring.datasource.url=jdbc:mysql://localhost:3306/userdb?useSSL=false&serverTimezone=UTC

spring.datasource.username=root

spring.datasource.password=your_password

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

MyBatis 配置

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

mybatis.type-aliases-package=com.example.usermanagement.entity

日志级别

logging.level.root=INFO

logging.level.org.mybatis=DEBUG

```

确保在 MySQL 中创建了 `userdb` 数据库,并创建相应的 `users` 表:

```sql

CREATE DATABASE userdb;

USE userdb;

CREATE TABLE users (

id INT AUTO_INCREMENT PRIMARY KEY,

username VARCHAR(50) NOT NULL,

password VARCHAR(50) NOT NULL,

email VARCHAR(100),

created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

```

3. 创建实体类 `User.java`

```java

package com.example.usermanagement.entity;

import lombok.Data;

import java.time.LocalDateTime;

@Data

public class User {

private Integer id;

private String username;

private String password;

private String email;

private LocalDateTime createdAt;

}

```

4. 创建 Mapper 接口 `UserMapper.java`

```java

package com.example.usermanagement.mapper;

import com.example.usermanagement.entity.User;

import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

@Select("SELECT * FROM users WHERE id = #{id}")

User findById(Integer id);

@Select("SELECT * FROM users")

List<User> findAll();

@Insert("INSERT INTO users(username, password, email) VALUES(#{username}, #{password}, #{email})")

@Options(useGeneratedKeys = true, keyProperty = "id")

void insert(User user);

@Update("UPDATE users SET username = #{username}, password = #{password}, email = #{email} WHERE id = #{id}")

void update(User user);

@Delete("DELETE FROM users WHERE id = #{id}")

void delete(Integer id);

}

```

5. 创建 Mapper XML(可选)

如果你更喜欢使用 XML 配置 SQL,可以创建 `UserMapper.xml`:

```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.usermanagement.mapper.UserMapper">

<resultMap id="UserResultMap" type="User">

<id property="id" column="id" />

<result property="username" column="username" />

<result property="password" column="password" />

<result property="email" column="email" />

<result property="createdAt" column="created_at" />

</resultMap>

<select id="findById" parameterType="int" resultMap="UserResultMap">

SELECT * FROM users WHERE id = #{id}

</select>

<select id="findAll" resultMap="UserResultMap">

SELECT * FROM users

</select>

<insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id">

INSERT INTO users(username, password, email)

VALUES(#{username}, #{password}, #{email})

</insert>

<update id="update" parameterType="User">

UPDATE users

SET username = #{username},

password = #{password},

email = #{email}

WHERE id = #{id}

</update>

<delete id="delete" parameterType="int">

DELETE FROM users WHERE id = #{id}

</delete>

</mapper>

```

相关推荐
琪露诺大湿1 分钟前
JavaEE-多线程初阶(5)
java·开发语言·jvm·java-ee·idea·多线程·基础
helloworld工程师15 分钟前
电商系统架构演进
java·大数据·人工智能
徐寿春16 分钟前
必修 -- 常用笔试题
java
一叶飘零_sweeeet43 分钟前
Ribbon 与 Feign:微服务调用中的差异探究
spring·spring cloud·ribbon
曾维俊1 小时前
Volatile详解
java
多加点辣也没关系1 小时前
easyExcel - 导出合并单元格
java
XMJ20022 小时前
flutter插件:录制系统播放的声音
java·flutter
编程修仙2 小时前
Vector和ArrayList的比较
java·开发语言
fat house cat_2 小时前
MySQL是怎么解决幻读和不可重复读的?
java·数据库·mysql
程序猿麦小七3 小时前
springboot006基于SpringBoot的网上订餐系统(源码+包运行+LW+技术指导)
java·spring boot·后端·网上订餐