springboot基础加进阶(三) (整合第三方技术junit、mybatis、mybatis-plus、druid)

173 阅读1分钟

本人已参与[新人创作礼]活动,一起开启掘金创作之路。


在这里插入图片描述

一、整合junit

springboot在测试时分两步:
		*1.注入你要测试的对象
		*2.执行要测试的对象对应的方法

在这里插入图片描述 在这里插入图片描述

二、整合mybatis

2.1 创建新模块

在这里插入图片描述

2.2 选择使用的技术集

在这里插入图片描述

2.3 在yml中配置数据源:

在这里插入图片描述

2.4 定义dao层接口与映射的配置

在这里插入图片描述

2.5 测试

在这里插入图片描述

三、 整合mybatis-plus

在这里插入图片描述 在这里插入图片描述

3.1 mp目录

在这里插入图片描述

package cn.hncj.Dao;

import cn.hncj.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
 * Created on 2022/5/5.
 *
 * @author Hou chaof
 */
@Mapper
//
public interface UserDao extends BaseMapper<User> {

 /*
    使用mp不需要写sql,只需继承BaseMapper<>
    @Select("select * from tb_user where id= #{id}")
    public User getById(Integer id);*/
}

package cn.hncj.entity;

import lombok.Data;

/**
 * Created on 2022/5/5.
 *
 * @author Hou chaof
 */
@Data
public class User {
    private Integer id;
    private String username;
    private String password;
}

package cn.hncj;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootMybatisPlusApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisPlusApplication.class, args);
    }

}


spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db1
    username: root
    password:


mybatis-plus:
  global-config:
    db-config:
      table-prefix: tb_

package cn.hncj;

import cn.hncj.Dao.UserDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootMybatisPlusApplicationTests {
    @Autowired
    private UserDao userDao;
    @Test
    void contextLoads() {
        System.out.println(userDao.selectById(2));
    }

}

测试结果通过: 在这里插入图片描述

四、 整合druid

在这里插入图片描述