Skip to content

Commit

Permalink
add city service
Browse files Browse the repository at this point in the history
  • Loading branch information
leelance committed Nov 27, 2016
1 parent 1b9260a commit 72cd57b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
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);
}
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);
}
}

0 comments on commit 72cd57b

Please sign in to comment.