springboot整合Junit,Mybatis,Mybatis-Plus,Druid

篇幅有点长,所以把总结写在开头:

springboot整合第三方万能公式:

1.新建工程时选择需要的starter或者手动添加对应的starter

2.配置对应的设置或者采用默认配置

一、springboot整合junit

1.导入Junit坐标到pom.xml(创建springboot工程时将自动导入此坐标,我们只需要检查一下就好了)


      org.springframework.boot
      spring-boot-starter-test
      test

 2.编写dao层数据

springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第1张图片 

springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第2张图片

注意:启动类和测试类要放在同一个包路径下,否则在编译时会出错springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第3张图片

二、springboot整合MyBatis

1.最最关键的一步就是在创建工程的时候选择MyBatis Framework以及MySQL Driver

springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第4张图片

 可以检查一下pom文件springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第5张图片

 2.编写配置文件,数据源

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

 放一下数据库数据~springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第6张图片

 

 开始使用

编写实体类


public class book {
    private int id;
    private String type;
    private String name;
    private String description;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "book{" +
                "id=" + id +
                ", type='" + type + '\'' +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}

 编写dao层

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface bookDao {
    @Select("select * from heima1 where id =#{id}")
    public book getById(Integer id);
}

 在测试类中进行测试:

@SpringBootTest
class Springboot07MybatisApplicationTests {
    @Autowired
    private com.ws.springboot_07_mybatis.dao.bookDao bookDao;

    @Test
    void contextLoads() {
        System.out.println(bookDao.getById(1));
    }

}

成功!!!! 

springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第7张图片

三、springboot整合MyBatis -Puls

1.需要找MyBatis -Puls在下面的链接中Maven Repository: Search/Browse/Explore (mvnrepository.com)springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第8张图片

选择一个版本点击进入,复制maven内容

springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第9张图片 粘贴进入工程的pom文件,刷新

springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第10张图片

 2.编写配置文件

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

 3.编写实体类

package com.ws.domain;

public class heima1 {
    private int id;
    private String type;
    private String name;
    private String description;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "book{" +
                "id=" + id +
                ", type='" + type + '\'' +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}

4.编写dao层(注意;这里的@Mapper和继承的BaseMapper<实体类>是mybatis-plus的关键之一),接下来什么方法也不用写,mybatis-plus已经帮你写好了!!!

package com.ws.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ws.domain.heima1;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface bookDao extends BaseMapper {

}

5.进行测试

package com.ws;

import com.ws.dao.bookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot08MybatisplusApplicationTests {

    @Autowired
    private com.ws.dao.bookDao bookDao;
    @Test
    void contextLoads() {
        System.out.println(bookDao.selectById(2));

    }

}

 都是增删改查方法,需要大家自己去探索

springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第11张图片

 好了,看测试结果,成功!!!!

springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第12张图片

 四、SpringBoot整合Druid

 1.需要找Druid在下面的链接中Maven Repository: Search/Browse/Explore (mvnrepository.com)

springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第13张图片进行复制

springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第14张图片

 粘贴进入pom文件,刷新

springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第15张图片

连接数据源

springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第16张图片

编写实体类

package com.ws.domin;

public class book {
    private int id;
    private String type;
    private String name;
    private String description;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "book{" +
                "id=" + id +
                ", type='" + type + '\'' +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}

编写dao层

package com.ws.dao;


import com.ws.domin.book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface bookDao {
    @Select("select * from heima1 where id =#{id}")
    public book getById(Integer id);
}

编写测试类

package com.ws;

import com.ws.dao.bookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot08DruidApplicationTests {

    @Autowired
    private bookDao bookDao;

    @Test
    void contextLoads() {
        System.out.println(bookDao.getById(1));
    }

}

 运行,测试成功!!!

 springboot整合Junit,Mybatis,Mybatis-Plus,Druid_第17张图片

 

你可能感兴趣的:(mybatis,junit,spring,boot)