javaEEday08-Mybatis-入门2

This commit is contained in:
2026-05-31 16:10:43 +08:00
parent 67b08e9e7b
commit 5f1e30d98b
3 changed files with 42 additions and 3 deletions

View File

@@ -1,4 +1,16 @@
package com.inmind.mapper;
import com.inmind.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper//在运行时,会自动生成该接口的实现类对象(代理对象),并且将该对象交给spring的IOC容器
public interface UserMapper {
//定义操作User表的方法增删改查
//查询所有用户信息的方法
@Select("select * from user")//当调用list()方法时会自动发送该SQL语句给数据库mybatis
public List<User> list();
}

View File

@@ -66,4 +66,15 @@ public class User {
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
", phone='" + phone + '\'' +
'}';
}
}

View File

@@ -1,13 +1,29 @@
package com.inmind;
import com.inmind.mapper.UserMapper;
import com.inmind.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootMybatisQuickstartApplicationTests {
import java.util.List;
@Test
@SpringBootTest//springboot整合junit单元测试的注解
class SpringbootMybatisQuickstartApplicationTests {
//测试mybatis查询所有的用户信息到java工程中
//依赖注入UserMapper对象
@Autowired
private UserMapper userMapper;
@Test//测试方法
void contextLoads() {
//调用UserMapper的list()方法,查询所有用户信息
List<User> users = userMapper.list();
System.out.println(users);
users.stream().forEach(user->{
System.out.println(user);
});
}
}