forked from leelance/spring-boot-all
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
spring-boot-jpa-querydsl/src/main/java/com/lance/querydsl/service/CityService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.lance.querydsl.service; | ||
|
||
import java.util.List; | ||
|
||
import com.lance.querydsl.entity.CityEntity; | ||
|
||
public interface CityService { | ||
|
||
/** | ||
* findAll | ||
* @return | ||
*/ | ||
List<CityEntity> findAll(String hotelName); | ||
|
||
/** | ||
* Save | ||
* @param city | ||
*/ | ||
void save(CityEntity city); | ||
|
||
void delete(long id); | ||
|
||
void delete(CityEntity city); | ||
} |
59 changes: 59 additions & 0 deletions
59
spring-boot-jpa-querydsl/src/main/java/com/lance/querydsl/service/CityServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.lance.querydsl.service; | ||
|
||
import java.util.List; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.util.StringUtils; | ||
|
||
import com.lance.querydsl.entity.CityEntity; | ||
import com.lance.querydsl.entity.QCityEntity; | ||
import com.lance.querydsl.repository.CityRepository; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.jpa.impl.JPAQuery; | ||
|
||
@Service | ||
public class CityServiceImpl implements CityService { | ||
@PersistenceContext | ||
private EntityManager em; | ||
@Autowired | ||
private CityRepository cityRepository; | ||
|
||
/** | ||
* findAll | ||
* @return | ||
*/ | ||
public List<CityEntity> findAll(String hotelName) { | ||
QCityEntity cityEntity = QCityEntity.cityEntity; | ||
JPAQuery<CityEntity>query = new JPAQuery<>(em); | ||
BooleanExpression express = cityEntity.state.eq("1"); | ||
|
||
if(StringUtils.hasText(hotelName)) { | ||
express = express.and(cityEntity.hotels.any().name.likeIgnoreCase('%'+hotelName+'%')); | ||
} | ||
return query.select(cityEntity).from(cityEntity).where(express).fetch(); | ||
} | ||
|
||
/** | ||
* Save | ||
* @param city | ||
*/ | ||
@Transactional | ||
public void save(CityEntity city) { | ||
cityRepository.save(city); | ||
} | ||
|
||
@Transactional | ||
public void delete(long id) { | ||
cityRepository.delete(id); | ||
} | ||
|
||
@Transactional | ||
public void delete(CityEntity city) { | ||
cityRepository.delete(city); | ||
} | ||
} |