首页 > 代码库 > SpringMVC 强大的注解方式,走一个流程,看看,这些注解用过么
SpringMVC 强大的注解方式,走一个流程,看看,这些注解用过么
-
实现方式一:
- Controller层:
1 import java.util.List; 2 import java.util.Map; 3 import java.util.NoSuchElementException; 4 import java.util.Optional; 5 6 import org.apache.commons.lang3.StringUtils; 7 import org.bson.types.ObjectId; 8 import org.slf4j.helpers.MessageFormatter; 9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.data.domain.Page; 11 import org.springframework.data.domain.PageRequest; 12 import org.springframework.data.domain.Pageable; 13 import org.springframework.data.mongodb.core.MongoTemplate; 14 import org.springframework.http.HttpStatus; 15 import org.springframework.http.MediaType; 16 import org.springframework.http.ResponseEntity; 17 import org.springframework.security.access.prepost.PreAuthorize; 18 import org.springframework.security.core.annotation.AuthenticationPrincipal; 19 import org.springframework.security.core.userdetails.UserDetails; 20 import org.springframework.util.Assert; 21 import org.springframework.web.bind.annotation.GetMapping; 22 import org.springframework.web.bind.annotation.PatchMapping; 23 import org.springframework.web.bind.annotation.PathVariable; 24 import org.springframework.web.bind.annotation.RequestBody; 25 import org.springframework.web.bind.annotation.RequestMapping; 26 import org.springframework.web.bind.annotation.RestController; 27 28 import lombok.extern.slf4j.Slf4j; 29 30 /** 31 * Created by wangqb/BGStone on 2017-07-11 32 */ 33 @RestController 34 @RequestMapping("/wqb") 35 @Slf4j 36 public class DisputableNoteController { 37 @Autowired 38 NoteDBRepository noteDBRepository; 39 40 @Autowired 41 EntityRepository entityRepository; 42 43 @Autowired 44 MongoTemplate mongoTemplate; 45 46 @PreAuthorize("hasRole(‘" + admin + "‘) ") 47 @GetMapping(value = "http://www.mamicode.com/querypage/{pageNo:[//d]+}/{pageSize:[//d]+}", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}) 48 public ResponseEntity<?> queryData(@AuthenticationPrincipal UserDetails activeUser, 49 @PathVariable String pageNo, 50 @PathVariable String pageSize){ 51 52 logger.debug("User ‘{}‘ is allocating note.", activeUser.getUsername()); 53 int page = 1; 54 int size = 20; 55 if (pageNo != null) { 56 page = Integer.valueOf(pageNo); 57 } 58 if (pageSize != null) { 59 size = Integer.valueOf(pageSize); 60 } 61 62 63 Pageable pageable = new PageRequest(page,size); 64 65 Page<Entity> entityPage = entityReponsitory.findByStatus(pageable); 66 67 68 logger.debug("List<Invoice>.Size() ‘{}‘ was allocated to user ‘{}‘ for typing.", entityPage.getSize() <= 0 ? null : entityPage.getContent().size(), 69 activeUser.getUsername()); 70 71 System.out.println("List<Entity>.all() " + result.toString() +" was allocated to user "+ activeUser.getUsername() +" for typing."); 72 73 logger.info("List<Entity>.content() ‘{}‘ was allocated to user ‘{}‘ for typing.", entityPage.getSize() <= 0 ? null :entityPage.getContent().size(), 74 activeUser.getUsername()); 75 return ResponseEntity.status(HttpStatus.OK).body(entityPage); 76 } 77 78 @PreAuthorize("hasRole(‘" + admin +"‘)") 79 @GetMapping(value = http://www.mamicode.com/{"/find/{id:.+}/data"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}) 80 public ResponseEntity<?> findById(@AuthenticationPrincipal UserDetails activeUser, 81 @PathVariable("id") String noteId){ 82 logger.debug("User ‘{}‘ is allocating note.", activeUser.getUsername()); 83 84 System.out.println(" Optional<Entity> "+ entityOptional.get().toString() +" was allocated to user "+ activeUser.getUsername() +" for typing."); 85 86 logger.debug("Invoice ‘{}‘ was allocated to user ‘{}‘ for typing.", entityOptional == null ? null : entityOptional.get(), 87 activeUser.getUsername()); 88 return ResponseEntity.status(HttpStatus.OK).body(entityOptional); 89 } 90 }
- 数据持久层 Repository的代码
1 import org.bson.types.ObjectId; 2 import org.springframework.data.domain.Page; 3 import org.springframework.data.domain.Pageable; 4 import org.springframework.data.mongodb.repository.MongoRepository; 5 import org.springframework.data.mongodb.repository.Query; 6 import org.springframework.data.rest.core.annotation.RepositoryRestResource; 7 import org.springframework.data.rest.core.annotation.RestResource; 8 import org.springframework.security.access.prepost.PreAuthorize; 9 10 import java.util.Optional; 11 12 /** 13 * Created by *** on 2017/3/28. 14 */ 15 @RepositoryRestResource(collectionResourceRel = "test", path = "testpath", exported = true) 16 public interface EntityRepository extends MongoRepository<Entity, String>, EntityRepositoryCustom, 17 EntityAllotRepositoryCustom { 18 19 /* 20 * Created by wangqb on 2017-07-11 21 */ 22 @Query(value="http://www.mamicode.com/{‘status‘: ‘condition‘ }") 23 Page<Entity> findByStatus(Pageable pageable); 24 }
-
实现方式二:
查询条件版二:
1 int page = 1; 2 int size = 20; 3 if (pageNo != null) { 4 page = Integer.valueOf(pageNo); 5 } 6 if (pageSize != null) { 7 size = Integer.valueOf(pageSize); 8 } 9 10 Pageable pageable = new PageRequest(page,size); 11 12 Invoice invoiceVo = new Invoice(); 13 invoiceVo.setStatus(Invoice.Status.Incorrect); 14 ExampleMatcher matcher = ExampleMatcher.matching() 15 .withMatcher("status", GenericPropertyMatchers.exact()); 16 Example example = Example.of(invoiceVo); 17 18 Page<Invoice> invoicePage = invoiceRepository.findAll(example, pageable);
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #777777 }</style>
SpringMVC 强大的注解方式,走一个流程,看看,这些注解用过么
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。