網(wǎng)站首頁 編程語言 正文
springjdbcTemplatetest訪問mysql數(shù)據(jù)庫,批量插入數(shù)據(jù)-Java文檔類資源-CSDN下載
這是傳統(tǒng)的javaweb方式訪問數(shù)據(jù)庫:
1.獲取 驅(qū)動
2.獲取連接 ----配置鏈接池,并從鏈接池獲取連接,就是鏈接池技術
3.創(chuàng)建執(zhí)行sql語句的處理器 ----
Statement stat = conn.createStatement(); 或者 preparedstatement
或者dbutil輔助類,接收不同類型的數(shù)據(jù)庫查詢結(jié)果
4.執(zhí)行sql
5.處理結(jié)果
6.回收資源
jdbctample
spring中提供了 jdbctample輔助查詢,不用再管連接池 回收資源 之類的
1.需要在xml中聲明 jdbctample,并提供數(shù)據(jù)源
2.調(diào)用時,@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()//查詢行數(shù)
{
int a=jdbcTemplate.queryForObject("select count(*) from student",Integer.class);//第二個參數(shù)表示返回類型的class,因為是int類型,所以就是intenger
return a;
}
public Student select_one_info_dao(int id)//查詢一行數(shù)據(jù)的詳細信息
{ //注意student類中要有無參的構造函數(shù)
String sql="select * from student where id=?";
//第一個參數(shù)是sql語句,第二個是返回值類型(固定寫法),第三個是sql參數(shù)的值
Student student=jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper(Student.class),id);
return student;
}
public List select_all_list_dao()
{//和查詢一個相比,使用的是query方法,參數(shù)沒變
String sql="select * from student where id<=?";
List students=jdbcTemplate.query(sql,new BeanPropertyRowMapper(Student.class),99);
return students;
}
//批量插入,一下插入好幾行數(shù)據(jù)
public void add_batch_dao(List
原文鏈接:https://blog.csdn.net/sharesb/article/details/123967037
相關推薦
- 2022-05-06 docker?save與docker?export的區(qū)別_docker
- 2022-05-26 Flutter?UI實現(xiàn)側(cè)拉抽屜菜單_Android
- 2022-08-20 python深入講解魔術方法_python
- 2023-01-15 GoLang內(nèi)存泄漏原因排查詳解_Golang
- 2022-04-12 C#實現(xiàn)六大設計原則之迪米特法則_C#教程
- 2022-06-23 Python+Turtle制作獨特的表白圖_python
- 2022-11-25 Python中日期和時間的用法超強總結(jié)_python
- 2023-08-30 linux服務器使用rsync 和 inotify或者sersync 實現(xiàn)服務器之間文件實時同步
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支