springboot前后端分离,向数据库添加数据

本文介绍了如何在Spring Boot项目中利用Mybatis框架,配合MySQL数据库8.0版本,实现后台添加用户的功能。文章详细阐述了从数据库操作到前端页面的构建,包括yml配置、pom依赖、统一返回结果类、枚举类、添加页面的jQuery异步刷新技术以及Controller层的代码编写。最后,通过启动测试验证了用户添加的成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

后台添加一个用户

描述:

1.使用的是springboot+mybatis的框架
2.MySQL数据库版本是8.0
3.项目时JavaWeb项目
4.使用了jQuery框架(ajax异步刷新技术)

数据库

直接执行SQL语句(直接执行下面的SQL代码—在Navicat工具中,或者在cmd中登录MySQL执行也可以)

DROP TABLE IF EXISTS `sysrole`;
CREATE TABLE `sysrole`  (
  `roleid` int(0) NOT NULL AUTO_INCREMENT COMMENT '角色id roleid',
  `rolename` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '角色名称',
  `roledesc` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '角色描述',
  `rolestate` int(0) NULL DEFAULT NULL COMMENT '角色状态 0不可用 1 可用',
  PRIMARY KEY (`roleid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '角色信息表' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of sysrole
-- ----------------------------
INSERT INTO `sysrole` VALUES (1, '超级管理员', '负责系统管理', 1);
INSERT INTO `sysrole` VALUES (2, '管理员', '各个表进行管理', 1);
INSERT INTO `sysrole` VALUES (3, '普通用户', '查看信息,对自己信息修改', 1);
INSERT INTO `sysrole` VALUES (4, '游客', '查看部分信息', 1);
INSERT INTO `sysrole` VALUES (5, '教师', '555', 1);
INSERT INTO `sysrole` VALUES (6, '工作人员', '31321', 1);

-- Table structure for sysuser
-- ----------------------------
DROP TABLE IF EXISTS `sysuser`;
CREATE TABLE `sysuser`  (
  `userid` int(0) NOT NULL AUTO_INCREMENT,
  `roleid` int(0) NULL DEFAULT NULL COMMENT '角色id roleid',
  `username` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `userpwd` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `usertruename` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `usersex` varchar(2) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `useridentity` varchar(18) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `userphone` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `useraddr` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `userstate` int(0) NULL DEFAULT NULL,
  `pcid` int(0) NULL DEFAULT 1 COMMENT '头像图片id',
  PRIMARY KEY (`userid`) USING BTREE,
  INDEX `FK_sysuser_sysrole_r`(`roleid`) USING BTREE,
  CONSTRAINT `FK_sysuser_sysrole_r` FOREIGN KEY (`roleid`) REFERENCES `sysrole` (`roleid`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 15 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户基本信息表' ROW_FORMAT = Dynamic;

-- ----------------------------

-- ----------------------------
-- Table structure for sysrole
-- ----------------------------

目录结构

在这里插入图片描述
在这里插入图片描述

配置

1.yml配置
server:
  port: 端口号(一般是8080)
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
    username: 数据库账号
    password: 数据库密码
mybatis-plus:
  global-config:
    db-config:
      id-type: assign_id
  configuration:
    map-underscore-to-camel-case: false
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:mappers/*.xml
  #    配置类别名 --简写
  type-enums-package: edu.znzz.springbootmybatisdemo1
  #  配置swagger
swagger:
  enabled: true
  title: 'SHY API'
  description: CDETIME
  base-package: eedu.znzz.springbootmybatisdemo1.controller




2.pom依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.9.0.RELEASE</version>
        </depen
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值