• Springboot+mybatis 完整实现CRUD练习项目(带service分层)


    0. mysql 建表代码

    DROP TABLE IF EXISTS `user`;
    CREATE TABLE `user` (
    `userid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '序号',
    `username` varchar(25) COLLATE utf8mb4_unicode_ci DEFAULT null COMMENT '用户名',
    `loginname` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT null COMMENT '账号',
    `password` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT null COMMENT '密码',
    `phone` varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT null COMMENT '联系方式',
    PRIMARY KEY (`userid`) USING BTREE
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='用户表';
    
    
    INSERT INTO `demo1`.`user`(`userid`, `username`, `loginname`, `password`, `phone`) VALUES (1, 'a', 'aa', '12345', '18988881111');
    
    
    SELECT * FROM user;
    TRUNCATE TABLE user;--清空表数据
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    1.项目结构与详细代码

    在这里插入图片描述

    1.1 pom依赖

    
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.7.5version>
            <relativePath/> 
        parent>
        <groupId>com.examplegroupId>
        <artifactId>demo1artifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>demo1name>
        <description>demo1description>
        <properties>
            <java.version>1.8java.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>2.2.2version>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-devtoolsartifactId>
                <scope>runtimescope>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>com.mysqlgroupId>
                <artifactId>mysql-connector-jartifactId>
                <scope>runtimescope>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
            
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>2.1.1version>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-jdbcartifactId>
            dependency>
        dependencies>
    
        <build>
            <plugins>
    
            plugins>
        build>
    
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    1.2 application.yml

    server:
      port: 8999
    spring:
      application:
        name: demo1
      datasource: #这是MySQL所使用的配置
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/demo1?useSSL=true&useUnicode=true&characterEncoding=utf8
        username: root
        password: xxx
    
    #这是mybatis所使用的配置
    mybatis:
      type-aliases-package: com.example.demo.entity
      mapper-locations: classpath*:mappers/*.xml
      #使用mybatis有两个重要的配置:1:mapper-locations:告诉mybatisSQL的映射文件在这里
      #2:type-aliases-package:告诉mybatis对应的实体类位置
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    1.3 User 实体类 (用了lombok插件,省略了get/set方法

    package com.example.demo.entity;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
        private String userid;
        private String username;
        private String loginname;
        private String password;
        private String phone;
    
        @Override
        public String toString() {
            return "User{" +
                    "userid='" + userid + '\'' +
                    ", username='" + username + '\'' +
                    ", loginname='" + loginname + '\'' +
                    ", password='" + password + '\'' +
                    ", phone='" + phone + '\'' +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    1.4 UserMapper 接口

    package com.example.demo.mapper;
    
    import com.example.demo.entity.User;
    import org.apache.ibatis.annotations.Mapper;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    
    @Mapper
    @Repository
    public interface UserMapper {
    
        List<User> queryUserList();
    
        User queryUserByUsername(String username);
    
        boolean addUser(String username,String loginname,String password,String phone);
    
        boolean updateUser(String userid,String username,String loginname,String password,String phone);
    
        boolean deleteUser(String userid);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    1.5 userMapper.xml

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.example.demo.mapper.UserMapper">
        <select id="queryUserList" resultType="com.example.demo.entity.User">
            select * from user;
        select>
    
        <select id="queryUserByUsername" resultType="com.example.demo.entity.User">
            select * from user where username=#{param1};
        select>
    
        <insert id="addUser">
            insert into user (username,loginname,password,phone)
            values (#{param1},#{param2},#{param3},#{param4});
        insert>
    
        <update id="updateUser">
            update user set username=#{param2},loginname=#{param3},password=#{param4},phone=#{param5}
            where userid=#{param1};
        update>
    
        <delete id="deleteUser">
            delete from user where userid = #{param1}
        delete>
    
    
    mapper>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    1.6 UserService

    package com.example.demo.service;
    
    import com.example.demo.entity.User;
    
    import java.util.List;
    
    public interface UserService {
        List<User> queryUserList();
    
        User queryUserByUsername(String username);
    
        boolean addUser(String username,String loginname,String password,String phone);
    
        boolean updateUser(String userid,String username,String loginname,String password,String phone);
    
        boolean deleteUser(String userid);
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    1.7 UserServiceImpl

    package com.example.demo.service.imp;
    
    import com.example.demo.entity.User;
    import com.example.demo.mapper.UserMapper;
    import com.example.demo.service.UserService;
    import org.apache.ibatis.annotations.Param;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserMapper userMapper;
    
        @Override
        public List<User> queryUserList() {
            List<User> userList = userMapper.queryUserList();
            return userList;
        }
    
        @Override
        public User queryUserByUsername(String username) {
            User user = userMapper.queryUserByUsername(username);
            //System.out.println("username:"+username);
            //System.out.println("user:"+user);
            return user;
        }
    
        @Override
        public boolean addUser(String username, String loginname, String password, String phone) {
            boolean user = userMapper.addUser(username, loginname, password, phone);
            return user;
        }
    
        @Override
        public boolean updateUser(String userid, String username, String loginname, String password, String phone) {
            boolean updateUser = userMapper.updateUser(userid, username, loginname, password, phone);
            return updateUser;
        }
    
        @Override
        public boolean deleteUser(String userid) {
            boolean deleteUser = userMapper.deleteUser(userid);
            return deleteUser;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    1.8 UserController

    package com.example.demo.controller;
    
    import com.example.demo.entity.User;
    import com.example.demo.service.imp.UserServiceImpl;
    import org.apache.ibatis.annotations.Param;
    import org.springframework.beans.factory.annotation.Autowired;
    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
    public class UserController {
    
        @Autowired
        private UserServiceImpl userService;
    
        @RequestMapping("/queryUserList")
        public List<User> queryUserList() {
            return userService.queryUserList();
        }
    
        @RequestMapping("/queryUserByUsername")
        public User queryUserByUsername(String username) {
            //System.out.println("controller  username:"+username);
            return userService.queryUserByUsername(username);
        }
    
        @RequestMapping("/addUser")
        public String addUser(String username, String loginname, String password, String phone) {
            boolean addUser = userService.addUser(username, loginname, password, phone);
            if (addUser){
                return "添加一名用户成功";
            }else {
                return "添加一名用户失败";
            }
        }
    
        @RequestMapping("/updateUser")
        public String updateUser(String userid, String username, String loginname, String password, String phone) {
            boolean updateUser = userService.updateUser(userid, username, loginname, password, phone);
            if (updateUser){
                return "修改一名用户信息成功";
            }else {
                return "修改一名用户信息失败";
            }
        }
    
        @RequestMapping("/deleteUser")
        public String deleteUser(String userid) {
            boolean deleteUser = userService.deleteUser(userid);
            if (deleteUser){
                return "删除一名用户成功";
            }else {
                return "删除一名用户失败";
            }
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    参考博客

    SpringBoot集成Mybatis这一篇就够了!

  • 相关阅读:
    3DMAX各种拼图建模插件集锦之“彩虹系列”
    Tmall商品详情API接口(商品详情接口,商品列表接口,APP商品详情接口)代码对接教程
    Android-Framework 应用间跳转时,提供 Android Broadcast 通知
    腾讯mini项目-【指标监控服务重构】2023-08-12
    微软确认:测试版用户可在Win11上运行Android应用
    OpenCV图像处理——目标追踪
    java springboot在当前测试类中添加临时属性 不影响application和其他范围
    boltdb一瞥
    python基于PHP+MySQL的网上药店销售购物管理系统
    EfficientViT:高分辨率密集预测的多尺度线性关注
  • 原文地址:https://blog.csdn.net/qq_43757282/article/details/128011607