栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis-plus环境搭建及测试

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis-plus环境搭建及测试

po.xml


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.why
    springbootmybatisplus01
    0.0.1-SNAPSHOT
    springbootmybatisplus01
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        


        
            com.baomidou
            mybatis-plus-boot-starter
            3.3.1.tmp
        


    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


SQL脚本

DROP TABLE IF EXISTS user;
CREATE TABLE user  (
  id int(20) NOT NULL COMMENT '主键ID',
  name varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',
  age int(11) NULL DEFAULT NULL COMMENT '年龄',
  email varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱',
  PRIMARY KEY (id) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

INSERT INTO user VALUES (1, '张三', 18, '123qq.com');
INSERT INTO user VALUES (2, '李四', 19, '345qq.com');
INSERT INTO user VALUES (3, '王五', 18, '678qq.com');

User实体类

package com.why.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.stereotype.Component;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class User {
    private static final long serialVersionUID=1L;
    @TableId(value = "id", type = IdType.AUTO)//id 自增
    private Integer id;
    private String name;
    private Integer age;
    private String email;

    public User(String name, Integer age, String email) {
        this.name = name;
        this.age = age;
        this.email = email;
    }
}

UserMapper

package com.why.mapper;

import com.baomidou.mybatisplus.core.mapper.baseMapper;
import com.why.pojo.User;
import org.springframework.stereotype.Component;



@Component
public interface UserMapper extends baseMapper{

}

package com.why;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.why.mapper")
public class Springbootmybatisplus01Application {
    public static void main(String[] args) {
        SpringApplication.run(Springbootmybatisplus01Application.class, args);
    }

}

application.properties

#com.mysql.cj.jdbc.Driver:springBoot2.1以上要加上cj
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#serverTimezone=GMT%2B8:添加时区
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?serverTimezone=GMT%2B8
spring.datasource.username=mybatisplus
spring.datasource.password=mybatisplus

#mybatis日志:添加后可以查看执行的sql语句
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

测试方法

package com.why;

import com.why.mapper.UserMapper;
import com.why.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springbootmybatisplus01ApplicationTests {
    @Autowired private UserMapper userMapper;

    @Test
    void contextLoads() {

    }

    @Test
    void insert(){
        userMapper.insert(new User("张三",20,"529044029@qq.com"));
    }

}

项目结构

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/337826.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号