第一步:创建一个SpringBoot工程,只选择MySQL驱动即可
第二步:导入MybatisPlus依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
第三步:在application.yml中配置数据源
spring:
datasource:
password: 123456
username: root
url: jdbc:mysql://localhost:3306/homework?serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
第四步:编写实体类
package com.gaoyang.pojo;
public class Student {
private String sno;
private String sname;
private String sex;
private Integer age;
private String dept;
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getName() {
return sname;
}
public void setName(String sname) {
this.sname = sname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Student{" +
"sno='" + sno + '\'' +
", sname='" + sname + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", dept='" + dept + '\'' +
'}';
}
}
第五步:编写dao接口
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
}
第六步:测试获取全部数据selectAll
@SpringBootTest
class Mybatisplus01HelloworldApplicationTests {
@Autowired
private StudentMapper studentMapper;
@Test
void testMp() {
List<Student> students = studentMapper.selectList(null);
System.out.println(students);
}
}