• springboot2.1.3使用jdbcTemplate


    这里只是备忘一下使用方式,至于配置数据源信息不在此文中讲解,忘谅解。

    1.  查询返回List<Long>数据集 (这里比如返回userId,long型)

    @Autowired
    @Qualifier("xxxxx") // 这里自己定义的数据源名称
    private JdbcTemplate jdbcTemplate;

    String sql = "SELECT t.user_id FROM users"; List<Long> idList = jdbcTemplate.queryForList(sql, Long.class);

    2. 查询返回单条记录并转对象(Object,比如 Users)

    String sql = "SELECT t.user_id, t.age, t.update_at FROM my_users t WHERE t.user_id = ?";
    // sql需要执行的语句
    // new Object[]{userId}. 传入参数
    // new RowMapper<T> 需要将返回的字段自行处理映射为实体对象
    Users u
    = jdbcTemplate.queryForObject(sql, new Object[]{userId}, new RowMapper<Users> { @Override public Users mapRow(ResultSet rs, int rowNum) throws SQLException { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Users u = new Users(); u.setUserId(Long.parseLong(rs.getString("user_id")));// 这里用rs.getString("列名") 方式取出 u.setAge(Integer.parseInt(rs.getString("age"))); u.setUpdateAt(sdf.parse(rs.getString("update_at"))); return u; } catch (Exception e) { return null; } } });

    未完,后续再添加。

  • 相关阅读:
    链家网各城市二手房价格
    mitmproxy 配置
    Python操作APP -- Appium-Python-Client
    Appium连接模拟器
    adb server version (xx) doesn't match this client (xx); killing...
    Appnium 环境搭建
    KeyError: 'xxx does not support field: _id'
    Python执行JS -- PyExecJS库
    Python -- CSV文件读写
    Git的基本使用 -- 分支管理
  • 原文地址:https://www.cnblogs.com/jimmyshan-study/p/11149420.html
Copyright © 2020-2023  润新知