【java學習】spring mvc 公用dao的實現,定義基本的增刪改查

來源:互聯網
上載者:User

標籤:gpo   encrypt   ann   對象   post   入行   actor   obj   coding   

介面類:

  

package com.blog.db.dao;import com.blog.util.Pagination;import java.util.List;public interface PublicDao<T> {    public void setMapper(Class t);    public void setTableName(String tn);    public List<T> queryList(Pagination pagination);    public void deleteByID(int ID);    public void add(T t,Object[] values,int[] types);     public T queryById(int id);    public T queryByField(String fieldname,String value);    public void updateByID(T t,int ID,Object[] values,int[] types);}

 

實作類別:

package com.blog.db.dao.impl;import com.blog.db.dao.PublicDao;import com.blog.util.Pagination;import org.springframework.beans.factory.annotation.Value;import org.springframework.dao.EmptyResultDataAccessException;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.RowMapper;import org.springframework.stereotype.Repository;import javax.annotation.Resource;import java.sql.Types;import java.util.List;/** * 公用dao實現 * @deprecated  提供常見的列表、插入、查詢單個、更新資料服務 * @author ztf * */@Repository("publicDao")public class PublicDaoimpl<T> implements PublicDao<T> {    @Resource    private JdbcTemplate jdbcTemplate;    private String table_name = ""; //表名稱    private List<String> fields; //表欄位    @Value("${db_schema}")    private String schema; //表模式    private RowMapper<T> mapper;    /**     * 設定Mapper映射     */    public void setMapper(Class t){        this.mapper = new BeanPropertyRowMapper<T>(t);    }    public RowMapper<T> getMapper(){        return mapper;    }    public void setTableName(String tn){        this.table_name = tn;    }    public String getTable_name() {        return schema+"."+table_name;    }    public List<String> getFields(){        return this.fields;    }    public String getFieldsToString(){        String _fields;        List<String> fields = getFields();        _fields = String.join(",",fields);        return _fields;    }    /**     *  查詢列表     *  @param pagination 傳入公用util中定義的分頁對象     *  @return List<T>     * */    public List<T> queryList(Pagination pagination) {        List lists = null;        String sql = "select * from ? ?";        lists = jdbcTemplate.queryForList(                sql,                getTable_name(),                pagination.getSql(),                getMapper()        );        return lists;    }    /**     *  根據ID刪除某一個行     *  @param ID 傳入行id     * */    public void deleteByID(int ID) {        String sql = "delete from "+                getTable_name()+" where ID=?";        jdbcTemplate.update(                sql,                new Object[] { ID },                new int[] {Types.INTEGER}        );    }    /**     *  插入資料     *  @param t 傳入操作的資料對象     *  @param values 值的object[]對象     *  @param types 值對象的類型     * */    public void add(T t,Object[] values,int[] types) {        String sql = "insert into "+getTable_name()+"("+getFieldsToString()+") values(?)";        jdbcTemplate.update(                sql,                values,                types        );    }    /**     *  根據ID查詢指定的行     *  @param id 傳入id     * */    public T queryById(int id) {        T t = null;        String sql = "select * from "+getTable_name()+" where ID=?";        try{            t = jdbcTemplate.queryForObject(                    sql,                    new Object[] {id},                    new int[] {Types.INTEGER},                    getMapper()            );        }catch(EmptyResultDataAccessException e){            t = null;        }        return t;    }    /**     *  根據指定的fieldname查詢指定值的單行資料     *  @param fieldname 傳入id     * */    public T queryByField(String fieldname,String value) {        T t = null;        String sql = "select * from "+getTable_name()+" where "+fieldname+"=?";        try{            t = jdbcTemplate.queryForObject(                    sql,                    new Object[] {value},                    new int[] {Types.VARCHAR},                    getMapper()            );        }catch(EmptyResultDataAccessException e){            t = null;        }        return t;    }    /**     *  根據指定的id更新指定值的單行資料     *  @param t 傳入更新對象     *  @param ID 傳入指定的ID     *  @param values 值的object[]對象     *  @param types 值對象的類型 int[]     * */    public void updateByID(T t, int ID,Object[] values,int[] types) {        String sql = "update "+getTable_name()+" set username=?,password=?,pic=?,describe=?,check_time=? where ID="+ID;        jdbcTemplate.update(                sql,                values,                types        );    }}

核心的地方在:如何傳遞Mapper

這裡我使用的是

private RowMapper<T> mapper;    /**     * 設定Mapper映射     */    public void setMapper(Class t){        this.mapper = new BeanPropertyRowMapper<T>(t);    }    public RowMapper<T> getMapper(){        return mapper;    }

在調用Dao時傳入 T類的.class對象

調用例如(部分代碼):

 @Qualifier("publicDao")    private PublicDao<User> ud;    public User Sign(String username, String password) throws UnsupportedEncodingException {        String md5_pass = MD5Encryption.getEncryption(password);        ud.setTableName("users"); //指定查詢表名        ud.setMapper(User.class); //指定映射class對象        User user = ud.queryByField("username",username); //根據欄位 username查詢值為xxx 

可以實現基本表的增刪改查,提升開發效率,

如果有特殊的Dao可以自訂一個專屬Dao  再添加特殊方法

【java學習】spring mvc 公用dao的實現,定義基本的增刪改查

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.