下面的图就是异常报错图
下面是yaml的配置
#配置相关信息第二种:druid专用的配置
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/user_db?serverTimezone=UTC
username: root
password: admin
下面是实体类:
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import org.springframework.stereotype.Repository;
//实体类
@Repository
@TableName(value = "t_book") //当配置文件中没有配置的时候,可以在这个地方确定数据控中的哪个表
public class Book {
// 这里的属性名 要和数据库表中的属性名一致,要不然最终的查询结果是null
@TableField(value = "id")
private int id;
@TableField(value = "bookName")
private String bookName;
@TableField(value = "statue")
private String statue;
@Override
public String toString() {
return "Book{" +
"id='" + id + '\'' +
", bookName='" + bookName + '\'' +
", statue='" + statue + '\'' +
'}';
}
public String getStatue() {
return statue;
}
public void setStatue(String statue) {
this.statue = statue;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
}
下面是Dao接口
```java
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.company.domain.Book;
import org.apache.ibatis.annotations.Mapper;
//定义数据层接口与映射配置
//数据库SQL映射需要添加@Mapper被容器识别到
@Mapper //实体类作为泛型传进去
public interface BookDao extends BaseMapper<Book> {
// extends BaseMapper<Book>里面包含了非常多的方法
}
下面是测试类:
import com.company.Dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = Springboot07DruidApplication.class) //这样就相当于指定了引导类
class Springboot07DruidApplicationTests {
@Autowired
private BookDao bookDao ;
@Test
void contextLoads() {
System.out.println(bookDao.selectById(10086));
}
}