程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> 幾個簡單的基類

幾個簡單的基類

編輯:JAVA編程入門知識

 

BaseController

package com.yunzainfo.common.base;

import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yunzainfo.common.pojo.BasePage;
import com.yunzainfo.common.pojo.GridReturn;
import com.yunzainfo.common.pojo.MsgReturn;

public abstract class  BaseController<T,C extends BasePage> {

    
    protected abstract BaseService<T,C> getBaseService();
    
    @RequestMapping("/insert")
    @ResponseBody
    public MsgReturn insert(T t){
        if(this.getBaseService().insert(t)>0){
            return new MsgReturn(true, "新增成功!");
        }
        return new MsgReturn(false, "新增失敗!");
    }
    
    @RequestMapping("/delete")
    @ResponseBody
    public MsgReturn delete(T t){
        if(this.getBaseService().delete(t)>0){
            return new MsgReturn(true, "刪除成功!");
        }
        return new MsgReturn(false, "刪除失敗!");
    }
    
    @RequestMapping("/update")
    @ResponseBody
    public MsgReturn update(T t){
        if(this.getBaseService().update(t)>0){
            return new MsgReturn(true, "修改成功!");
        }
        return new MsgReturn(false, "修改失敗!");
    }
    
    @RequestMapping("/get")
    @ResponseBody
    public T get(T t){
        return (T) this.getBaseService().get(t);
    }

    /**
     * 分頁查詢
     * @param c 查詢參數類
     * @return
     */
    @RequestMapping("/findList")
    @ResponseBody
    public GridReturn findList(C c){
        if (c.getLimit() != null||c.getLength() != null) {
            c.setLength(c.getLimit());
            c.setLimit(c.getLimit());
            c.setOracleEnd(c.getLimit() + c.getStart());
            c.setOracleStart(c.getStart());
        }
        
        List<T> lsit = this.getBaseService().findList(c);
        int count = this.getBaseService().getTotalCount(c);
        
        return new GridReturn(c.getDraw(), count, count, lsit);
    }
}

 

BaseMapper

package com.yunzainfo.common.base;

import java.util.List;

public interface BaseMapper<T,C> {

    public int insert(T t);
    
    
    public int delete(T t);
    
    public int update(T t);
    
    public T get(T t);
    
    public List<T> findList(C c);
    public int getTotalCount(C c);
}

 

BaseService

package com.yunzainfo.common.base;

import java.util.List;

public interface BaseService<T,C> {

    public int insert(T t);
    
    
    public int delete(T t);
    
    public int update(T t);
    
    public T get(T t);
    
    public List<T> findList(C c);
    public int getTotalCount(C c);
}

 

BaseServiceImpl

package com.yunzainfo.common.base;

import java.util.List;


public abstract class BaseServiceImpl<T,C> {
    
    
    public abstract BaseMapper<T,C> getChildMapper();

    public int insert(T t){
        return this.getChildMapper().insert(t);
    }
    
    
    public int delete(T t){
        return this.getChildMapper().delete(t);
    }
    
    public int update(T t){
        return this.getChildMapper().update(t);
    }
    
    public T get(T t){
        return this.getChildMapper().get(t);
    }
    
    public List<T> findList(C c){
        return this.getChildMapper().findList(c);
    }
    
    public int getTotalCount(C c){
        return this.getChildMapper().getTotalCount(c);
    }
}

 

BasePage

package com.yunzainfo.common.pojo;

public class BasePage {

    protected int start;
    protected Integer limit;
    protected Integer length;
    protected int draw;
    
    protected Integer oracleStart;
    protected Integer oracleEnd;
    public int getStart() {
        return start;
    }
    public void setStart(int start) {
        this.start = start;
    }
    public Integer getLimit() {
        return limit;
    }
    public void setLimit(Integer limit) {
        this.limit = limit;
    }
    public Integer getLength() {
        return length;
    }
    public void setLength(Integer length) {
        this.length = length;
    }
    public int getDraw() {
        return draw;
    }
    public void setDraw(int draw) {
        this.draw = draw;
    }
    public Integer getOracleStart() {
        return oracleStart;
    }
    public void setOracleStart(Integer oracleStart) {
        this.oracleStart = oracleStart;
    }
    public Integer getOracleEnd() {
        return oracleEnd;
    }
    public void setOracleEnd(Integer oracleEnd) {
        this.oracleEnd = oracleEnd;
    }
    
    
}

 

GridReturn

package com.yunzainfo.common.pojo;
 
import java.util.List;

public class GridReturn {

    private int draw;//請求次數
    private long recordsTotal;//總記錄數
    private long recordsFiltered;//過濾後記錄數
    private List<?> data;
    
    public GridReturn(int draw,long recordsTotal,long recordsFiltered,List<?> data){
        this.data=data;
        this.draw=draw;
        this.recordsFiltered=recordsFiltered;
        this.recordsTotal=recordsTotal;
    }
    
    public int getDraw() {
        return draw;
    }
    public void setDraw(int draw) {
        this.draw = draw;
    }
    public long getRecordsTotal() {
        return recordsTotal;
    }
    public void setRecordsTotal(long recordsTotal) {
        this.recordsTotal = recordsTotal;
    }
    public long getRecordsFiltered() {
        return recordsFiltered;
    }
    public void setRecordsFiltered(long recordsFiltered) {
        this.recordsFiltered = recordsFiltered;
    }
    public List<?> getData() {
        return data;
    }
    public void setData(List<?> data) {
        this.data = data;
    }
    

}

 

MsgReturn

package com.yunzainfo.common.pojo;
 
public class MsgReturn {

    private boolean success; // 是否成功
    private Object msg; // 返回消息
    private Object otherObject;// 其他對象

    public MsgReturn() {

    }

    /**
     * 是否更新成功的構造方法
     * 
     * @param success
     *            是否成功
     * @param msg
     *            消息
     */
    public MsgReturn(boolean success, Object msg) {
        this.success = success;
        this.msg = msg;
        this.otherObject = "";
    }

    /**
     * 是否更新成功的構造方法
     * 
     * @param success
     *            是否成功
     * @param msg
     *            消息
     * @param otherObject
     *            其他對象
     */
    public MsgReturn(boolean success, Object msg, Object otherObject) {
        this.success = success;
        this.msg = msg;
        this.otherObject = otherObject;
    }

    /**
     * 異常時的構造函數
     * 
     * @param errormsg
     *            異常消息
     */
    public MsgReturn(Object errormsg) {
        this.success = false;
        this.msg = false;
        this.otherObject = "";
    }

    /**
     * 判斷是否成功
     * 
     * @return
     */
    public boolean isSuccess() {
        return success;
    }

    /**
     * 設置返回是否成功的狀態
     * 
     * @param success
     */
    public void setSuccess(boolean success) {
        this.success = success;
    }

    /**
     * 設置其他對象
     * 
     * @return
     */
    public Object getOtherObject() {
        return otherObject;
    }

    /**
     * 獲取其他對象
     * 
     * @param otherObject
     */
    public void setOtherObject(Object otherObject) {
        this.otherObject = otherObject;
    }

    /**
     * 獲取返回的消息
     * 
     * @return
     */
    public Object getMsg() {
        return msg;
    }

    /**
     * 設置返回的消息
     * 
     * @param msg
     */
    public void setMsg(Object msg) {
        this.msg = msg;
    }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved