SpringBoot——整合Junit&MyBatis&SSM

96 阅读1分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第25天,点击查看活动详情

SpringBoot整合junit

环境准备

image.png

创建 BookService 接口

public interface BookService {
    public void save();
}

创建一个 BookServiceImpl 类,使其实现 BookService

@Service
public class BookServiceImpl implements BookService {
    @Override
    public void save() {
        System.out.println("book service is running ...");
    }
}

编写测试类

@SpringBootTest
class Demo2ApplicationTests {

    @Autowired
    private BookService bookService;

    @Test
    public void save() {
        bookService.save();
    }
}

结果

image.png

SpringBoot整合mybatis

创建模块

  • 选择当前模块需要使用的技术集(MyBatis、MySQL)

image.png

定义实体类

public class Book {
    private Integer id;
    private String name;
    private String type;
    private String description;

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

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getType() {
        return type;
    }

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

    public String getDescription() {
        return description;
    }

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

}

定义dao接口

public interface BookDao {
    @Select("select * from tb_brand where id = #{id}")
    public Book getById(Integer id);
}

定义测试类

@SpringBootTest
class Demo3ApplicationTests {


    @Autowired
    private BookDao bookDao;

    @Test
    void testGetById() {
        Book book = bookDao.getById(1);
        System.out.println(book);
    }
}

编写配置

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

SpringBoot整合SSM

主要从以下几部分完成

1.pom.xml

配置起步依赖,必要的资源坐标(druid)

2.application.yml

设置数据源、端口等

3.配置类

全部删除

4.dao

设置@Mapper

5.测试类

6.页面

放置在resources目录下的static目录中