DictionaryDao.java 3.65 KB
package vion.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import vion.model.Account;
import vion.model.Dictionary;
import vion.model.DictionaryType;

import java.util.List;

@Repository
public class DictionaryDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    private static final String SELECT_DICTIONARY_BY_UNID = "SELECT * FROM tbl_dictionary_info WHERE id=?";

    public Dictionary getDictionaryById(Integer id) {
        List<Dictionary> dictionaries= jdbcTemplate.query(SELECT_DICTIONARY_BY_UNID, new BeanPropertyRowMapper<Dictionary>(Dictionary.class), new Object[]{id});
        if(dictionaries.size()>0){
            return dictionaries.get(0);
        }else{
            return null;
        }
    }

    private static final String SELECT_DICTIONARY_ALL= "SELECT * FROM tbl_dictionary_info";

    public List<Dictionary> getDictionaryAll() {
        return jdbcTemplate.query(SELECT_DICTIONARY_ALL, new BeanPropertyRowMapper<Dictionary>(Dictionary.class));
    }

    private static final String SAVE_DICTIONARY= "INSERT INTO tbl_dictionary_info(\n" +
            "            type, key, value, remark)\n" +
            "    VALUES (?, ?, ?, ?);";

    public Integer addDictionary(Dictionary dictionary) {
        return jdbcTemplate.update(SAVE_DICTIONARY,
                new Object[]{
                        dictionary.getType(),dictionary.getKey(),dictionary.getValue(),dictionary.getRemark()});
    }
    private static final String UPDATE_DICTIONARY= "UPDATE tbl_dictionary_info\n" +
            "   SET type=?, key=?, value=?, remark=?\n" +
            " WHERE id=?;";

    public Integer updateDictionary(Dictionary dictionary) {
        return jdbcTemplate.update(UPDATE_DICTIONARY,
                new Object[]{
                        dictionary.getType(),dictionary.getKey(),dictionary.getValue(),dictionary.getRemark(),dictionary.getId()});
    }

    private static final String SELECT_TYPE_BY_UNID = "SELECT * FROM tbl_dictionary_type WHERE id=?";

    public DictionaryType getDictionaryTypeById(Integer id) {
        List<DictionaryType> dictionaries= jdbcTemplate.query(SELECT_TYPE_BY_UNID, new BeanPropertyRowMapper<DictionaryType>(DictionaryType.class), new Object[]{id});
        if(dictionaries.size()>0){
            return dictionaries.get(0);
        }else{
            return null;
        }
    }

    private static final String SELECT_DICTIONARYTYPE_ALL= "SELECT * FROM tbl_dictionary_type";

    public List<DictionaryType> getDictionaryTypeAll() {
        return jdbcTemplate.query(SELECT_DICTIONARYTYPE_ALL, new BeanPropertyRowMapper<DictionaryType>(DictionaryType.class));
    }

    private static final String SAVE_DICTIONARYTYPE= "INSERT INTO tbl_dictionary_type(\n" +
            "            type, name, remark)\n" +
            "    VALUES (?, ?, ?);";

    public Integer addDictionaryType(DictionaryType dictionaryType) {
        return jdbcTemplate.update(SAVE_DICTIONARYTYPE,
                new Object[]{
                        dictionaryType.getType(),dictionaryType.getName(),dictionaryType.getRemark()});
    }
    private static final String UPDATE_DICTIONARYTYPE= "UPDATE tbl_dictionary_type\n" +
            "   SET type=?, name=?, remark=?\n" +
            " WHERE id=?;";

    public Integer updateDictionaryType(DictionaryType dictionaryType) {
        return jdbcTemplate.update(UPDATE_DICTIONARYTYPE,
                new Object[]{
                        dictionaryType.getType(),dictionaryType.getName(),dictionaryType.getRemark(),dictionaryType.getId()});
    }
}