javaEEday08-Mybatis-入门2
This commit is contained in:
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
@@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest//springboot整合junit单元测试的注解
|
||||
class SpringbootMybatisQuickstartApplicationTests {
|
||||
//测试mybatis,查询所有的用户信息到java工程中
|
||||
|
||||
@Test
|
||||
//依赖注入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);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user