iT邦幫忙

2022 iThome 鐵人賽

DAY 17
0

Review

昨日我們講解了JdbcTemplate的是前DB與JAR檔準備,也介紹JdbcTemplate的Insert、BatchInsert、Update

今日我們將繼續透過JdbcTemplate完成後續的CREUD

Select

查詢單一數據

需注意查詢不到時會拋出異常,此時需要使try catch讓其Return null
創建Java Bean

public class Product {
    private String productId;
    private int inventory;
    //getter setter toString 略
}
@Test
public void testDay24(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean24.xml");
    System.out.println("容器啟動完成....");
    JdbcTemplate jdbcTemplate = ioc.getBean(JdbcTemplate.class);

    String sql = "SELECT * FROM PRODUCT WHERE PRODUCT_ID = ?";
    //RowMapper:資料略與Java Bean的映射 -> 實現類BeanPropertyRowMapper
    Product product = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Product.class),"P001");
    System.out.println(product);
}

Result
https://ithelp.ithome.com.tw/upload/images/20221009/20128084Tyw2GxOqTX.jpg
查詢不到值

//RowMapper:資料略與Java Bean的映射 -> 實現類BeanPropertyRowMapper
Product product = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Product.class),"P111");

Result
https://ithelp.ithome.com.tw/upload/images/20221009/201280844ly9fKpfAy.jpg
修改後程式

    JdbcTemplate jdbcTemplate = ioc.getBean(JdbcTemplate.class);

    String sql = "SELECT * FROM PRODUCT WHERE PRODUCT_ID = ?";
    Product product = null;
    //RowMapper:資料略與Java Bean的映射
    try{
        product = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Product.class),"P111");
    }catch (Exception e){

    }
    System.out.println(product);//null

查詢多條數據

@Test
public void testDay24(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean24.xml");
    System.out.println("容器啟動完成....");
    JdbcTemplate jdbcTemplate = ioc.getBean(JdbcTemplate.class);

    String sql = "SELECT PRODUCT_ID AS productId ,INVENTORY AS inventory FROM PRODUCT WHERE INVENTORY >?";
    List<Product> product = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Product.class),20);
    System.out.println(product);
}

Result
https://ithelp.ithome.com.tw/upload/images/20221009/201280841Vf2DS8WLH.jpg

查詢單一值

@Test
public void testDay24(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean24.xml");
    System.out.println("容器啟動完成....");
    JdbcTemplate jdbcTemplate = ioc.getBean(JdbcTemplate.class);

    String sql = "SELECT MAX(INVENTORY) FROM PRODUCT";
    Integer count = jdbcTemplate.queryForObject(sql,Integer.class);
    System.out.println(count);
}

Result
https://ithelp.ithome.com.tw/upload/images/20221009/20128084jW1c6RhNLz.jpg

NameParameter

加入NameParameter設定

<!-- 配置NamedParameterJdbcTemplate   -->
<bean id="NamedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>
@Test
public void testDay24(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean24.xml");
    System.out.println("容器啟動完成....");
    JdbcTemplate jdbcTemplate = ioc.getBean(JdbcTemplate.class);
    NamedParameterJdbcTemplate nameJdbcTemplate = ioc.getBean(NamedParameterJdbcTemplate.class);

    String sql = "INSERT INTO PRODUCT (PRODUCT_ID,INVENTORY) VALUES (:productId,:inventory)";
    Map<String, Object> map = new HashMap<>();
    map.put("productId","P100");
    map.put("inventory",0);
    int num = nameJdbcTemplate.update(sql,map);
    System.out.println(num);
}

Result
https://ithelp.ithome.com.tw/upload/images/20221009/20128084jJNqxQdHCV.jpg
https://ithelp.ithome.com.tw/upload/images/20221009/201280842jfpWE8dNf.jpg

SqlParameterSource傳參

可以透過Java Bean的屬性為Sql傳參數

@Test
public void testDay24(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean24.xml");
    NamedParameterJdbcTemplate nameJdbcTemplate = ioc.getBean(NamedParameterJdbcTemplate.class);
    
    String sql = "INSERT INTO PRODUCT (PRODUCT_ID,INVENTORY) VALUES (:productId,:inventory)";
    Product product = new Product();
    product.setProductId("P101");
    product.setInventory(101);
    int num = nameJdbcTemplate.update(sql,new BeanPropertySqlParameterSource(product));
    System.out.println(num);
}

Result
https://ithelp.ithome.com.tw/upload/images/20221010/20128084UNabUKWMm0.jpg
https://ithelp.ithome.com.tw/upload/images/20221010/20128084wtLPFmkoSE.jpg

DAO

<context:component-scan base-package="com.swj"></context:component-scan>
@Repository
public class ProductDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    public void saveProduct(Product product){
        String sql = "INSERT INTO PRODUCT (PRODUCT_ID,INVENTORY) VALUES (?,?)";
        jdbcTemplate.update(sql,product.getProductId(),product.getInventory());
    }
}
@Test
public void testDay24(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean24.xml");
    ProductDao dao = ioc.getBean(ProductDao.class);
    Product product = new Product();
    product.setProductId("P102");
    product.setInventory(102);
    dao.saveProduct(product);
}

Result
https://ithelp.ithome.com.tw/upload/images/20221010/201280844d8A07LHh4.jpg


上一篇
Day23 - JdbcTemplate (1)
下一篇
Day25 - Declarative transaction management (1)
系列文
這些年,我們早該學會的Spring Framework30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言