• MyBatis-Plus中Service用法



    MyBatis-Plus中Service用法

    • MySQL和MyBatis-Plus包
    • SpringBoot配置信息
    • @Mapper 或 @MapperScan 注解
    • BaseMapper< T >接口
    • IService< T > 接口
    • ServiceImpl
    • service自定义方法与mapper一样,接口定义方法,service需要实现接口方法

    1、准备数据库与表

    
    CREATE DATABASE `mybatis_plus` 
    
    USE `mybatis_plus`;
    
    DROP TABLE IF EXISTS `user`;
    
    CREATE TABLE `user` (
      `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
      `user_name` VARCHAR(30) CHARACTER SET utf8mb3 COLLATE utf8_general_ci DEFAULT NULL COMMENT '姓名',
      `age` INT DEFAULT NULL COMMENT '年龄',
      `email` VARCHAR(50) DEFAULT NULL COMMENT '邮箱',
      `sex` VARCHAR(10) DEFAULT NULL,
      PRIMARY KEY (`id`)
    );
    
    
    INSERT  INTO `user`(`id`,`user_name`,`age`,`email`,`sex`) VALUES 
    (1,'李四',21,'list@qq.com','男'),
    (2,'Billie',24,'test5@qq.com','男'),
    (3,'HeXi',20,'test6@qq.com','男'),
    (4,'馨馨',21,'xinxin@qq.com','女'),
    (5,'小红',22,'xiaohong@qq.com','女'),
    (6,'兮兮',20,'xixi@qq.com','女');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述

    2、创建SpringBoot项目SQL(MySQL Driver)

    • 省略

    3、pom.xml导入jar包配置刷新Maven

            
       <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-boot-starterartifactId>
                <version>3.5.2version>
            dependency>
          
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <scope>runtimescope>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    4、配置application.yml

    # 连接数据库信息
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=UTC
        username: root
        password: 111111
    
    # MyBatis-Plus全局配置
    mybatis-plus:
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 配置日志信息
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5、启动类

    package com.sgz.mybatisplus;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @MapperScan("com.sgz.mybatisplus.mapper")   // 启动类配置@MapperScan,mapper接口配置@Mapper,二选一
    public class Day66MybatisplusMapperApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(Day66MybatisplusMapperApplication.class, args);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    6、创建pojo类

    package com.sgz.mybatisplus.pojo;
    
    public class User {
    
        private Integer id;
        private String userName;
        private Integer age;
        private String email;
        private String sex;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", userName='" + userName + '\'' +
                    ", age=" + age +
                    ", email='" + email + '\'' +
                    ", sex='" + sex + '\'' +
                    '}';
        }
    }
    
    
    • 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

    7、mapper接口继承BaseMapper< T >

    package com.sgz.mybatisplus.mapper;
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.sgz.mybatisplus.pojo.User;
    import org.apache.ibatis.annotations.Mapper;
    import org.springframework.stereotype.Repository;
    
    //@Mapper    // 启动类配置@MapperScan,mapper接口配置@Mapper,二选一
    @Repository // 解决报错问题
    public interface UserMapper extends BaseMapper<User> {
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    8、service接口继承IService< T >

    package com.sgz.mybatisplus.service;
    
    import com.baomidou.mybatisplus.extension.service.IService;
    import com.sgz.mybatisplus.pojo.User;
    
    public interface IUserService extends IService<User> {
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    9、serviceImpl实体类继承ServiceImpl和实现 service接口

    package com.sgz.mybatisplus.service.impl;
    
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.sgz.mybatisplus.mapper.UserMapper;
    import com.sgz.mybatisplus.pojo.User;
    import com.sgz.mybatisplus.service.IUserService;
    import org.springframework.stereotype.Service;
    
    @Service
    public class IUserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    10、测试类

    package com.sgz.mybatisplus;
    
    import com.sgz.mybatisplus.mapper.UserMapper;
    import com.sgz.mybatisplus.service.IUserService;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    class Day66MybatisplusMapperApplicationTests {
    
        @Autowired
        private IUserService userService;
    
        @Test
        void contextLoads() {
            System.out.println(userService.getById(1));
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    解的思路、身份认证方法、密码破解方法、字典
    Android BLE 蓝牙开发——扫码枪基于BLESSED
    python基于openpyxl操作excel
    【vue2第十七章】VueRouter 编程式导航跳转传参(点击按钮跳转路由和如何传递参数)
    ES写入数据时:circuit_breaking_exception[[parent] Data too large
    一种利用合法工具渗透的新型方法
    2019阿里java面试题
    KITTI 3D 数据可视化
    单稳态中间继电器UEG/A-4DPDT/DC220V
    什么c++流行造轮子而不是调包侠?
  • 原文地址:https://blog.csdn.net/s17856147699/article/details/126354125