JdbcTemplate资料整理

news/2024/7/9 22:57:46 标签: 数据库, mysql, postgresql, json
 JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
 用JdbcTemplate完成crud操作
     - update():执行DML语句,增删改语句
     - queryForMap():查询结果将结果集封装为map集合
     	-- 注意 : 这个方法查询的结果集长度只能是1
     - queryForList():查询结果将结果集封装为list集合
       -- 注意: 将每一条记录封装为一个Map集合,再将map集合装载到List集合中。
	  - query()查询结果,将结果封装为javaBean对象           
     - queryForObject(): 查询结果,将结果封装为对象
     
     
public class JavaDemo01 {
    public static void main(String[] args) {
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
      String sql = "update account set username = 'Abbe' where id = ?";
        int count = template.update(sql ,1);
        System.out.println(count);
    }
}
package com.itheima;

import org.springframework.jdbc.core.JdbcTemplate;
import util.JDBCUtils;

public class JavaDemo01 {
    public static void main(String[] args) {
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        String sql = "insert into account(id,username,password)values(?,?,?)";
        int count = template.update(sql,2,"Jerry","123");
        System.out.println(count);
    }
}

public class JavaDemo01 {
    public static void main(String[] args) {
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        String sql = "delete from  account where id = ?";
        int count = template.update(sql,1);
        System.out.println(count);
    }
}
public class JavaDemo01 {
    public static void main(String[] args) {
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        String sql = "select * from account where id = ?";
        Map<String, Object> map = template.queryForMap(sql, 2);
        for (String key : map.keySet()) {
            System.out.println(key + "--" +map.get(key));
        }
        
    }
public class JavaDemo01 {
    public static void main(String[] args) {
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        String sql = "select * from account where id = ?";
        List<Map<String, Object>> list = template.queryForList(sql, 2);
        for (Map<String, Object> map : list) {
            for (Map.Entry<String, Object> en : map.entrySet()) {
                System.out.println(en.getKey()  +" -" + en.getValue());
            }
        }

    }
}

public class JavaDemo01 {
    public static void main(String[] args) {
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        String sql = "select * from account where id = ?";
        Account account = template.queryForObject(sql, new BeanPropertyRowMapper<Account>(Account.class), 2);
        System.out.println(account);

    }
}
public class JavaDemo01 {
    public static void main(String[] args) {
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        String sql = "select * from account ";
        List<Account> list = template.query(sql, new BeanPropertyRowMapper<Account>(Account.class));
        for (Account account : list) {
            System.out.println(account);
        }


    }
}
public class JavaDemo01 {
    public static void main(String[] args) {
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        String sql = "select count(*) from account ";
        Long total = template.queryForObject(sql, Long.class);
        System.out.println(total);


    }
}

util 包下 JDBUtils.java src druid.properties

package com.itheima.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class JDBCController {
    @Autowired
    JdbcTemplate template;
    @RequestMapping("/findAll")
    public List<Map<String,Object>> findAll(){
        String sql = "select * from user";
        List<Map<String, Object>> list_maps = template.queryForList(sql);
        return list_maps;
    }
    
    
    
    @GetMapping("/addUser")
    public String addUser(){
        String sql = "insert into user(username,birthday,sex,address)values('Anne','1987/11/12','男','济南')";
        template.update(sql);
        return "ok!";

    }
    @GetMapping("/updateUser/{id}")
    public String updateUser(@PathVariable("id")Integer id){
        String sql = "update user set username = ? , address = ? where id =" +id;
        Object[] obj = new Object[2];
        obj[0] = "Jeac";
        obj[1] = "济南";
        template.update(sql,obj);
        return "update_ok";

    }
    @GetMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable("id")Integer id){
        String sql = "delete from user where id = "+id;
        template.update(sql);
        return "delete_ok";

    }
}


http://www.niftyadmin.cn/n/1248776.html

相关文章

MixTime 黑设会之原力觉醒

在大家的翘首盼望下&#xff0c;MixLab 终于在1月9日迎来了2021年第一次线下聚会&#xff0c;之后我们也将围绕 #人工智能交叉领域的最新进展/知识# 进行常态化分享。MixTime 黑设会HackerDesigner Community Offline Meetup虽然线下活动刚刚启动&#xff0c;工作蛙就已经收到了…

mybatis知识

mybatis知识 导入maven jar包 <dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.5</version></dependency><dependency><groupId>junit</group…

寻求生物设计的想象空间 | 无学科专栏

Neri Oxman被业界认为是 “全球最大胆的跨学科思想家”“我不认为时尚只是时尚&#xff0c;生物只是生物。就像我从不把建筑、设计、文化分割对待。”—— Neri Oxman在之前翻译的论文中&#xff0c;Neri为我们展示了近些年来 “成功” 的生物设计商业公司&#xff0c;也对其进…

java IO (二)

接上篇 1、IO异常的处理 众所周知&#xff0c;所有被打开的系统资源&#xff0c;比如流、文件或者Socket连接等&#xff0c;都需要被开发者手动关闭&#xff0c;否则随着程序的不断运行&#xff0c;资源泄露将会累积成重大的生产事故。 JDK7前处理 建议使用try...catch...f…

AR实时求解数独 |Mixlab混合现实

首先&#xff0c;什么是WebAssembly&#xff1f;我们需要知道它到底是什么&#xff01;WebAssembly是一种可以让C/C这些非JavaScript语言编写的代码在浏览器上运行&#xff0c;是一种在web上运行二进制文件的技术标准。就前端来说&#xff0c;它因为支持了更多的语言&#xff0…

java JUnit单元测试、反射、注解

1、Junit单元测试 Junit的概念 概述 : Junit是Java语言编写的第三方单元测试框架(工具类)作用 : 用来做“单元测试”——针对某个普通方法&#xff0c;可以像main()方法一样独立运行&#xff0c;它专门用于测试某个方法。 Junit的使用步骤 1.在模块下创建lib文件夹,把Junit的…

寻找AR中的Big Difference - v3.0 | AR指南

昨天2021.1.16周六上海的黑设会上&#xff0c;傅嵌华、荔枝君、Nakhqin为我们分享了AR、VR、XR相关的研究、行业动态、创新项目等。在2021新的一年&#xff0c;我有了新的理解&#xff0c;有种曲径通幽、别有洞天的感觉。于是抽空更新了这一指南&#xff0c;分享给大家。目前&a…

Java中fastjson库将Map、JSON、String相互转换

Java中fastjson库将Map、JSON、String相互转换json就是一串字符串 只不过元素会使用特定的符号标注。{} 双括号表示对象[] 中括号表示数组"" 双引号内是属性或值: 冒号表示后者是前者的值&#xff08;这个值可以是字符串、数字、也可以是另一个数组或对象&#xff09…