JdbcTemplate增刪改查總結

2021-08-15 23:15:53 字數 1625 閱讀 6795

查詢資料

查詢單條資料,可以使用queryforint或者queryforobject,使用queryforobject時,第二個引數代表返回的值的資料型別

queryforobject(string sql,class

requiredtype): t - jdbctemplate

查詢單行資料,使用queryformap來進行查詢

queryformap(string sql):map - jdbctemplate

queryformap(string sql,object...args):map - jdbctemplate

查詢多行資料,而且這些多條資料時是不完全的表的列

queryforlist(string sql) 

如果有條件的話,需要將條件拼湊到sql語句中

queryforlist(string sql,object...args):

如果有條件的話,將條件以陣列的形式填寫到第二個引數中

,返回的是list

<

map<

string,object>>型別,例如

string sql =

"select id,name,age from student where age > ? and school = ?"

list

<

map<

string,object>>

list

= queryforlist(sql,new object[

]);

增加資料

1.插入資料使用udpate方法,返回的是新增幾行資料,使用方法都是一樣的

udpate(string sql):int - jdbctemplate

update(string sql,object...args): int - jdbctemplate

更新資料

1.更新資料使用udpate方法,返回的更新的行數,使用方法都是一樣的

udpate(string sql):int - jdbctemplate

update(string sql,object...args): int - jdbctemplate

eg:string sql = "update lsj_test set name = ? where age = ?";

int index = jdbctemplate.update(sql,new object);

刪除資料

1.刪除資料使用udpate方法,返回的刪除的行數,使用方法都是一樣的

udpate(string sql):int - jdbctemplate

update(string sql,object...args): int - jdbctemplate

eg:string sql = "delete lsj_test where name = ?";

int index = jdbctemplate.update(sql,new object);

JdbcTemplate 執行 DML 增刪改操作

jdbctemplate依賴於資料庫連線池,如果沒有相應的知識儲備,請點這些文字先行學習druid資料庫連線池 建立乙個資料庫,再建立一張表,新增幾條記錄 create database db1 use db1 create table account id int primary keyauto ...

mysql add attr mysql增刪改處理

首頁 代號姓名 性別民族 生日 db new mysqli localhost root mydb mysqli connect error or die 連線失敗!sql select from info result db query sql if result attr result fetc...

mysql增刪改查效果 mysql增刪改查

檢視所有資料庫 mysql show databases 建立乙個庫ghd並指定字符集為utp8 mysql create database ghd charset utf8 檢視mysql支援的字符集 mysql show char set 建立乙個表,並設定id為主鍵 create table ...