博客主页:不会压弯的小飞侠
✨欢迎关注:点赞收藏⭐留言✒
✨系列专栏:SpringBoot专栏(每日更新)
SpringBoot快速入门(IDEA联网版): 点击查看
SpringBoot - 无需idea联网的springboot项目:点击查看
SpringBoot入门案例-阿里云版和纯手工版:点击查看
SpringBoot-基础配置和属性配置详解:点击查看
SpringBoot-yaml语法规则和读取数据:点击查看
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
欢迎大佬指正,一起学习!一起加油!
创建一个tbl_book表
详细步骤操作 点击直接查看
准备工作完成后,正式进入SpringBoot整合第三方技术的学习。
⭐⭐⭐注意:
创建SpringBoot项目时使用的是阿里云版,详细教程在springboot专栏:SpringBoot专栏
注意:java version与自己配置的jdk版本保持一致
⭐⭐⭐注意:什么也不勾选。
去掉这两行代码。
<relativePath/> <!-- lookup parent from repository -->
<name>springboot_03_junit</name>
<description>Demo project for Spring Boot</description>
BookDao:
package com.jkj.dao;
public interface BookDao {
public void save();
}
BookDaoImpl:
package com.jkj.dao.impl;
import com.jkj.dao.BookDao;
import org.springframework.stereotype.Repository;
@Repository
public class BookDaoImpl implements BookDao {
@Override
public void save() {
System.out.println("BookDao is Running...");
}
}
package com.jkj;
import com.jkj.dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot03JunitApplicationTests {
//注入你要测试的对象
@Autowired
private BookDao bookDao;
@Test
void contextLoads() {
//执行要测试对象对应的方法
bookDao.save();
}
}
作用:设置Junit加载的springboot启动类
属性:classes:设置springboot启动类
⭐⭐⭐注意:如果测试类不在启动包中,则需要设置属性classes
@SpringBootTest(classes = Springboot03JunitApplication.class)
选择:
#配置相关信息
spring:
datasource:
driver-class-name=com: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
username: root
password: root
package com.jkj.domain;
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 + '\'' +
'}';
}
}
package com.jkj.dao;
import com.jkj.domain.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface BookDao {
@Select("select * from tbl_book where id=#{id}")
public Book getById(int id);
}
package com.jkj;
import com.jkj.dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot03MybatisApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void contextLoads() {
System.out.println(bookDao.getById(2));
}
}
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
driver-class-name=com: com.mysql.cj.jdbc.Driver
选择:
#配置相关信息
spring:
datasource:
driver-class-name=com: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
username: root
password: root
package com.jkj.domain;
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 + '\'' +
'}';
}
}
package com.jkj.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jkj.domain.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface BookDao extends BaseMapper<Book> {
}
}
package com.jkj;
import com.jkj.dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot03MybatisPlusApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void contextLoads() {
System.out.println(bookDao.selectById(2));
}
}
⭐⭐⭐注意:
此时会报错:Table ‘springboot.book’ doesn’t exist
需要在yml中设置MP配置相关信息。
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_
添加后重新测试即可。
可以在这个网站搜索依赖直接点击跳转复制即可Maven Rrepository
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.11</version>
</dependency>
两种方式:
#配置相关信息
spring:
datasource:
driver-class-name=com: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
#spring:
# datasource:
# druid:
# driver-class-name=com: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
# username: root
# password: root
package com.jkj.domain;
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 + '\'' +
'}';
}
}
package com.jkj.dao;
import com.jkj.domain.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface BookDao {
@Select("select * from tbl_book where id=#{id}")
public Book getById(int id);
}
package com.jkj;
import com.jkj.dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootO3DruidApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void contextLoads() {
System.out.println(bookDao.getById(3));
}
}