一、创建一个新项目:
1.1 选择---spring innitializr。
1.2 输入项目名称 选择Java版本为8。
1-3 添加本项目中你会获取的依赖,一个✔代表着pom里会出现的坐标。
1-4 点击完成。
1-5 项目创建成功后,检查pom里是否存在这两个坐标,如果没有可以手动添加或者重复以上步骤尝试。
<!--导入对应的starter-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
二、配置
2-1 在application.yml配置数据库链接 注意属性后要空格才能识别变色
#<!--配置相关信息-->
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssmbuiLd?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=true
usename: root
password: 123456
2-2 创建数据库表
2-3 创建数据表对象层--domain-Books类(新手入门还没有用到注解)
package com.itheima.springboot_05_mybatis.damain;
public class Books {
private int bookID;
private String bookName;
private String bookCounts;
private String detail;
public int getBookID() {
return bookID;
}
public void setBookID(int bookID) {
this.bookID = bookID;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookCounts() {
return bookCounts;
}
public void setBookCounts(String bookCounts) {
this.bookCounts = bookCounts;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
@Override
public String toString() {
return "Books{" +
"bookID=" + bookID +
", bookName='" + bookName + '\'' +
", bookCounts='" + bookCounts + '\'' +
", detail='" + detail + '\'' +
'}';
}
}
2-4 创建dao层-BookDao类
package com.itheima.springboot_05_mybatis.dao;
import com.itheima.springboot_05_mybatis.damain.Books;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface BooksDao {
@Select("select * from books where bookID= #{bookID}")
public static Books getBookID(int bookID);
}
三、测试类
package com.itheima.springboot_05_mybatis;
import com.itheima.springboot_05_mybatis.dao.BooksDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot05MybatisApplicationTests {
@Autowired(required=false)
private BooksDao booksDao;
@Test
void contextLoads() {
System.out.println(BooksDao.getBookID(1));
}
}