添加pom.xml依賴
< dependency> < groupId> cn.zhxu</ groupId> < artifactId> bean-searcher-boot-starter</ artifactId> < version> 4.2.9</ version>
</ dependency>
在controller層注入
@Autowired private MapSearcher mapSearcher; @Autowired private BeanSearcher beanSearcher; @GetMapping ( "findCustomer" ) public ResponseEntity < ? > findCustomer ( ) { HashMap < String , Object > map = new HashMap < > ( ) ; map. put ( "phone" , "2" ) ; mapSearcher. searchList ( CustomerOne . class , map) ; List < CustomerOne > customerOnes = beanSearcher. searchList ( CustomerOne . class , map) ; return ResponseEntity . ok ( ) . body ( customerOnes) ; }
entity實體類
@Data
@TableName ( "customer" )
@SearchBean ( tables= "customer" , autoMapTo = "customer" )
public class CustomerOne { private String id; private String phone; private String address; }
SQL 日志
logging : level : cn.zhxu.bs : DEBUG
有了實體類后,接下來我們便用 MapSearcher 的 search(Class beanClass, Map<String, Object> params): SearchResult<Map<String, Object>> 方法來體驗一下如何 只用一行代碼 實現一個檢索接口,代碼如下
@RestController
@RequestMapping ( "/user" )
public class UserController { @Autowired private MapSearcher mapSearcher; @GetMapping ( "/index" ) public SearchResult < Map < String , Object > > index ( HttpServletRequest request) { return mapSearcher. search ( User . class , MapUtils . flat ( request. getParameterMap ( ) ) ) ; } }