package com.controller;

import com.transaction_.AEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("jdbc")
public class JdbcTemplateController {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @RequestMapping("jdbc1")
    @ResponseBody
    public String jdbc1() {
        return String.valueOf(jdbcTemplate.update("insert into `wb_tbl_a`(company_id,status) values (?,?)", 100, 100));
    }
    /**
     * jdbc查询所有
     */
    @RequestMapping("queryAll")
    @ResponseBody
    public List jdbc2() {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("111","zxy");
        List<AEntity> query = jdbcTemplate.query("select * from wb_tbl_a",
                new BeanPropertyRowMapper<AEntity>(AEntity.class));
        return query;
    }
    /**
     * 查询单个
     *
     */
    @RequestMapping("queryOne")
    @ResponseBody
    public AEntity queryOne() {
       return jdbcTemplate.queryForObject("select * from wb_tbl_a where id =?",
                new BeanPropertyRowMapper<AEntity>(AEntity.class),
                "10"
        );
    }
    /**
     * 聚合查询 查询总数
     */
    @RequestMapping("queryCount")
    @ResponseBody
    public Long queryCount() {
        return jdbcTemplate.queryForObject("select count(*) from wb_tbl_a ",
                Long.class
        );
    }
}
