package com.happy.dao.impl; import com.happy.Model.HotelDict; import com.happy.Until.UUIDUtil; import com.happy.dao.HotelDictDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.stereotype.Repository; import java.util.List; @Repository("HotelDictDao") public class HotelDictImplDao implements HotelDictDao { @Autowired private NamedParameterJdbcTemplate namedParameterJdbcTemplate; public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() { return namedParameterJdbcTemplate; } public void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate namedParameterJdbcTemplate) { this.namedParameterJdbcTemplate = namedParameterJdbcTemplate; } @Override public int insertHotelDict(HotelDict hotelDict) { String sql = "INSERT INTO hotel_dict (id, name, file_url, create_id, create_date, modify_date, status) VALUES (:id, :name, :file_url, :create_id, :create_date, :modify_date, :status);\n"; MapSqlParameterSource sps = new MapSqlParameterSource(); sps.addValue("name",hotelDict.getName()); sps.addValue("file_url",hotelDict.getFileUrl()); sps.addValue("create_id",hotelDict.getCreateId()); sps.addValue("create_date",hotelDict.getCreateDate()); sps.addValue("modify_date",hotelDict.getModifyDate()); sps.addValue("status",hotelDict.getStatus()); if(hotelDict.getId()==null){ sps.addValue("id", UUIDUtil.generateID()); } int num = 0; try{ num = namedParameterJdbcTemplate.update(sql, sps); } catch(Exception e){ e.printStackTrace(); } return num; } @Override public int updateHotelDict(HotelDict hotelDict) { String sql = "UPDATE hotel_dict SET name=:name, file_url=:file_url, create_id=:create_id, modify_date=:modify_date, status=:status WHERE id = :id"; MapSqlParameterSource sps = new MapSqlParameterSource(); sps.addValue("name",hotelDict.getName()); sps.addValue("file_url",hotelDict.getFileUrl()); sps.addValue("create_id",hotelDict.getCreateId()); sps.addValue("modify_date",hotelDict.getModifyDate()); sps.addValue("status",hotelDict.getStatus()); sps.addValue("id", hotelDict.getId()); int num = 0; try{ num = namedParameterJdbcTemplate.update(sql, sps); } catch(Exception e){ e.printStackTrace(); } return num; } @Override public int delHotelDict(int id) { String sql = "DELETE FROM `hotel_dict` WHERE id = :id "; MapSqlParameterSource sps = new MapSqlParameterSource(); sps.addValue("id",id); int num = 0; try{ num = namedParameterJdbcTemplate.update(sql, sps); }catch (Exception e){ e.printStackTrace(); } return num; } @Override public HotelDict getById(int id) { String sql = "SELECT * FROM `hotel_dict` WHERE id = :id "; MapSqlParameterSource sps = new MapSqlParameterSource(); sps.addValue("id",id); List list = null; try{ list = namedParameterJdbcTemplate.query(sql, sps, new BeanPropertyRowMapper<>(HotelDict.class)); }catch (Exception e){ e.printStackTrace(); } if(list != null && list.size()>0) return list.get(0); return null; } @Override public List queryPage(String sqlx, int page, int rows) { int start = (page - 1) * rows;// 每页的起始下标 String sql = "SELECT * FROM `hotel_dict` WHERE 1=1 "+sqlx+" ORDER BY id DESC limit :start,:rows "; MapSqlParameterSource sps = new MapSqlParameterSource(); sps.addValue("start", start); sps.addValue("rows", rows); List list = namedParameterJdbcTemplate.query(sql, sps, new BeanPropertyRowMapper<>(HotelDict.class)); if (list != null && list.size() > 0) return list; return null; } @Override public int queryTotal(String sqlx) { String sql = "SELECT count(*) FROM`hotel_dict` where 1=1 "+sqlx; MapSqlParameterSource sps = new MapSqlParameterSource(); return namedParameterJdbcTemplate.queryForInt(sql, sps); } @Override public List queryList(String sqlx) { String sql = "SELECT * FROM `hotel_dict` WHERE 1=1 "+sqlx; List list = null; try{ list = namedParameterJdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HotelDict.class)); }catch (Exception e){ e.printStackTrace(); } if(list != null && list.size()>0) return list; return null; } }