springboot连接dao数据源的几种方式

1、extends  CrudRepository ,例如:

@Repository
public interface AmzDataStatisticRepository extends CrudRepository<AmzDataStatistic, Long> {

}

在Service注入AmzDataStatisticRepository 类即可调用
2、mybatis注解实现mapper

@Mapper        
public interface IAmzFileDao { 
    

    @Select(" select * from AMZ_File where DATEDIFF(day,t2.upload_time,GETDATE())=0 and type = #{processStatus}")
    List<AmzFile> findByRowDataByprocessStatus(@Param("processStatus") String processStatus);
}

在Service注入IAmzFileDao 类即可调用
3、直接注入JdbcTemplate

@Repository
public class JdbcTemplateDao implements IJdbcTemplateDao {
    private static final Log logger = LogFactory.getLog(JdbcTemplateDao.class);
    
    private static final SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 批量插入还原数据
     */
    @Override
    public void batchAddAmzRestore(List<AmzRestore> vos) {
        SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String sql="INSERT INTO AMZ_RESTORE ( file_id,"
                                            + "transaction_date,"
                                            + "currency,"
                                            + "crt_time,"
                                            + "upd_time ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        jdbcTemplate.batchUpdate(sql,new BatchPreparedStatementSetter(){

            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                AmzRestore vo = vos.get(i);
                
                ps.setString(16, f.format(vo.getCrtTime()));
                ps.setString(17, f.format(vo.getUpdTime()));
            }

            @Override
            public int getBatchSize() {
                return vos.size();
            }
            
        });
        
    }

}

在Service注入IJdbcTemplateDao 类即可调用

4、mybatis的mapper用xml来实现

猜你喜欢

转载自blog.csdn.net/douxingpeng1/article/details/82527705