一、关于实体类的配置这里不再简述(博客前一篇文章已经描述了)
二、Repository(即dao类)的配置
@Repository
public interface UsersRepository extends JpaRepository<Users,Integer> {
}
三、sevice层的配置
1.service接口层
public interface UsersService {
void addUser(Users user);
void deleteUserById(int uid);
void updateUser(Users user);
Users selectUserById(int uid);
List<Users> selectAllUsers();
void addUserList(List<Users> usersList);
}
2.service实现层
@Service
public class UsersServiceImpl implements UsersService {
@Resource
private UsersRepository usersRepository;
@Override
public void addUser(Users user) {
usersRepository.save(user);
}
@Override
public void deleteUserById(int uid) {
usersRepository.deleteById(uid);
}
@Override
public void updateUser(Users user) {
usersRepository.save(user);
}
@Override
public Users selectUserById(int uid) {
return usersRepository.getOne(uid);
}
@Override
public List<Users> selectAllUsers() {
return usersRepository.findAll();
}
@Override
public void addUserList(List<Users> usersList) {
usersRepository.saveAll(usersList);
}
四、测试类的配置
@RunWith(SpringRunner.class)
@SpringBootTest
public class UsersServiceTest {
@Resource
private UsersService usersService;
@Test
public void addUser(){
Users users = new Users();
users.setUsername("张三");
users.setPassword("123456");
users.setGender("男");
usersService.addUser(users);
}
@Test
public void deleteUserById(){
usersService.deleteUserById(2);
}
@Test
public void updateUser(){
Users users = new Users();
users.setUid(1);
users.setUsername("张三");
users.setPassword("123456");
users.setGender("女");
usersService.updateUser(users);
}
@Test
public void selectAllUsers(){
List<Users> usersList = new ArrayList<>();
usersList = usersService.selectAllUsers();
for(Users u:usersList){
System.out.println(u);
}
}
@Test
public void addUserList(){
List<Users> usersList = new ArrayList<>();
for(int i=0;i<50;i++){
Users users = new Users();
users.setUsername("赵"+i);
users.setPassword("2345");
users.setGender("男");
users.setBirthday("1211-12-03");
users.setAvatar("unkonw.jpg");
usersList.add(users);
}
usersService.addUserList(usersList);
}