返回规范(Result)

1. 先上例子:

public ResultData getcarForceInsInformation(String InsId){
    ResultData resultData=new ResultData();
    List date= insuranceRepository.getcarForceInsInformation(InsId);
   if(date!=null){
       resultData.setMessage("查询成功");
       resultData.setResult("true");
       resultData.setPageData(new PageData(date,date.size()));
   }
   else{
       resultData.setMessage("系统错误,查询失败");
       resultData.setResult("false");
   }

    return  resultData;
}
  2. ResultData 是   包含: 返回结果 result,返回数据 PageData , 返回信息 message

public class ResultData {

    // 错误状态码 比如 "00", "01", "02"
    private String result;
    // 错误中文描述 比如 "正常""日期格式不正确" "缺少permit参数"
    private String message;
    // 返回数据
    private PageData pageData;

    public ResultData() {
    }

    public ResultData(String result, String message, PageData pageData) {
        this.result = result;
        this.message = message;
        this.pageData = pageData;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public PageData getPageData() {
        return pageData;
    }

    public void setPageData(PageData pageData) {
        this.pageData = pageData;
    }
}
3. pageData: 是 包含分页的信息

public class PageData<T> {

    private int total;
    private List<T> data;

    public PageData(List<T> data, int total) {
        this.total = total;
        this.data = data;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }
}
4. 获得 pagedata 的底层方法: 在一个抽象类

package com.iris.live.services.data.repositories.impl;

import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.iris.live.services.models.PageData;
import org.hibernate.SQLQuery;
import org.hibernate.jpa.HibernateQuery;
import org.hibernate.transform.Transformers;
import org.hibernate.type.Type;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * Created by sxy on 2016/7/28.
 */
public abstract class BaseRepository {

    @PersistenceContext
    protected EntityManager entities;

    protected <T> T getScalar(String sql, Class<T> clazz) {
        return this.getScalar(sql, Maps.newHashMap(), clazz);
    }

    protected <T> T getScalar(String sql, Map params, Class<T> clazz) {
        List list = this.getList(sql, params);
        if (list.size() == 0) {
            return null;
        }

        Map first = (Map) Iterables.get(list, 0);
        if (first.entrySet().size() == 0) {
            return null;
        }

        Map.Entry entry = (Map.Entry) Iterables.get(first.entrySet(), 0);
        return (T) entry.getValue();
    }

    protected List getList(String sqlStr) {
        return this.getList(sqlStr, Maps.newHashMap());
    }

    protected List getList(String sqlStr, Map params) {
        Query query = this.entities.createNativeQuery(sqlStr);
        if (params != null) {
            for (Object key : params.keySet()) {
                query.setParameter((String) key, params.get(key));
            }
        }

        return this.toMap(query);
    }

    protected int getCount(String sqlStr) {
        return this.getCount(sqlStr, Maps.newHashMap());
    }

    protected int getCount(String sqlStr, Map params) {
        Query query = this.entities.createNativeQuery(sqlStr);
        if (params != null) {
            for (Object key : params.keySet()) {
                query.setParameter((String) key, params.get(key));
            }
        }

        BigInteger result = (BigInteger) query.getSingleResult();
        return result.intValueExact();
    }

    protected PageData getPage(String sqlStr, int pageSize, int pageIndex) {
        return this.getPage(sqlStr, Maps.newHashMap(), pageSize, pageIndex);
    }

    protected PageData getPage(String sqlStr, Map params, int pageSize, int pageIndex) {
        List data = null;
        if (pageSize == 0 && pageIndex == 0){
            data = this.getList(sqlStr,params);
        }else{
            data = this.getList(String.format("%s limit %d, %d", sqlStr, pageIndex * pageSize, pageSize), params);
        }
        int total = this.getCount(String.format("select cast(count(1) as UNSIGNED) rows from (%s) as q", sqlStr), params);
        return new PageData(data, total);
    }

    private List toMap(Query query) {
        if (query instanceof HibernateQuery) {
            query.unwrap(HibernateQuery.class)
                    .getHibernateQuery()
                    .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
            return query.getResultList();
        }

        throw new IllegalStateException();
    }

    private void addScalar(Query query, String column, Type type) {
        if (query instanceof HibernateQuery) {
            query.unwrap(SQLQuery.class).addScalar(column, type);
        }

        throw new IllegalStateException();
    }
}

6.每个 model对应的 

       Amodel----Aservice---AserviceImpl-------  AmodelRespotory------AmodelRespotoryImpl-----baserespotory


service层: 是接口 里面 有 静态的类(要分页 需要继承 requestBase) +方法
public interface AfterMarketService {

serviceImpl层: 实现接口,注意有 上标+事务提交+主动注入
@Service
@Transactional//(readOnly = true)
public class AfterMarketServiceImpl implements AfterMarketService {

    @Autowired
    private AfterMarketRepository repository;

dao层: 接口正规的
@NoRepositoryBean
public interface AccountRepository {
  jpa版本的 可以实现简单的 crud
public interface AttachmentPolicyRepository extends JpaRepository<AttachmentPolicyModel, Integer>, JpaSpecificationExecutor<AttachmentPolicyModel> {

daoimpl层: 实现正规的 接口 和继承 底层的 baserespotory
@Repository
@Transactional//(readOnly = true)
public class AccountRepositoryImpl extends BaseRepository implements AccountRepository



public abstract class BaseRepository {


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值