這篇文章主要講解了“怎么使用Spring注解及代理模式”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么使用Spring注解及代理模式”吧!
成都創(chuàng)新互聯(lián)憑借專業(yè)的設(shè)計(jì)團(tuán)隊(duì)扎實(shí)的技術(shù)支持、優(yōu)質(zhì)高效的服務(wù)意識(shí)和豐厚的資源優(yōu)勢,提供專業(yè)的網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站優(yōu)化、軟件開發(fā)、網(wǎng)站改版等服務(wù),在成都10余年的網(wǎng)站建設(shè)設(shè)計(jì)經(jīng)驗(yàn),為成都1000多家中小型企業(yè)策劃設(shè)計(jì)了網(wǎng)站。
- 1.基本的SSJ的導(dǎo)入 - 2.SpringDataJpa
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>${spring-data-jpa.version}</version> </dependency>
1.配置對(duì)應(yīng)的Spring-data-jpa
<jpa:repositories base-package="cn.itsource.aisell.repository" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager" ></jpa:repositories>
2.繼承JpaRepository interface EmployeeRepository extends JpaRepository<Employee,Long>, JpaSpecificationExecutor<Employee>
3.完成相應(yīng)的CRUD
employeeRepository.findAll(); employeeRepository.findOne(Long id); employeeRepository.save(對(duì)象); //添加或者修改 employeeRepository.delete(id/對(duì)象); employeeRepository.findAll(Pageable) -> 分頁 Pageable pageable = new PageRequest(0, 5); //0就是第一頁 employeeRepository.findAll(Sort) -> 排序 Sort sort = new Sort(Sort.Direction.DESC,"username"); 如果要把分頁和排序結(jié)合起來: new PageRequest(0, 5,sort);
1.名稱規(guī)則 findByUsername(String username) -> 根據(jù)名稱查詢 username = ? findByUsernameLike(String username) -> username like ? findByUsernameLikeAndEmailLike(String username,String email) -> username like ? and email like ? 詳細(xì)規(guī)則請(qǐng)看文件(idea也有提示) 2.Query注解 @Query("jpql的語句") @Query("select o from Employee o where o.name like ?1") //@Query("select o from Employee o where o.name like :name") @Query(nativeQuery = true,value="select * from employee")
可以不寫SQL也能夠完成功能
1.我們的Repository繼承JpaSpecificationExecutor EmployeeRepository extends JpaRepository<Employee,Long>, JpaSpecificationExecutor<Employee> 2.findAll的查詢方法 employeeRepository.findAll(Specification<T> spec) employeeRepository.findAll(Specification<T> spec,Pageable pageable) new Specification<Employee>{ //root:(根,表)獲取字段 //query:where,group by ,order by... //cb: 條件判斷(一個(gè)或者多個(gè)) @Override public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) { Path path = root.get("username");//拿到要做查詢的字段 Predicate p = cb.like(path, "%1%");//like代表做模糊查詢,后面就是它的條件值 return p; } }
<dependency> <groupId>com.github.wenhao</groupId> <artifactId>jpa-spec</artifactId> <version>3.1.0</version> </dependency> //第一個(gè)參數(shù): true -> 條件過濾啟用 //第二個(gè)參數(shù): 需要過濾的屬性 //第三個(gè)參數(shù): 過濾條件的 ?對(duì)應(yīng)的值 Specification<Person> specification = Specifications.<Person>and() .eq(StringUtils.isNotBlank(request.getName()), "name", name) .gt(Objects.nonNull(request.getAge()), "age", 18) .build();
Query作用:接收前臺(tái)傳過來的查詢條件(分頁,數(shù)據(jù)) BaseQuery:公共的查詢【規(guī)范,公共的代碼】 int currentPage=1; getJpaPage{return currentPage-1} int pageSize=10; String orderName; Boolean orderType=true; 提供getter,setter方法 //規(guī)定子類必需提供一個(gè)拿到條件的方法 protected abstract Specification createSpec(); //提供獲取排序的方法 Sort createSort(){ //1.如果orderName沒有值,就返回null【不排序】 //2.如果orderType是DESC,就升級(jí)(反之就降序) } EmployeeQuery:Employee單獨(dú)的查詢 username,email,age,... Specification createSpec(){...}
感謝各位的閱讀,以上就是“怎么使用Spring注解及代理模式”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)怎么使用Spring注解及代理模式這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
網(wǎng)站名稱:怎么使用Spring注解及代理模式
轉(zhuǎn)載源于:http://aaarwkj.com/article20/iggcco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、企業(yè)建站、ChatGPT、網(wǎng)站營銷、關(guān)鍵詞優(yōu)化、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)