@Override public void save(SysSensitiveWord sysSensitiveWord) { // 构造SQL的参数 Map<String, Object> paramMap = toMap(sysSensitiveWord); KeyHolder keyHolder = new GeneratedKeyHolder(); new NamedParameterJdbcTemplate(jdbcTemplate) .update(INSERT_SQL, new MapSqlParameterSource(paramMap), keyHolder); // 生成主键 sysSensitiveWord.setWordId(keyHolder.getKey().longValue()); }
/** * 将对象转换成Map * * @param sysSensitiveWord * @return */ public Map<String, Object> toMap(SysSensitiveWord sysSensitiveWord) { Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("word_id", sysSensitiveWord.getWordId()); paramMap.put("word", sysSensitiveWord.getWord()); paramMap.put("math_type", sysSensitiveWord.getMathType()); paramMap.put("create_date", sysSensitiveWord.getCreateDate()); paramMap.put("update_date", sysSensitiveWord.getUpdateDate()); paramMap.put("is_valid", sysSensitiveWord.getIsValid()); return paramMap; }
@Override public void modify(SysSensitiveWord sysSensitiveWord) { StringBuilder sql = new StringBuilder(); List<Object> param = new ArrayList<Object>(); sql.append(" UPDATE sys_sensitive_word SET "); if (sysSensitiveWord.getWordId() != null) { sql.append(" word_id = ?, "); param.add(sysSensitiveWord.getWordId()); } if (sysSensitiveWord.getWord() != null) { sql.append(" word = ?, "); param.add(sysSensitiveWord.getWord()); } if (sysSensitiveWord.getMathType() != null) { sql.append(" math_type = ?, "); param.add(sysSensitiveWord.getMathType()); } if (sysSensitiveWord.getCreateDate() != null) { sql.append(" create_date = ?, "); param.add(sysSensitiveWord.getCreateDate()); } if (sysSensitiveWord.getUpdateDate() != null) { sql.append(" update_date = ?, "); param.add(sysSensitiveWord.getUpdateDate()); } if (sysSensitiveWord.getIsValid() != null) { sql.append(" is_valid = ? "); param.add(sysSensitiveWord.getIsValid()); } sql.append(" WHERE word_id = ? "); param.add(sysSensitiveWord.getWordId()); jdbcTemplate.update(sql.toString(), param.toArray()); }
/** * 根据对象查询条数 * * @param sysSensitiveWord * @return Long */ @Override public Long count(SysSensitiveWord sysSensitiveWord) { StringBuilder sql = new StringBuilder(); sql.append(" SELECT count(*) "); sql.append(" FROM sys_sensitive_word _this "); sql.append(" WHERE 1 = 1 "); List<Object> param = new ArrayList<Object>(); if (sysSensitiveWord != null) { if (sysSensitiveWord.getWordId() != null) { sql.append(" AND _this.`word_id` = ? "); param.add(sysSensitiveWord.getWordId()); } if (sysSensitiveWord.getWord() != null && !"".equals(sysSensitiveWord.getWord())) { sql.append(" AND _this.`word` = ? "); param.add(sysSensitiveWord.getWord()); } if (sysSensitiveWord.getMathType() != null && !"".equals(sysSensitiveWord.getMathType())) { sql.append(" AND _this.`math_type` = ? "); param.add(sysSensitiveWord.getMathType()); } if (sysSensitiveWord.getCreateDate() != null) { sql.append(" AND _this.`create_date` = ? "); param.add(sysSensitiveWord.getCreateDate()); } if (sysSensitiveWord.getUpdateDate() != null) { sql.append(" AND _this.`update_date` = ? "); param.add(sysSensitiveWord.getUpdateDate()); } if (sysSensitiveWord.getIsValid() != null && !"".equals(sysSensitiveWord.getIsValid())) { sql.append(" AND _this.`is_valid` = ? "); param.add(sysSensitiveWord.getIsValid()); } } return jdbcTemplate.queryForObject(sql.toString(), Long.class, param.toArray()); }
/** * 根据对象查询 * * @param sysSensitiveWord * @param orders 排序字段,可多字段排序。格式{{"字段名", "asc"}, {"字段名, "desc"}} * @param offset 开始索引 * @param rows 条数(从第offset+1条开始,取rows条) * @return List */ @Override public List<SysSensitiveWord> find( SysSensitiveWord sysSensitiveWord, String[][] orders, Long offset, Long rows) { StringBuilder sql = new StringBuilder(); sql.append(SELECT_SQL); sql.append(" WHERE 1 = 1 "); List<Object> param = new ArrayList<Object>(); if (sysSensitiveWord != null) { if (sysSensitiveWord.getWordId() != null) { sql.append(" AND _this.`word_id` = ?"); param.add(sysSensitiveWord.getWordId()); } if (sysSensitiveWord.getWord() != null && !"".equals(sysSensitiveWord.getWord())) { sql.append(" AND _this.`word` = ?"); param.add(sysSensitiveWord.getWord()); } if (sysSensitiveWord.getMathType() != null && !"".equals(sysSensitiveWord.getMathType())) { sql.append(" AND _this.`math_type` = ?"); param.add(sysSensitiveWord.getMathType()); } if (sysSensitiveWord.getCreateDate() != null) { sql.append(" AND _this.`create_date` = ?"); param.add(sysSensitiveWord.getCreateDate()); } if (sysSensitiveWord.getUpdateDate() != null) { sql.append(" AND _this.`update_date` = ?"); param.add(sysSensitiveWord.getUpdateDate()); } if (sysSensitiveWord.getIsValid() != null && !"".equals(sysSensitiveWord.getIsValid())) { sql.append(" AND _this.`is_valid` = ?"); param.add(sysSensitiveWord.getIsValid()); } } // 排序 if (orders != null && orders.length > 0) { sql.append(" ORDER BY "); for (int i = 0; i < orders.length; i++) { String[] order = orders[i]; if (i != 0) { sql.append("_this.`").append(order[0]).append("`, "); } if (order.length == 1) { sql.append("_this.`").append(order[0]).append("` ASC "); } else { sql.append("_this.`").append(order[0]).append("` ").append(order[1]); } } } // 分页 if (offset != null && rows != null) { sql.append(" limit ?,? "); param.add(offset); param.add(rows); } else if (rows != null) { sql.append(" limit ? "); param.add(rows); } return jdbcTemplate.query( sql.toString(), param.toArray(), BeanPropertyRowMapper.newInstance(SysSensitiveWord.class)); }