1.引入mybatis依赖坐标
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.编写yml文件,配置jdbc信息
#datasource
spring:
datasource:
url: jdbc:mysql://localhost:3306/user
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
3.编写接口
@Mapper
@Repository
public interface UserDao {
@Select("select * from users")
List<User> findAll();
}
-
进行调用测试
@SpringBootTest class SpringbootMybatisApplicationTests { @Autowired private UserDao userDao; @Test void contextLoads() { List<User> users = userDao.findAll(); for (User user : users) { System.out.println(user); } }}