spring-data-jpa

spring-data-jpa 相当于封装的dao层。

例如:CouponRepository
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import com.book511.domain.Coupon;

/**
* @author LJ
*
*/
@Transactional(readOnly = true)
public interface CouponRepository extends JpaRepository<Coupon, Long>,
JpaSpecificationExecutor<Coupon> {

/**
* 通过用户编号查找Coupon列表
*
* @author: LJ
* @Create at: 2011-11-1
* @param baseAccountId
* @return
*/
@Query("from Coupon c where c.baseAccount.id = ?1")
public List<Coupon> findByBaseAccountId(Long baseAccountId);

/**
* 通过用户名返回一个Coupon
*
* @author: LJ
* @Create at: 2011-11-1
* @param username
* @return
*/
@Query("from Coupon c where c.baseAccount.username=:username")
public Coupon getCoupon(@Param("username") String username);

}

原来的service层不改变,关心业务。
import org.springframework.transaction.annotation.Transactional;

import com.book511.domain.BaseAccount;

/**
* @author LJ
*
*/
@Transactional
public interface CouponService {


/**
* 判断用户是否已经存在图联币基本表
* @author:      LJ
* @Create at:   2011-10-31
* @param ba
* @return 如果为真,则表示已经存在;如果为否,则表示不存在。
*/
public boolean isExsitCoupon(BaseAccount ba);

/**
* 检测当前数据库中,根据用户表,查询用户是否已经有了图联币基本表。
* <p>
* 如果没有,则为其增加一个基本表,并初始化值为零。如果有,则跳过不增加。
*
* @author: LJ
* @Create at: 2011-10-31
*/
public void checkAddCoupon();

}

接口实现:
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.book511.domain.BaseAccount;
import com.book511.domain.Coupon;
import com.book511.repositories.CouponRepository;
import com.book511.service.coupon.CouponService;
import com.book511.service.um.BaseAccountService;

/**
* 图联币基本表业务层操作
*
* @author LJ
*
*/
@Service
@Transactional
public class CouponServiceImpl implements CouponService {

private static final Logger log = LoggerFactory
.getLogger(CouponServiceImpl.class);

private final static int size = 100;

@PersistenceContext
private EntityManager em;

@Inject
private CouponRepository couponRepository;


@Inject
private BaseAccountService baseAccountService;


@Override
public boolean isExsitCoupon(BaseAccount ba) {
log.debug("isExsitCoupon ");
// TODO Auto-generated method stub
Long baseAccountId = ba.getId();
List<Coupon> coupons = couponRepository.findByBaseAccountId(baseAccountId);
if (coupons.size() == 0) {
return false;
}
return true;
}

@Override
public void checkAddCoupon() {
// 查找出所有用户,并循环读取
for (int i = 0;; i++) {
List<BaseAccount> bas = baseAccountService.findPage(i, size);
if (null == bas || 0 == bas.size()) {
// 如果用户为空,则跳出循环,表示已经检出了所有用户
break;
}
for (BaseAccount ba : bas) {
// 判断是否已经拥有图联币基本表
boolean flag = isExsitCoupon(ba);
if (!flag) {
// 创建图联币基本信息
BigDecimal initValue = new BigDecimal(0.00);
Coupon coupon = new Coupon();
coupon.setBaseAccount(ba);
coupon.setAvailableAmount(initValue);
coupon.setFreezeAmount(initValue);
coupon.setTotalAmount(initValue);
coupon.setUsedCoupon(initValue);
coupon.setUuid(UUID.randomUUID().toString());
coupon.setCreated(new Date());
couponRepository.save(coupon);
}
}

}
}

}

 

你可能感兴趣的:(spring)