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


    0. mysql 建表代码

    DROP TABLE IF EXISTS `user`;
    CREATE TABLE `user` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '序号',
    `account` varchar(25) 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 (`id`) USING BTREE
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='用户表';
    
    
    INSERT INTO user (account,password,phone)  VALUES 
    ('ken','ken123','19977778888'),
    ('jianghai','jianghai123','19977778888');
    
    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.wmgroupId>
        <artifactId>stumsartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>stumsname>
        <description>Demo project for Spring Bootdescription>
        <properties>
            <java.version>1.8java.version>
        properties>
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-thymeleafartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>2.1.1version>
            dependency>
    
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <scope>runtimescope>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-jdbcartifactId>
            dependency>
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombokgroupId>
                                <artifactId>lombokartifactId>
                            exclude>
                        excludes>
                    configuration>
                plugin>
            plugins>
            <resources>
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.xmlinclude>
                    includes>
                    <filtering>truefiltering>
                resource>
            resources>
        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
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87

    1.2 application.properties

    server.port=8999
    
    #连接数据库
    spring.datasource.username=xxxxx
    spring.datasource.password=xxxxx
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/stums?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    
    #配置mybatis
    mybatis.mapper-locations=classpath:mapper/*.xml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1.3 UserController

    package com.wm.stums.controller;
    
    import com.wm.stums.entity.User;
    import com.wm.stums.mapper.UserMapper;
    
    import org.apache.ibatis.annotations.Param;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    @RestController
    public class UserController {
    
        @Autowired
        private UserMapper userMapper;
    
        @RequestMapping("/queryUserList")
        public List queryUserList(){
            List<User> users = userMapper.queryUserList();
            return users;
        }
    
        @RequestMapping("/queryUserByAccount")
        public User queryUserByAccount(@Param("account") String account){
    
            User user = userMapper.queryUserByAccount(account);
            if (user==null){
                User userNull = new User();
                userNull.setAccount("该用户不存在!");
                return userNull;
            }
            return user;
        }
    
        @RequestMapping("/addUser")
        public boolean addUser (String account, String password, String phone){
            System.out.println(account+"__"+password+"__"+phone);
            boolean addUser = userMapper.addUser(account, password, phone);
            return addUser;
        }
    
        @RequestMapping("/updateUser")
        public boolean updateUser (String id,String account,String password,  String phone){
            System.out.println(id+"__"+account+"__"+password+"__"+phone);
            boolean updateUser = userMapper.updateUser(id, account, password, phone);
            return updateUser;
        }
    
        @RequestMapping("/deleteUser")
        public boolean deleteUser (String id){
            System.out.println("id:"+id);
            boolean deleteUser = userMapper.deleteUser(id);
            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
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

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

    package com.wm.stums.entity;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.io.Serializable;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class User implements Serializable {
    
        private String id;
    
        private String account;
    
        private String password;
    
        private String phone;
    
    
        @Override
        public String toString() {
            return "User{" +
                    "id='" + id + '\'' +
                    ", account='" + account + '\'' +
                    ", 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
    • 29
    • 30
    • 31
    • 32
    • 33

    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.wm.stums.mapper.UserMapper">
    
        <select id="queryUserList" resultType="com.wm.stums.entity.User">
           select * from user
        select>
    
        <select id="queryUserByAccount" resultType="com.wm.stums.entity.User">
            select * from user
            where account=#{param1}
        select>
    
        <insert id="addUser" >
            insert into user (account, password, phone)
            VALUES (#{param1},#{param2},#{param3})
        insert>
    
        <update id="updateUser" >
            update user
            set account = #{param2},password=#{param3},phone=#{param4}
            where id = #{param1}
        update>
    
        <delete id="deleteUser" >
            delete from user where id = #{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
    • 31
    • 32

    1.6 UserMapper

    package com.wm.stums.mapper;
    
    import com.wm.stums.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 queryUserByAccount(String account);
    
        boolean addUser (String account, String password, String phone);
    
        boolean updateUser(String id, String account, String password, String phone);
    
        boolean deleteUser (String id);
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    参考博客

    SpringBoot集成Mybatis这一篇就够了!

  • 相关阅读:
    定时器之编码器模式
    LeetCode 383.赎金信(模拟,for(char c : 容器)的使用)
    你真会判断DataGuard的延迟吗?
    复习SGI STL二级空间配置器(内存池) | 笔记自用
    【信息安全原理】——传输层安全(学习笔记)
    ReactNative入门(二)——导航和路由
    Hadoop学习总结(Shell操作)
    ElasticSearch(超详细解说)[springBoot整合ES并简单实现增删改查]
    Jmeter接口自动化(八)函数 上
    Codeforces Round #831 (Div. 1 + Div. 2)
  • 原文地址:https://blog.csdn.net/qq_43757282/article/details/127857165