1.UserService
/**
*
* Copyright: Copyright (c) 2012 Asiainfo-Linkage
*
* service@date:2014-3-25
* @ClassName: UserService.java
* @Description: 用户服务接口
*
* @version: v1.0.0上午9:54:56eleven
* @author: elevenSpringDataJpa
*
* Modification History:
* Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 新建
*/
package com.xr.service;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import com.xr.bean.User;
public interface UserService {
/**
*
* @Title: addUser
* @Description: TODO(添加用户)
* @return: void
* @author: eleven
* @date: 2014-3-25
* @history: Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 修改原因
*/
void addUser(User user);
/**
*
* @Title: addUser
* @Description: TODO(批量添加用户)
* @param list
* @return: void
* @author: eleven
* @date: 2014-3-25
* @history: Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 修改原因
*/
void addUser(List list);
/**
*
* @Title: deleteUser
* @Description: TODO(删除用户)
* @param user
* @return: void
* @author: eleven
* @date: 2014-3-25
* @history: Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 修改原因
*/
void deleteUser(User user);
/**
*
* @Title: deleteUser
* @Description: TODO(删除用户)
* @param id
* @return: void
* @author: eleven
* @date: 2014-3-25
* @history: Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 修改原因
*/
void deleteUser(String id);
/**
*
* @Title: deleteUser
* @Description: TODO(删除用户)
* @param list
* @return: void
* @author: eleven
* @date: 2014-3-25
* @history: Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 修改原因
*/
void deleteUser(List list);
/**
*
* @Title: updateUser
* @Description: TODO(编辑用户)
* @param user
* @return: void
* @author: eleven
* @date: 2014-3-25
* @history: Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 修改原因
*/
void updateUser(User user);
/**
*
* @Title: existUser
* @Description: TODO(判断用户是否存在)
* @param id
* @return
* @return: boolean
* @author: eleven
* @date: 2014-3-25
* @history: Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 修改原因
*/
boolean existUser(String id);
/**
*
* @Title: queryUserById
* @Description: TODO(查询用户)
* @param id
* @return
* @return: User
* @author: eleven
* @date: 2014-3-25
* @history: Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 修改原因
*/
User queryUserById(String id);
/**
*
* @Title: queryAllUser
* @Description: TODO(查询用户)
* @return
* @return: List
* @author: eleven
* @date: 2014-3-25
* @history: Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 修改原因
*/
List queryAllUser();
/**
*
* @Title: queryListUser
* @Description: TODO(查询用户)
* @param list
* @return
* @return: List
* @author: eleven
* @date: 2014-3-25
* @history: Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 修改原因
*/
List queryListUser(List list);
/**
*
* @Title: queryAllUserSort
* @Description: TODO(查询用户)
* @param sort
* @return
* @return: List
* @author: eleven
* @date: 2014-3-25
* @history: Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 修改原因
*/
List queryAllUserBySort(Sort sort);
/**
*
* @Title: queryUserByPage
* @Description: TODO(查询用户)
* @param pageable
* @return
* @return: Page
* @author: eleven
* @date: 2014-3-25
* @history: Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 修改原因
*/
Page queryUserByPage(Pageable pageable);
}
/**
*
* Copyright: Copyright (c) 2012 Asiainfo-Linkage
*
* service.impl@date:2014-3-25
* @ClassName: UserServiceImpl.java
* @Description: 用户服务接口实现类
*
* @version: v1.0.0上午9:56:48eleven
* @author: elevenSpringDataJpa
*
* Modification History:
* Date Author Version Description
* ---------------------------------------------------------*
* 2014-3-25 eleven v1.0.0 新建
*/
package com.xr.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xr.bean.User;
import com.xr.repository.UserRepository;
import com.xr.service.UserService;
@Service(value = "userService")
@Transactional
public class UserServiceImpl implements UserService {
@Resource
private UserRepository userRepository;
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
/* (非 Javadoc)
* Title: addUser
* Description:
* @see service.UserService#addUser()
*/
@Override
public void addUser(User user) {
userRepository.save(user);
}
/* (非 Javadoc)
* Title: addUser
* Description:
* @param list
* @see com.xr.service.UserService#addUser(java.util.List)
*/
@Override
public void addUser(List list) {
userRepository.save(list);
}
/* (非 Javadoc)
* Title: deleteUser
* Description:
* @param user
* @see com.xr.service.UserService#deleteUser(com.xr.bean.User)
*/
@Override
public void deleteUser(User user) {
userRepository.delete(user);
}
/* (非 Javadoc)
* Title: deleteUser
* Description:
* @param id
* @see com.xr.service.UserService#deleteUser(java.lang.String)
*/
@Override
public void deleteUser(String id) {
userRepository.delete(id);
}
/* (非 Javadoc)
* Title: deleteUser
* Description:
* @param list
* @see com.xr.service.UserService#deleteUser(java.util.List)
*/
@Override
public void deleteUser(List list) {
userRepository.delete(list);
}
/* (非 Javadoc)
* Title: updateUser
* Description:
* @param user
* @see com.xr.service.UserService#updateUser(com.xr.bean.User)
*/
@Override
public void updateUser(User user) {
userRepository.save(user);
}
/* (非 Javadoc)
* Title: existUser
* Description:
* @param id
* @return
* @see com.xr.service.UserService#existUser(java.lang.String)
*/
@Override
public boolean existUser(String id) {
return userRepository.exists(id);
}
/* (非 Javadoc)
* Title: queryUserById
* Description:
* @param id
* @return
* @see com.xr.service.UserService#queryUserById(java.lang.String)
*/
@Override
public User queryUserById(String id) {
return userRepository.findOne(id);
}
/* (非 Javadoc)
* Title: queryAllUser
* Description:
* @return
* @see com.xr.service.UserService#queryAllUser()
*/
@Override
public List queryAllUser() {
return userRepository.findAll();
}
/* (非 Javadoc)
* Title: queryListUser
* Description:
* @param list
* @return
* @see com.xr.service.UserService#queryListUser(java.util.List)
*/
@Override
public List queryListUser(List list) {
return userRepository.findAll(list);
}
/* (非 Javadoc)
* Title: queryAllUserBySort
* Description:
* @param sort
* @return
* @see com.xr.service.UserService#queryAllUserBySort(org.springframework.data.domain.Sort)
*/
@Override
public List queryAllUserBySort(Sort sort) {
return userRepository.findAll(sort);
}
/* (非 Javadoc)
* Title: queryUserByPage
* Description:
* @param pageable
* @return
* @see com.xr.service.UserService#queryUserByPage(java.awt.print.Pageable)
*/
@Override
public Page queryUserByPage(Pageable pageable) {
return userRepository.findAll(pageable);
}
}
* Copyright: Copyright (c) 2014 Asiainfo-Linkage
package com.xr.service;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.xr.bean.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class UserServiceTest {
@Resource
private UserService userService;
@Test
public void test() {
//新增
// User user = new User();
// user.setId(UUID.randomUUID().toString());
// user.setName("springdatajpa");
// user.setPwd("123456");
// userService.addUser(user);
//批量新增
// User user = null;
// List list = new ArrayList();
// for(int i = 0; i < 10; i++){
// user = new User();
// user.setId(UUID.randomUUID().toString());
// user.setName("name" + i);
// user.setPwd("pwd" + i);
// user.setAge(i);
// list.add(user);
// }
// userService.addUser(list);
//删除
// User user = new User();
// user.setId("009c3691-19ff-4114-987f-9eb36054c768");
// userService.deleteUser(user);
//批量删除
//查询
// List idList = new ArrayList();
// idList.add("06cd2f98-abb5-485e-87b4-8042b84be7ff");
// List list = userService.queryListUser(idList);
// System.out.println(list);
// List list = userService.queryAllUserBySort(new Sort(new Order(Direction.DESC, "name")));
// System.out.println(list);
Page pageUser = userService.queryUserByPage(new PageRequest(1, 3));
System.out.println(pageUser.getContent());
pageUser = userService.queryUserByPage(new PageRequest(1, 3, new Sort(new Order(Direction.DESC, "name"))));
System.out.println(pageUser.getContent());
}
}
其他配置代码不变,测试JpaRepository的CrudApi