DaoImpl增删改查实例

/**
 * 
 */
package com.iflytek.edu.tlsys.answersheet.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.iflytek.edu.elp.common.dao.base.AbstractBaseOperationsDao;
import com.iflytek.edu.elp.common.util.BeanToMap;
import com.iflytek.edu.tlsys.answersheet.dao.AnswersheetConfigDao;
import com.iflytek.edu.tlsys.answersheet.domain.AnswersheetConfig;
import com.iflytek.edu.tlsys.answersheet.dto.AnswersheetConfigDTO;

/**
 * 
 * @author yswang4
 *
 */
@Repository("answersheetConfigDao")
public class AnswersheetConfigDaoImpl extends AbstractBaseOperationsDao implements AnswersheetConfigDao {

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(getClass());
	
	@Override
	public void insert(AnswersheetConfig answersheetConfig) {
		super.add(BeanToMap.convertToMap(answersheetConfig));
	}
	
	@Override
	public void delete(String uuid){
		deleteById(uuid);
	}

	
	@Override
	public AnswersheetConfig find(String uuid) {
		Assert.notNull(uuid, "uuid is required");
		try{
			StringBuilder sql = new StringBuilder(
					"select * from answersheet_config where  uuid = ?  ");
			getLog().info("sql:" + sql.toString());
			return (AnswersheetConfig)jdbcTemplate.queryForObject(sql.toString(),
					new AnswersheetConfigMapper(), new Object[] { uuid });
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}
	
	/**
	 * 
	 * @author yswang4
	 *
	 */
	private class AnswersheetConfigMapper implements RowMapper<AnswersheetConfig> {

		@Override
		public AnswersheetConfig mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			AnswersheetConfig answersheetConfig = new AnswersheetConfig();
			answersheetConfig.setUuid(rs.getString("uuid"));
			answersheetConfig.setCreateDatetime(rs.getTimestamp("createDatetime"));
			answersheetConfig.setLocationJson(rs.getString("locationJson"));
			answersheetConfig.setCreateUserId(rs.getString("createUserId"));
			answersheetConfig.setTopicSetName(rs.getString("topicSetName"));
			answersheetConfig.setTopicSetUid(rs.getString("topicSetUid"));
			answersheetConfig.setUpdateDatetime(rs.getDate("updateDatetime"));
			answersheetConfig.setUpdateUserId(rs.getString("updateUserId"));
			answersheetConfig.setSubjectName(rs.getString("subjectName"));
			answersheetConfig.setGradeName(rs.getString("gradeName"));
			answersheetConfig.setScanMode(rs.getString("scanMode"));
			answersheetConfig.setStuCount(rs.getInt("stuCount"));
			answersheetConfig.setFrontJson(rs.getString("frontJson"));
			answersheetConfig.setHtml(rs.getString("html"));
			return answersheetConfig;
		}

	}

	
	@Override
	public void update(AnswersheetConfig answersheetConfig) {
		Assert.notNull(answersheetConfig.getUuid(), "uuid is required! ");
		Assert.notNull(answersheetConfig.getLocationJson(), "locationJson is required! ");
		StringBuilder sql = new StringBuilder();
		sql.append("UPDATE " + getTableName() + " SET locationJson = ? ,html = ?, frontJson = ?, topicSetName = ? ");
		sql.append(" , topicSetUid = ?, updateDatetime = ? , updateUserId = ? WHERE uuid = ?");
		int i = jdbcTemplate.update(sql.toString(),
                new Object[]{answersheetConfig.getLocationJson(), 
			answersheetConfig.getHtml(),
			answersheetConfig.getFrontJson(),
			answersheetConfig.getTopicSetName(),
			answersheetConfig.getTopicSetUid(),
			answersheetConfig.getUpdateDatetime(), 
			answersheetConfig.getUpdateUserId(),
			answersheetConfig.getUuid()});
	}
	
	@Override
	public List<AnswersheetConfig> findByUserId(String userId) {
		Assert.notNull(userId, "userId is required");
		String sql = "select * from answersheet_config where createUserId = ? order by createDateTime desc ";	
		log.info("sql:" + sql);
		return super.jdbcTemplate.query(sql, new Object[]{userId}, new AnswersheetConfigMapper());
	}
	
	@Override 
	public List<AnswersheetConfigDTO> findByUserId(String userId, Integer curIndex, Integer pageSize) {
		Assert.notNull(userId, "userId is required");
		String sql = "select uuid, topicSetName, createDatetime from answersheet_config where createUserId = ? order by createDateTime desc limit ?, ? ";	
		log.info("sql:" + sql);
		return super.jdbcTemplate.query(sql, new Object[]{ userId, (curIndex-1)*pageSize, pageSize}, new AnswersheetConfigDTOMapper());
	}
	
	@Override 
	public Integer findCountByUserId(String userId) {
		Assert.notNull(userId, "userId is required");
		String sql = "select count(*) from answersheet_config where createUserId = ? ";	
		log.info("sql:" + sql);
		return super.jdbcTemplate.queryForObject(sql, new Object[]{userId}, Integer.class);
	}
	
	@Override
	public AnswersheetConfig findByTopicSetUid(String topicSetUId) {
		Assert.notNull(topicSetUId, "topicSetUId is required");
		String sql = "select uuid,updateDatetime from answersheet_config where topicSetUid = ?";
		List<AnswersheetConfig> result = jdbcTemplate.query(sql,
				new Object[] { topicSetUId },
				new RowMapper<AnswersheetConfig>() {

					@Override
					public AnswersheetConfig mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						AnswersheetConfig answersheetConfig = new AnswersheetConfig();
						answersheetConfig.setUuid(rs.getString("uuid"));
						answersheetConfig.setUpdateDatetime(rs
								.getTimestamp("updateDatetime"));
						return answersheetConfig;
					}
				});
		if (result == null || result.isEmpty()) {
			return null;
		}
		return result.iterator().next();
	}
	
	@Override
	public void deleteByTopicSetUid(String topicSetUid){
		Assert.notNull(topicSetUid, "topicSetUId is required");
		String sql = "delete from answersheet_config where topicSetUid = ?";
		jdbcTemplate.update(sql, new Object[]{topicSetUid});
	}
	
	@Override
	protected String getTableName() {
		return "answersheet_config";
	}

	@Override
	protected String getTableKey() {
		return "uuid";
	}

	@Override
	protected Logger getLog() {
		return this.log;
	}
	
	/**
	 * 
	 * @author yswang4
	 *
	 */
	private class AnswersheetConfigDTOMapper implements RowMapper<AnswersheetConfigDTO> {

		@Override
		public AnswersheetConfigDTO mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			AnswersheetConfigDTO answersheetConfig = new AnswersheetConfigDTO();
			answersheetConfig.setUuid(rs.getString("uuid"));
			answersheetConfig.setCreateDatetime(rs.getTimestamp("createDatetime"));
			answersheetConfig.setTopicSetName(rs.getString("topicSetName"));
			return answersheetConfig;
		}

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值