最基本操作
創(chuàng)建:create table customers(id int identity primary key not null, name varchar(15)not null); identity主鍵自增長
選擇:select * from table1 where 范圍
插入:insert into table1(field1,field2) values(value1,value2)
刪除:delete from table1 where 范圍
更新:update table1 set field1=value1 where 范圍
示例:
create table people (id int identity primary key ,姓名 char(50) not null,年齡 int not null,地址 char(50) not null)
select * from people
insert into people (姓名,年齡,地址) values('張三',15,'西安')
delete from people where id = 7
update people set 年齡 = 18 where id = 1
一、數據庫
1.創(chuàng)建數據庫
create database [if not exists] db_name [character set xxx] [collate xxx]
*創(chuàng)建一個名稱為mydb1的數據庫延窜。
create database mydb1;
*創(chuàng)建一個使用utf8字符集的mydb2數據庫湘捎。
create database mydb2 character set utf8;
*創(chuàng)建一個使用utf8字符集茂契,并帶校對規(guī)則的mydb3數據庫窝爪。
create database mydb3 character set utf8 collate utf8_bin ;
2.查看數據庫
show databases;查看所有數據庫
show create database db_name; 查看數據庫的創(chuàng)建方式
3.修改數據庫
alter database db_name [character set xxx] [collate xxxx]
4.刪除數據庫
drop database [if exists] db_name;
5.使用數據庫
切換數據庫 use db_name;
查看當前使用的數據庫 select database();
二暖哨、表
1.創(chuàng)建表
create table tab_name(
field1 type,
field2 type,
...
fieldn type
)[character set xxx][collate xxx];
****java和mysql的數據類型比較
String? ----------------------? char(n) varchar(n) 255? 65535
byte short int long float double -------------- tinyint? smallint int bigint float double
boolean ------------------ bit 0/1
Date ------------------ Date雌澄、Time嫁乘、DateTime、timestamp
FileInputStream FileReader? ------------------------------ Blob Text
*創(chuàng)建一個員工表employee
create table employee(
id int primary key auto_increment ,
name varchar(20),
gender bit default 1,
birthday date,
entry_date date,
job varchar(20),
salary double,
resume text
);
約束:
primary key
unique
not null
auto_increment 主鍵字段必須是數字類型扭吁。
2.查看表信息
desc tab_name 查看表結構
show tables 查看當前數據庫中的所有的表
show create table tab_name 查看當前數據庫表建表語句
3.修改表結構
(1)增加一列
alter table tab_name add [column] 列名 類型;
(2)修改一列類型
alter table tab_name modify 列名 類型;
(3)修改列名
alter table tab_name change [column] 列名 新列名 類型;
(4)刪除一列
alter table tab_name drop [column] 列名;
(5)修改表名
rename table 表名 to 新表名;
(6)修該表所用的字符集
alter table student character set utf8;
4.刪除表
drop table tab_name;
三撞蜂、表操作
1.insert?into(增)
insert into tab_name (field1,filed2,.......) values (value1,value2,.......);
insert into tab_name values(value1,value2,......);
*插入的數據應與字段的數據類型相同。
*數據的大小應在列的規(guī)定范圍內侥袜,例如:不能將一個長度為80的字符串加入到長度為40的列中蝌诡。
*在values中列出的數據位置必須與被加入的列的排列位置相對應。
*字符和日期型數據應包含在單引號中'zhang' '2013-04-20'
*插入空值:不指定某列的值或insert into table value(null)枫吧,則該列取空值浦旱。
*如果要插入所有字段可以省寫列列表,直接按表中字段順序寫值列表insert into tab_name values(value1,value2,......);
*示例
insert into emp (name,birthday,entry_date,job,salary) values ('張飛','1990-09-09','2000-01-01','打手',999);
insert into emp values (7,'黃忠',1,'1970-05-05','2001-01-01','戰(zhàn)神',1000,null);
2.update(改)
update tab_name set field1=value1,field2=value2,......[where 語句]
*UPDATE語法可以用新值更新原有表行中的各列九杂。
*SET子句指示要修改哪些列和要給予哪些值颁湖。
*WHERE子句指定應更新哪些行。如沒有WHERE子句例隆,則更新所有的行甥捺。
*示例:
將所有員工薪水修改為5000元。
update emp set salary=5000;
將姓名為’zs’的員工薪水修改為3000元裳擎。
update emp set salary=3000 where name='zs';
將姓名為’ls’的員工薪水修改為4000元,job改為ccc涎永。
update emp set salary=4000,job='ccc' where name='zs';
將wu的薪水在原有基礎上增加1000元。
update emp set salar=salary+4000 where name='wu';
3.delete(刪)
delete from tab_name [where ....]
*如果不跟where語句則刪除整張表中的數據
*delete只能用來刪除一行記錄,不能只刪除一行記錄中的某一列值(這是update操作)羡微。
*delete語句只能刪除表中的內容谷饿,不能刪除表本身,想要刪除表妈倔,用drop
*同insert和update一樣博投,從一個表中刪除記錄將引起其它表的參照完整性問題,在修改數據庫數據時盯蝴,頭腦中應該始終不要忘記這個潛在的問題毅哗。
*TRUNCATE TABLE也可以刪除表中的所有數據,詞語句首先摧毀表捧挺,再新建表虑绵。此種方式刪除的數據不能在事務中恢復。
*示例:
刪除表中名稱為’zs’的記錄闽烙。
delete from emp where name='黃忠';
刪除表中所有記錄翅睛。
delete from emp;
使用truncate刪除表中記錄。
truncate table emp;
4.select(查)
(1)select [distinct] *|field1黑竞,field2捕发,......? from tab_name 其中from指定從哪張表篩選,*表示查找所有列很魂,也可以指定一個列列表明確指定要查找的列扎酷,distinct用來剔除重復行。
*示例:
查詢表中所有學生的信息遏匆。
select * from exam;
查詢表中所有學生的姓名和對應的英語成績法挨。
select name,english from exam;
過濾表中重復數據。
select distinct english from exam;
(2)select 也可以使用表達式幅聘,并且可以使用 as 別名
在所有學生分數上加10分特長分顯示坷剧。
select name,english+10,chinese+10,math+10 from exam;
統(tǒng)計每個學生的總分。
select name,english+chinese+math from exam;
使用別名表示學生總分喊暖。
select name,english+chinese+math as 總成績 from exam;
select name,english+chinese+math 總成績 from exam;
select name english from exam;
(3)使用where子句,進行過濾查詢撕瞧。
*示例:
查詢姓名為XXX的學生成績
select * from exam where name='張飛';
查詢英語成績大于90分的同學
select name,english from exam where english>90;
查詢總分大于200分的所有同學
select name,math+english+chinese as 總成績 from exam where math+english+chinese>200 ;
*where字句中可以使用:
*比較運算符:
> < >= <= <>?
between 10 and 20 值在10到20之間?
in(10,20,3)值是10或20或30
like '張pattern' pattern可以是%或者_陵叽,如果是%則表示任意多字符,此例中張三豐 張飛 張abcd 丛版,如果是_則表示一個字符張_巩掺,張飛符合。張
Is null
*邏輯運算符
在多個條件直接可以使用邏輯運算符 and or not
*示例
查詢英語分數在 80-100之間的同學页畦。
select name ,english from exam where english between 80 and 100;
查詢數學分數為75,76,77的同學胖替。
select name ,math from exam where math in (75,76,77);
查詢所有姓張的學生成績。
select * from exam where name like '張%';
查詢數學分>70,語文分>80的同學独令。
select name from exam where math>70 and chinese >80;
查找缺考數學的學生的姓名
select name from exam where math is null;
(4)Order by 指定排序的列端朵,排序的列即可是表中的列名,也可以是select 語句后指定的別名燃箭。
Asc 升序冲呢、Desc 降序,其中asc為默認值
ORDER BY 子句應位于SELECT語句的結尾招狸。
*示例:
對數學成績排序后輸出敬拓。
select * from exam order by math;
對總分排序按從高到低的順序輸出
select name ,(ifnull(math,0)+ifnull(chinese,0)+ifnull(english,0)) 總成績 from exam order by 總成績 desc;
對姓張的學生成績排序輸出
select name ,(ifnull(math,0)+ifnull(chinese,0)+ifnull(english,0)) 總成績 from exam where name like '張%' order by 總成績 desc;
(5)聚合函數:技巧,先不要管聚合函數要干嘛,先把要求的內容查出來再包上聚合函數即可裙戏。
count(列名)
統(tǒng)計一個班級共有多少學生乘凸?先查出所有的學生,再用count包上
select count(*) from exam;
統(tǒng)計數學成績大于70的學生有多少個累榜?
select count(math) from exam where math>70;
統(tǒng)計總分大于250的人數有多少营勤?
select count(name) from exam where (ifnull(math,0)+ifnull(chinese,0)+ifnull(english,0))>250;
sum(列名)
統(tǒng)計一個班級數學總成績?先查出所有的數學成績信柿,再用sum包上
select sum(math) from exam;
統(tǒng)計一個班級語文冀偶、英語、數學各科的總成績
select sum(math),sum(english),sum(chinese) from exam;
統(tǒng)計一個班級語文渔嚷、英語进鸠、數學的成績總和
select sum(ifnull(math,0)+ifnull(chinese,0)+ifnull(english,0)) as 總成績 from exam;
統(tǒng)計一個班級語文成績平均分
select sum(chinese)/count(*) from exam ;
注意:sum僅對數值起作用,否則會報錯形病。
AVG(列名):
IFNULL(expr1,expr2)
如果 expr1 不是 NULL客年,IFNULL() 返回 expr1,否則它返回 expr2漠吻。
IFNULL()返回一個數字或字符串值量瓜,取決于它被使用的上下文環(huán)境。
求一個班級數學平均分途乃?先查出所有的數學分绍傲,然后用avg包上。
select avg(ifnull(math,0)) from exam;
求一個班級總分平均分
select avg((ifnull(math,0)+ifnull(chinese,0)+ifnull(english,0))) from exam ;
Max耍共、Min
求班級最高分和最低分(數值范圍在統(tǒng)計中特別有用)
select Max((ifnull(math,0)+ifnull(chinese,0)+ifnull(english,0))) from exam;
select Min((ifnull(math,0)+ifnull(chinese,0)+ifnull(english,0))) from exam;
(6)group by字句烫饼,其后可以接多個列名,也可以跟having子句對group by 的結果進行篩選试读。
練習:對訂單表中商品歸類后杠纵,顯示每一類商品的總價
select product,sum(price) from orders group by product;
練習:查詢購買了幾類商品钩骇,并且每類總價大于100的商品
select product比藻,sum(price) from orders group by product having sum(price)>100;
!~having 和 where 的差別: where語句用在分組之前的篩選铝量,having用在分組之后的篩選,having中可以用合計函數银亲,where中就不行慢叨。使用where的地方可以用having替代。
示例:查詢商品列表中除了橘子以外的商品群凶,每一類商品的總價格大于500元的商品的名字
select product,sum(price) from orders where product<>'桔子'group by product having sum(price)>500;
(7)重點:Select from where group by having order by
Mysql在執(zhí)行sql語句時的執(zhí)行順序:from where select group by having order by
*分析:
select math+english+chinese as 總成績 from exam where 總成績 >250; ---- 不成功
select math+english+chinese as 總成績 from exam having 總成績 >250; --- 成功
select math+english+chinese as 總成績 from exam group by 總成績 having 總成績 >250; ----成功
select? math+english+chinese as 總成績 from exam order by 總成績;----成功
select * from exam as 成績 where 成績.math>85; ---- 成功
四插爹、約束
1.創(chuàng)建表時指定約束:
create table tb(
id int primary key auto_increment,
name varchar(20) unique not null,
ref_id int,
foreign key(ref_id) references tb2(id)
);
create table tb2(
id int primary key auto_increment
);
2.外鍵約束:
(1)增加外鍵:
可以明確指定外鍵的名稱,如果不指定外鍵的名稱,mysql會自動為你創(chuàng)建一個外鍵名稱请梢。
RESTRICT : 只要本表格里面有指向主表的數據赠尾, 在主表里面就無法刪除相關記錄。
CASCADE : 如果在foreign key 所指向的那個表里面刪除一條記錄毅弧,那么在此表里面的跟那個key一樣的所有記錄都會一同刪掉气嫁。
alter table book add [constraint FK_BOOK] foreign key(pubid) references pub_com(id) [on delete restrict] [on update restrict];
(2)刪除外鍵
alter table 表名 drop foreign key 外鍵(區(qū)分大小寫,外鍵名可以desc 表名查看);
3.主鍵約束:
(1)增加主鍵(自動增長够坐,只有主鍵可以自動增長)
Alter table tb add primary key(id) [auto_increment];
(2)刪除主鍵
alter table 表名 drop primary key
(3)增加自動增長
Alter table employee modify id int auto_increment;
(4)刪除自動增長
Alter table tb modify id int;
五寸宵、多表設計
一對一(311教室和20130405班級,兩方都是一):在任意一方保存另一方的主鍵
一對多元咙、多對一(班級和學生梯影,其中班級為1,學生為多):在多的一方保存一的一方的主鍵
多對多(教師和學生庶香,兩方都是多):使用中間表甲棍,保存對應關系
六、多表查詢
create table tb (id int primary key,name varchar(20) );
create table ta (
id int primary key,
name varchar(20),
tb_id int
);
insert into tb values(1,'財務部');
insert into tb values(2,'人事部');
insert into tb values(3,'科技部');
insert into ta values (1,'劉備',1);
insert into ta values (2,'關羽',2);
insert into ta values (3,'張飛',3);
mysql> select * from ta;
+----+------+-------+
| id | name | tb_id |
+----+------+-------+
|? 1 | aaa? |? ? 1 |
|? 2 | bbb? |? ? 2 |
|? 3 | bbb? |? ? 4 |
+----+------+-------+
mysql> select * from tb;
+----+------+
| id | name |
+----+------+
|? 1 | xxx? |
|? 2 | yyy? |
|? 3 | yyy? |
+----+------+
1.笛卡爾積查詢:兩張表中一條一條對應的記錄赶掖,m條記錄和n條記錄查詢感猛,最后得到m*n條記錄,其中很多錯誤數據
select * from ta ,tb;
mysql> select * from ta ,tb;
+----+------+-------+----+------+
| id | name | tb_id | id | name |
+----+------+-------+----+------+
|? 1 | aaa? |? ? 1 |? 1 | xxx? |
|? 2 | bbb? |? ? 2 |? 1 | xxx? |
|? 3 | bbb? |? ? 4 |? 1 | xxx? |
|? 1 | aaa? |? ? 1 |? 2 | yyy? |
|? 2 | bbb? |? ? 2 |? 2 | yyy? |
|? 3 | bbb? |? ? 4 |? 2 | yyy? |
|? 1 | aaa? |? ? 1 |? 3 | yyy? |
|? 2 | bbb? |? ? 2 |? 3 | yyy? |
|? 3 | bbb? |? ? 4 |? 3 | yyy? |
+----+------+-------+----+------+
2.內連接:查詢兩張表中都有的關聯數據,相當于利用條件從笛卡爾積結果中篩選出了正確的結果奢赂。
select * from ta ,tb where ta.tb_id = tb.id;
select * from ta inner join tb on ta.tb_id = tb.id;
mysql> select * from ta inner join tb on ta.tb_id = tb.id;
+----+------+-------+----+------+
| id | name | tb_id | id | name |
+----+------+-------+----+------+
|? 1 | aaa? |? ? 1 |? 1 | xxx? |
|? 2 | bbb? |? ? 2 |? 2 | yyy? |
+----+------+-------+----+------+
3.外連接
(1)左外連接:在內連接的基礎上增加左邊有右邊沒有的結果
select * from ta left join tb on ta.tb_id = tb.id;
mysql> select * from ta left join tb on ta.tb_id = tb.id;
+----+------+-------+------+------+
| id | name | tb_id | id? | name |
+----+------+-------+------+------+
|? 1 | aaa? |? ? 1 |? ? 1 | xxx? |
|? 2 | bbb? |? ? 2 |? ? 2 | yyy? |
|? 3 | bbb? |? ? 4 | NULL | NULL |
+----+------+-------+------+------+
(2)右外連接:在內連接的基礎上增加右邊有左邊沒有的結果
select * from ta right join tb on ta.tb_id = tb.id;
mysql> select * from ta right join tb on ta.tb_id = tb.id;
+------+------+-------+----+------+
| id? | name | tb_id | id | name |
+------+------+-------+----+------+
|? ? 1 | aaa? |? ? 1 |? 1 | xxx? |
|? ? 2 | bbb? |? ? 2 |? 2 | yyy? |
| NULL | NULL |? NULL |? 3 | yyy? |
+------+------+-------+----+------+
(3)全外連接:在內連接的基礎上增加左邊有右邊沒有的和右邊有左邊沒有的結果
select * from ta full join tb on ta.tb_id = tb.id; --mysql不支持全外連接
select * from ta left join tb on ta.tb_id = tb.id
union
select * from ta right join tb on ta.tb_id = tb.id;
mysql> select * from ta left join tb on ta.tb_id = tb.id
? ? -> union
? ? -> select * from ta right join tb on ta.tb_id = tb.id; --mysql可以使用此種方式間接實現全外連接
+------+------+-------+------+------+
| id? | name | tb_id | id? | name |
+------+------+-------+------+------+
|? ? 1 | aaa? |? ? 1 |? ? 1 | xxx? |
|? ? 2 | bbb? |? ? 2 |? ? 2 | yyy? |
|? ? 3 | bbb? |? ? 4 | NULL | NULL |
| NULL | NULL |? NULL |? ? 3 | yyy? |
+------+------+-------+------+------+
七陪白、分頁查詢
1,針對于SQLServer
1)
SELECT TOP 頁大小 * FROM B_USER? WHERE (? id NOT IN? (SELECT TOP ((頁碼-1)*頁大小) id? FROM B_USER ORDER BY id)) ORDER BY ID
select top 10 * from B_USER order by id 查詢前十條數據(1—10)
select top 10 * from B_USER where id not in(select top 10 id from B_USER order by id) order by id 查詢前十條數據(11—20)
select top 10 * from B_USER where id not in(select top 20 id from B_USER order by id) order by id 查詢前十條數據(21—30)
可用膳灶,主鍵id可以是任意類型
2)
SELECT TOP 頁大小 *? FROM (? SELECT? ROW_NUMBER() OVER (ORDER BY id) AS RowNo? FROM B_USER? ) AS A? WHERE RowNo> " + (頁碼-1)*頁大小
select top 10 *
from
(
select row_number() over(order by id) as rownumber,* from B_USER
) A
where rownumber > 10
可用咱士,主鍵id可以是任意類型
3)
select top 頁大小 * from table1 where id> (select isnull(max(id),0) from (select top ((頁碼-1)*頁大小) id from table1 order by id) as T? ) order by id
select top 10 * from B_USER
where id >
(
????select isnull(max(id),0)
????from
? ???? (
? ????select top 20 id from B_USER order by id
????????) A
)
order by id
可用,主鍵id可以是任意類型
4)
Select top 10 * from test where id>10
可用轧钓,主鍵id可以只能是int型
2司致,MySQL
MySQL數據庫實現分頁比較簡單,提供了 LIMIT函數聋迎。一般只需要直接寫到sql語句后面就行了。
LIMIT子句可以用來限制由SELECT語句返回過來的數據數量枣耀,它有一個或兩個參數霉晕,如果給出兩個參數庭再,第一個參數指定返回的第一行在所有數據中的位置,從0開始(注意不是1)牺堰,第二個參數指定最多返回行數拄轻。
例如:
select * from table WHERE … LIMIT 10; #返回前10行
select * from table WHERE … LIMIT 0,10; #返回前10行
select * from table WHERE … LIMIT 10,20; #返回第10-20行數據?
八,模糊查詢
正常數據庫的SQL查詢語句:
select * from [Project].[dbo].[InstallInfo_Gj] where CarCode like '5%'
Android中(SQLlite)模糊查詢的使用方法
Cursor c = db.rawQuery("SELECT carnum FROM CarNum where carnum like ?", new String[] { key+"%" }); 這條語句是正確的
Cursor c = db.rawQuery("SELECT carnum FROM CarNum where carnum like ?%", new String[] { key }); 這條是錯誤的
九伟葫,使用MyBatis時的分頁查詢以及多參數傳遞
1恨搓,對于傳遞多參數,以Map的形式
<!-- 在MyBatis的select筏养、insert斧抱、update、delete這些元素中都提到了parameterType這個屬性渐溶。MyBatis現在可以使用的parameterType有基本數據類型和JAVA復雜數據類型
基本數據類型:包含int,String,Date等辉浦。基本數據類型作為傳參茎辐,只能傳入一個宪郊。通過#{參數名} 即可獲取傳入的值
復雜數據類型:包含JAVA實體類、Map拖陆。通過#{屬性名}或#{map的KeyName}即可獲取傳入的值
-->
參考與上面的解說弛槐,使用#{map的KeyName}即可獲取傳入的值;
示例:
<select id="SelectRadioBymap" parameterType="java.util.Map" resultType="_RadioBean">
select * from radio where id > #{id} and type = #{type}
</select>
//傳遞多參數依啰,以Map的形式
@Test
public void testSelectRadioBymap() {
SqlSession sqlSession = getSqlSession();
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("id", 5);
paramMap.put("type", "computer");
List<RadioBean> c = sqlSession.selectList("com.examination.mapping.RadioBeanMapper.SelectRadioBymap", paramMap);
System.out.println(c);
}
注意乎串,這里使用的是Map<String, Object>,故而可以傳遞所有類型的參數約束孔飒。
當然灌闺,對于傳遞多參數約束也可以使用JavaBean,只不過為了傳遞參數而專門建個bean坏瞄,有點浪費
2桂对,對于分頁
當SQL語句中使用top時,在該關鍵字的后面如果跟著參數鸠匀,這是將不能使用#而應該使用$,當然對于其他的參數調用則只能使用#蕉斜,如果使用$則會報錯
//分頁查詢
@Test
public void testSelectRadioBypage() {
SqlSession sqlSession = getSqlSession();
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("page", 2);
paramMap.put("size", 10);
List<RadioBean> c = sqlSession.selectList("com.examination.mapping.RadioBeanMapper.SelectRadioBypage", paramMap);
System.out.println(c);
}
<!-- select top 10 * from radio where id > (select isnull(max(id),0) from (select top ((2-1)*10) id from radio order by id) as T ) order by id -->
<select id="SelectRadioBypage" parameterType="java.util.Map" resultType="_RadioBean">
select top ${size} * from radio where id > (select isnull(max(id),0) from (select top ((${page}-1)*${size}) id from radio order by id) as T ) order by id?
</select>
完整示例:
Mapper:
<mapper namespace="com.examination.mapping.RadioBeanMapper">
<select id="SelectRadioByid" parameterType="String" resultType="_RadioBean">
select * from radio where id = #{id}
</select>
<select id="SelectRadioall" resultType="_RadioBean">
select * from radio
</select>
<!-- select top 10 * from radio where id > (select isnull(max(id),0) from (select top ((2-1)*10) id from radio order by id) as T ) order by id -->
<select id="SelectRadioBypage" parameterType="java.util.Map" resultType="_RadioBean">
select top ${size} * from radio where id > (select isnull(max(id),0) from (select top ((${page}-1)*${size}) id from radio order by id) as T ) order by id?
</select>
<select id="SelectRadioBymap" parameterType="java.util.Map" resultType="_RadioBean">
select * from radio where id > #{id} and type = #{type}
</select>
</mapper>
java:
//獲取SqlSession
public SqlSession getSqlSession(){
//mybatis的配置文件
? ? ? ? String resource = "mybatisConfig.xml";
? ? ? ? //使用類加載器加載mybatis的配置文件(它也加載關聯的映射文件)
? ? ? ? InputStream is = MyBatisTest.class.getClassLoader().getResourceAsStream(resource);
? ? ? ? //構建sqlSession的工廠
? ? ? ? SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
? ? ? ? //使用MyBatis提供的Resources類加載mybatis的配置文件(它也加載關聯的映射文件)
? ? ? ? //Reader reader = Resources.getResourceAsReader(resource);
? ? ? ? //構建sqlSession的工廠
? ? ? ? //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
? ? ? ? //創(chuàng)建能執(zhí)行映射文件中sql的sqlSession
? ? ? ? SqlSession session = sessionFactory.openSession();
? ? ? ? return session;
}
//根據id查詢
@Test
public void testSelectRadioByid() {
SqlSession sqlSession = getSqlSession();
RadioBean c = sqlSession.selectOne("com.examination.mapping.RadioBeanMapper.SelectRadioByid", "1");
System.out.println(c);
}
//查詢全部
@Test
public void testSelectRadioall() {
SqlSession sqlSession = getSqlSession();
List<RadioBean> c = sqlSession.selectList("com.examination.mapping.RadioBeanMapper.SelectRadioall");
System.out.println(c);
}
//分頁查詢
@Test
public void testSelectRadioBypage() {
SqlSession sqlSession = getSqlSession();
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("page", 2);
paramMap.put("size", 10);
List<RadioBean> c = sqlSession.selectList("com.examination.mapping.RadioBeanMapper.SelectRadioBypage", paramMap);
System.out.println(c);
}
//傳遞多參數,以Map的形式
@Test
public void testSelectRadioBymap() {
SqlSession sqlSession = getSqlSession();
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("id", 5);
paramMap.put("type", "computer");
List<RadioBean> c = sqlSession.selectList("com.examination.mapping.RadioBeanMapper.SelectRadioBymap", paramMap);
System.out.println(c);
}
//where中>和in
select * from B_USER where create_date > '2016-03-27 21:30:31.103'
UPDATE b_user set amount = 0 WHERE id IN ('16cb318719314418af80947ee88173f4',
'1dbfe1dccdb74148b1f6cbfc866beb47','e1655a888d424e3a87767abb15e72a0a')