1 回答
TA贡献1858条经验 获得超8个赞
UserRepository像这样更新您的内容:
public interface UserRepository extends JpaRepository<User, Integer> {
User findOneByNameAndAge(String name, int age); // Answer point 2
List<User> findByName(String name); // Answer point 3
@Modifying
@Query("delete from User u where u.name = ?1")
int deleteByName(String name); // Answer point 4
@Modifying
@Query("update User u set u.name = ?1 where u.name = ?2")
int updateByName(String newName, String oldName); // Answer point 5
}
请注意,对于您的问题的第1点,JpaRepository已经为您提供了此方法:
userRepository.findOne(2);
使用方法如下:
// Provide necessary annotations..
public class UserRepositoryIntTest {
@Autowired UserRepository userRepository;
@Test
public void testThemAll() {
this.userRepository.findOne(2); // 1
this.userRepository.findOneByNameAndAge("david", 20); // 2
this.userRepository.findByName("david"); // 3
this.userRepository.deleteByName("david"); // 4
this.userRepository.updateByName("dave", "david"); // 5
}
}
添加回答
举报