網站首頁 編程語言 正文
springjdbcTemplatetest訪問mysql數據庫,批量插入數據-Java文檔類資源-CSDN下載
這是傳統的javaweb方式訪問數據庫:
1.獲取 驅動
2.獲取連接 ----配置鏈接池,并從鏈接池獲取連接,就是鏈接池技術
3.創建執行sql語句的處理器 ----
Statement stat = conn.createStatement(); 或者 preparedstatement
或者dbutil輔助類,接收不同類型的數據庫查詢結果
4.執行sql
5.處理結果
6.回收資源
jdbctample
spring中提供了 jdbctample輔助查詢,不用再管連接池 回收資源 之類的
1.需要在xml中聲明 jdbctample,并提供數據源
2.調用時,@autowired聲明 jdbctample
package com.hxut.rj92.zyk.dao;
import com.hxut.rj92.zyk.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.List;
@Component
public class UserdaoImpl implements Userdao{
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public int add_dao(Student student) {
int a= jdbcTemplate.update(" insert student values(?,?,?);", new Object[]{student.getName(), (Integer)student.getId(), student.getLoc()});
return a;
}
@Override
public int change_dao(Student student) {
int a= jdbcTemplate.update(" update student set name=?,loc=? where id=?;", new Object[]{student.getName(),student.getLoc(),(Integer)student.getId() });
return a;
}
public int del_dao(int id)
{
int a =jdbcTemplate.update("delete from student where id =?",new Object [] {id});
return a;
}
public int select_counts_dao()//查詢行數
{
int a=jdbcTemplate.queryForObject("select count(*) from student",Integer.class);//第二個參數表示返回類型的class,因為是int類型,所以就是intenger
return a;
}
public Student select_one_info_dao(int id)//查詢一行數據的詳細信息
{ //注意student類中要有無參的構造函數
String sql="select * from student where id=?";
//第一個參數是sql語句,第二個是返回值類型(固定寫法),第三個是sql參數的值
Student student=jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper(Student.class),id);
return student;
}
public List select_all_list_dao()
{//和查詢一個相比,使用的是query方法,參數沒變
String sql="select * from student where id<=?";
List students=jdbcTemplate.query(sql,new BeanPropertyRowMapper(Student.class),99);
return students;
}
//批量插入,一下插入好幾行數據
public void add_batch_dao(List
原文鏈接:https://blog.csdn.net/sharesb/article/details/123967037
相關推薦
- 2022-02-23 在項目中全局自動加載默認圖的技巧
- 2022-08-14 深入了解Android?IO的底層原理_Android
- 2022-06-19 基于Python實現音樂播放器的實現示例代碼_python
- 2023-05-05 Linux中grep命令詳解_linux shell
- 2022-11-03 Python列表推導式,元組推導式,字典推導式,集合推導式_python
- 2022-11-17 python中?OpenCV和Pillow處理圖像操作及時間對比_python
- 2023-09-18 Echarts常見問題總結(持續更新)
- 2022-06-23 Android?Socket實現多個客戶端聊天布局_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支