Tianyi's Blog Tianyi's Blog
首页
  • 计算机网络
  • 操作系统
  • 计算机科学
  • Nginx
  • Vue框架
  • 环境配置
  • Java
  • JVM
  • Spring框架
  • Redis
  • MySQL
  • RabbitMQ
  • Kafka
  • Mirror Sites
  • Dev Tools
  • Docker
  • Jenkins
  • Scripts
  • Windows
  • 科学上网
  • 旅行
  • 网站日记
  • 软件
  • 电子产品
  • 杂野
  • 分类
  • 友情链接
GitHub (opens new window)

Tianyi

一直向前,永不停止
首页
  • 计算机网络
  • 操作系统
  • 计算机科学
  • Nginx
  • Vue框架
  • 环境配置
  • Java
  • JVM
  • Spring框架
  • Redis
  • MySQL
  • RabbitMQ
  • Kafka
  • Mirror Sites
  • Dev Tools
  • Docker
  • Jenkins
  • Scripts
  • Windows
  • 科学上网
  • 旅行
  • 网站日记
  • 软件
  • 电子产品
  • 杂野
  • 分类
  • 友情链接
GitHub (opens new window)
  • Java

  • Golang

  • JVM的奇妙世界

  • Spring

    • SSM-Java开发工具链
    • SSM-IOC&AOP
    • MyBatis
    • MyBatis-Plus
    • mybatis搭建demo
      • SSM-JUnit、文件上传与拦截器
      • Spring基础
      • Spring Framework6
    • Spring增强封装

    • Redis

    • MySQL

    • RabbitMQ

    • Kafka

    • 分享

    • 后端
    • Spring
    tianyi
    2021-03-24
    目录

    mybatis搭建demo

    # SQL准备

    /*
     Navicat Premium Data Transfer
    
     Source Server         : localhost
     Source Server Type    : MySQL
     Source Server Version : 80029
     Source Host           : localhost:3306
     Source Schema         : ssm
    
     Target Server Type    : MySQL
     Target Server Version : 80029
     File Encoding         : 65001
    
     Date: 24/03/2024 10:01:31
    */
    
    SET NAMES utf8mb4;
    SET FOREIGN_KEY_CHECKS = 0;
    
    -- ----------------------------
    -- Table structure for user
    -- ----------------------------
    DROP TABLE IF EXISTS `user`;
    CREATE TABLE `user`  (
      `id` bigint NOT NULL COMMENT '主键ID',
      `name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '姓名',
      `age` int NULL DEFAULT NULL COMMENT '年龄',
      `email` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '邮箱',
      PRIMARY KEY (`id`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
    
    -- ----------------------------
    -- Records of user
    -- ----------------------------
    INSERT INTO `user` VALUES (1, 'Jone', 18, 'test1@baomidou.com');
    INSERT INTO `user` VALUES (2, 'Jack', 20, 'test2@baomidou.com');
    INSERT INTO `user` VALUES (3, 'Tom', 28, 'test3@baomidou.com');
    INSERT INTO `user` VALUES (4, 'Sandy', 21, 'test4@baomidou.com');
    INSERT INTO `user` VALUES (5, 'Billie', 24, 'test5@baomidou.com');
    
    SET FOREIGN_KEY_CHECKS = 1;
    
    
    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

    # 主类

    @RestController
    public class UserController {
        
        @Autowired
        private UserMapper userMapper;
        
        @GetMapping("/haha")
        public String getUser() {
            List<Userdao> userdaos = userMapper.queryUserList();
            for (Userdao userdao : userdaos) {
                System.out.println(userdao);
            }
            return "successs";
        }
    }
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    @Data
    public class Userdao {
        private Long id;
        private String name;
        private Integer age;
        private String email;
    }
    
    1
    2
    3
    4
    5
    6
    7
    @Mapper
    @Repository
    public interface UserMapper {
        //只是整合测试,为了可读性,只写了一个方法
        List<Userdao> queryUserList();
    }
    
    1
    2
    3
    4
    5
    6
    @SpringBootApplication
    @MapperScan("com.ziki.mybatis_delete.mapper")
    public class MybatisDeleteApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MybatisDeleteApplication.class, args);
        }
    
    }
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    # 配置

    spring:
      datasource:
        username: root
        url: jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
    mybatis:
      mapper-locations: classpath:mapper/*.xml
      type-aliases-package: com.ziki.mybatis_delete.dao
    
    1
    2
    3
    4
    5
    6
    7
    8
    9

    # mapper文件

    <?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="!!!!!!!!!!!!!">
    
        <select id="queryUserList" resultType="!!!!!!!!!!!">
            select * from user
        </select>
    </mapper>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    完善页面 (opens new window)
    MyBatis-Plus
    SSM-JUnit、文件上传与拦截器

    ← MyBatis-Plus SSM-JUnit、文件上传与拦截器→

    最近更新
    01
    JDK
    02-23
    02
    BadTasteCode && 优化
    09-11
    03
    Gradle 实践操作指南及最佳实践
    09-11
    更多文章>
    Theme by Vdoing | Copyright © 2021-2025 Tandy | 粤ICP备2023113440号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式