SpringDataJpa增删改查

资料来源网址:http://www.cnblogs.com/hawell/p/SpringDataJpa.html

Repository(几个常用的例子)

 1 @Repository
 2 public interface StormManageRepository extends JpaRepository<StormManage, Long> {
 3 
 4 //根据id删除StormManage
 5 @Query(value = "delete from bdsp_storm_manage where id=?1 ", nativeQuery = true)
 6 @Modifying
 7 public void deleteStormManageById(int id);
 8 
 9 //根据id获取数据源
10 @Query(value = "select * from bdsp_storm_manage where id=?1 ", nativeQuery = true)
11 public StormManage findStormManageById(int id);
12 
13 //修改Storm的state的状态
14 @Query(value = "update bdsp_storm_manage sm set sm.state=?1 where sm.id=?2 ", nativeQuery = true)
15 @Modifying
16 public void updateStormManageState(int state,int id);
17 
18 //根据id获取数据源
19 @Query(value = "select * from bdsp_storm_manage where enter_topic=?1 ", nativeQuery = true)
20 public List<StormManage> findStormManageByEnterTopic(int enterTopic);
21 
22 }

service层几个例子

 1 @Service
 2 @Transactional
 3 public class StormManageService {
 4 @Resource
 5 private StormManageRepository stormManageRepository;
 6 
 7 /**
 8 * 存储StormManage管理(直接调用save方法,如果想修改的话,需要传一个id)
 9 * */
10 public StormManage saveStormManage(StormManage stormManage){
11 StormManage sm=stormManageRepository.save(stormManage);
12 return sm;
13 }
14 
15 /**
16 * 分页查询
17 * */
18 public Page<StormManage> queryStormBy(Map<String, Object> searchParams, Pageable pageable) {
19 Page<StormManage> queryStormBy = null;
20 try {
21 Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
22 Specification<StormManage> spec = DynamicSpecifications.bySearchFilter(filters.values());
23 queryStormBy = stormManageRepository.findAll(spec, pageable);
24 } catch (Exception e) {
25 e.printStackTrace();
26 }
27 return queryStormBy;
28 }
29 
30 /**
31 * 根据id删除StormManage
32 * */
33 public void deleteStormManageById(int id){
34 stormManageRepository.deleteStormManageById(id);
35 }  
36 
37 }

 

Controller层(主要是分页查询)

 1 @RequestMapping("/toStormJsp")
 2 public String toStormJsp(HttpServletRequest request, Model model,@ModelAttribute("id") String id,HttpSession session) {
 3 try{
 4 
 5 Map<String, Object> searchParams = new HashMap<String, Object>();
 6 StreamUser user=(StreamUser)session.getAttribute("user");
 7 String superuser=user.getSuperuser();//superuser 0:normal 1:super
 8 if(superuser.equals("1")){
 9 searchParams.put("EQ_userName", "");
10 }else{
11 searchParams.put("EQ_userName",user.getUserName());
12 }
13 
14 Page<StormManage> stormManages = stormManageService.queryStormBy(searchParams, null);
15 List<StormManage> findStormManage = new ArrayList<StormManage>();
16 for (StormManage dm : stormManages) {
17 findStormManage.add(dm);
18 }
19 
20 int page = 0;
21 Sort sort = new Sort(Sort.Direction.DESC, "operateTime");//按时间排序
22 Pageable pager =new PageRequest(page, pageSize,sort);
23 Page<StormManage> page2 = stormManageService.queryStormBy(searchParams, pager);
24 List<StormManageDto> pageList = new ArrayList<StormManageDto>();
25 
26 for (StormManage dm : page2 ) {
27   pageList .add(dm);
28 }
29 int totalRecord=pageList.size();
30 int maxResult=10;
31 int totalPage = totalRecord % maxResult == 0 ? totalRecord / maxResult : totalRecord / maxResult + 1 ;
32 model.addAttribute("totalPage", totalPage);
33 model.addAttribute("totalList",totalRecord);
34 model.addAttribute("ipage",page+1);
35 
36  
37 
38 }catch (Exception e){
39 e.printStackTrace();
40 logger.info(e.getMessage());
41 }
42 return "storm/storm";
43 }

 

posted @ 2016-10-26 16:22  涤新云  阅读(7780)  评论(0编辑  收藏  举报