java BatchSqlUpdate,Insert Entity,deleteAll,select,selectAll和interface DAO

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java BatchSqlUpdate,Insert Entity,deleteAll,select,selectAll和interface DAO相关的知识,希望对你有一定的参考价值。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.Timestamp;

@Repository("tripDao")
public class TripRepository implements TripDAO {

    private final JdbcTemplate jdbcTemplate;

    private final String QUERY_SAVE = "INSERT into drier.t_trip(trip_id, stop_id, stop_sequence, arrival_time, departure_time, created, updated, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?);";

    @Autowired
    public TripRepository(
            JdbcTemplate jdbcTemplate
    ) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void saveAll(List<Trip> entity, int batchSize) {
        jdbcTemplate.batchUpdate(QUERY_SAVE,
                new BatchPreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement ps, int i) throws SQLException {
                        ps.setString(1, entity.get(i).getTripId());
                        ps.setLong(2, entity.get(i).getStopId());
                        ps.setInt(3, entity.get(i).getStopSequence());
                        ps.setTimestamp(4, new Timestamp(entity.get(i).getArrivalTime().getTime()));
                        ps.setTimestamp(5, new Timestamp(entity.get(i).getDepartureTime().getTime()));
                        ps.setTimestamp(6, new Timestamp(entity.get(i).getCreated().getTime()));
                        ps.setTimestamp(7, new Timestamp(entity.get(i).getUpdated().getTime()));
                        ps.setBoolean(8, entity.get(i).getStatus());
                    }

                    @Override
                    public int getBatchSize() {
                        return batchSize;
                    }
                });
    }
}
@Override
public void save(Trip entity) {
    jdbcTemplate.update(QUERY_SAVE,
      entity.getTripId(),
      entity.getStopId(),
      entity.getStopSequence(),
      new Timestamp(entity.getArrivalTime().getTime()),
      new Timestamp(entity.getDepartureTime().getTime()),
      new Timestamp(entity.getCreated().getTime()),
      new Timestamp(entity.getUpdated().getTime()),
      entity.getStatus()
    );
}
@Override
public void deleteAll() {
  String sql = "DELETE FROM drier.t_trip WHERE TRUE=TRUE;";
  jdbcTemplate.execute(sql);
}
public interface EntityDAO<E,K>  {
    void save(E entity);
    void saveAll(List<E> entity, int batchSize);
    List<E> findAll();
    E findById(K id);
    E update (E entity);
    void deleteById(K id);
    void delete(E entity);
    void deleteAll();
}
 @Override
    public List<RouteDrier> findAll() {
        String sql = "SELECT * FROM drier.t_route";
        return jdbcTemplate.query(sql, new RouteDrierMapper());
    }
    public Station findById(Long id) {
        String sql = "SELECT * FROM public.t_station WHERE id = ?;";
        try {
            return (Station) jdbcTemplate.queryForObject(sql,
                    new Object[]{id},
                    new StationMapper());
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
private class RouteDrierMapper implements RowMapper {

        @Override
        public RouteDrier mapRow(ResultSet rs, int rowNum) throws SQLException {
            RouteDrier routeDrier = new RouteDrier();
            routeDrier.setId(rs.getLong("id"));
            routeDrier.setRouteShortName(rs.getString("route_short_name"));
            routeDrier.setRouteLongName(rs.getString("route_long_name"));
            routeDrier.setRouteType(rs.getInt("route_type"));
            routeDrier.setCreated(rs.getTimestamp("created"));
            routeDrier.setUpdated(rs.getTimestamp("updated"));
            routeDrier.setStatus(rs.getBoolean("status"));
            return routeDrier;
        }
    }

以上是关于java BatchSqlUpdate,Insert Entity,deleteAll,select,selectAll和interface DAO的主要内容,如果未能解决你的问题,请参考以下文章

SQL SERVER 分页查询 和 Inser 添加并返回Id

Attribute "resultType" must be declared for element type "update" or "inser

ansible roles实践——安装java

Linux下安装Java

Java:插入查询异常

JAVA全栈第四天:Mybatis Mapper