码迷,mamicode.com
首页 > 其他好文 > 详细

UserCrudRepository

时间:2019-08-17 20:06:33      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:findall   setname   get   runner   oid   总数   user   pos   null   

public interface UserCrudRepository extends CrudRepository<User, Long> {
}
1
2
UserCrudRepositoryTests:

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserCrudRepositoryTests {

@Autowired
private UserCrudRepository userCrudRepository;

/**
* 新增
*/
@Test
public void add() {
User user = new User();
user.setName("ljk2");
user.setSex("1");
user.setAge(18);
user.setAddress("beijing");
User result = userCrudRepository.save(user);
Assert.assertNotNull(result);
}

/**
* 修改
*/
@Test
public void edit() {
User user = new User();
user.setId(1l);
user.setName("ljk2edit");
user.setSex("1");
user.setAge(18);
user.setAddress("beijing");
User result = userCrudRepository.save(user);
Assert.assertNotNull(result);
}

/**
* 根据id查询
*/
@Test
public void findById() {
Optional<User> userOptional = userCrudRepository.findById(1L);
User result = userOptional.get();
System.out.println(result);
}

/**
* 查询所有
*/
@Test
public void findAll() {
List<User> userList = (List<User>) userCrudRepository.findAll();
System.out.println(userList);
}

/**
* 计算总数
*/
@Test
public void count() {
long count = userCrudRepository.count();
System.out.println(count);
}

}

UserCrudRepository

标签:findall   setname   get   runner   oid   总数   user   pos   null   

原文地址:https://www.cnblogs.com/hyhy904/p/11369951.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!