pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
</dependencies>
yml
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3306/spring
username: root
password: ...
driver-class-name: com.mysql.cj.jdbc.Driver
mapper接口
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.test02.pojo.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
测试类
import com.example.test02.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testGetUser(){
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
}
}
结果
User(id=1, username=Jone, password=123, age=18, email=jone@qq.com)
User(id=2, username=Jack, password=123, age=20, email=jack@qq.com)
User(id=3, username=Tom, password=123, age=28, email=tom@qq.com)
User(id=5, username=poole, password=123, age=24, email=poole@qq.com)