基本查詢
查詢數(shù)據(jù)(單表查詢 多表連接 組合查詢 分頁查詢 有參數(shù) 沒參數(shù) 多個參數(shù) 返回值是一條 返回值是多條)
<select id="getInfoById" parameterType="int" resultType="UserInfo" >
select * from userinfo where id=#{id}
</select>
- resultType:
- 如果查詢返回的是一條數(shù)據(jù),則直接寫返回值類型
- 如果查詢返回的是多條數(shù)據(jù),則寫集合中的泛型
<select id="getAll" resultType="UserInfo" >
select * from userinfo
</select>
返回值
- 如果沒有參數(shù),則省略parameterType屬性
- 如果有一個參數(shù),是基本數(shù)據(jù)類型,則SQL語句中獲取參數(shù)時,用什么名字都能獲取到
但是基于編碼規(guī)范,建議使用接口中形參的名字 - 如果有多個參數(shù),解決方案:
省略parameterType屬性,SQL語句中通過參數(shù)的索引值獲取參數(shù),
索引值從0開始(不建議使用)使用實體類的類型傳入,SQL語句中獲取參數(shù)時,使用的是
實體類中的屬性名使用map類型傳入,SQL語句中獲取參數(shù)時,使用的是
map中元素的key
UserInfoMapper.java類
public UserInfo getUsers(Map<String, String> map);
UserInfoMapper.xml文件
<select id="getUsers" resultType="UserInfo" >
select * from userinfo where username= #{0} and password=#{1}
</select>
---------------------------------------------------------------------------------------------------------------
<select id="getUsers" parameterType="UserInfo" resultType="UserInfo" >
select * from userinfo where username=#{username} and password=#{password}
</select>
<select id="getUsers" parameterType="map" resultType="UserInfo" >
select * from userinfo where username=#{k_username} and password=#{k_password}
</select>
Test類
UserInfo user = new UserInfo();
user.setUsername("111");
user.setPassword("111");
UserInfo u = mapper.getUsers(user);
System.out.println(u);
---------------------------------------------------------------------------------------------------------------
Map<String, String> map = new HashMap<>();
map.put("k_username", "111");
map.put("k_password", "111");
UserInfo u = mapper.getUsers(map);
System.out.println(u);
組合查詢
-
where if- 字符串類型判空:username!=null and username!=''
- int類型判空:id!=0
- Date類型:date!=null
<select id="getUser" parameterType="UserInfo" resultType="UserInfo" >
select * from userinfo
<where>
<if test="username!=null and username!='' ">
and username like #{username}
</if>
<if test="email!=null and email!='' ">
and email=#{email}
</if>
</where>
</select>
分頁查詢
<select id="getPageUser" parameterType="map" resultType="UserInfo">
select * from userInfo limit #{k_index},#{k_pagesize}
</select>
查詢入職日期在指定日期之前 <
<select id="getEmpByHiredate" parameterType="Date" resultType="EmpInfo">
select * from emp where hiredate < #{hiredate}
</select>
Test部分(日期轉換)
String strdate="1985-01-01";
Date utilDate=null;
try {
utilDate = new SimpleDateFormat("yyyy-MM-dd").parse(strdate);
} catch (ParseException e) {
e.printStackTrace();
}
java.sql.Date hiredate = new java.sql.Date(utilDate.getTime());
List<EmpInfo> list1 = mapper.getEmpByHiredate(hiredate);
for(EmpInfo e:list1){
System.out.println(e);
}
插入數(shù)據(jù)
存儲數(shù)據(jù)
<insert id="insertEmp" parameterType="EmpInfo" >
insert into emp(ename,job,mgr,hiredate,sal,comm,deptno)
values (#{ename},#{job},#{mgr},#{hiredate},#{sal},#{comm},#{deptno})
</insert>
存儲數(shù)據(jù):主鍵采用最大值+1的方式生成
selectKey標簽:
-
keyProperty屬性:查詢到的數(shù)據(jù)賦值給Emp中的哪個屬性 -
resultType屬性:查詢的返回值類型 -
order屬性:設置查詢是在insert之前執(zhí)行還是之后執(zhí)行
<insert id="insertEmp" parameterType="EmpInfo" >
<selectKey keyProperty="empno" resultType="int" order="BEFORE" >
select max(empno)+1 from emp
</selectKey>
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno)
values (#{empno},#{ename},#{job},#{mgr},#{hiredate},#{sal},#{comm},#{deptno})
</insert>
更新數(shù)據(jù)
<update id="updateEmp" parameterType="EmpInfo">
update emp
set sal =#{sal},comm=#{comm}
where empno=#{empno}
</update>
Test部分 自動提交:session.commit();
//插入數(shù)據(jù)
EmpInfo e= new EmpInfo();
e.setEname("test012151");
e.setJob("salesman");
e.setMgr(7369);
e.setHiredate(new java.sql.Date(new Date().getTime()));
e.setSal(2560);
e.setComm(5693);
e.setDeptno(10);
mapper.insertEmp(e);
session.commit();
System.out.println("新生成的主鍵值"+e.getEmpno());*/
//修改數(shù)據(jù)
EmpInfo e = new EmpInfo();
e.setEmpno(7937);
e.setSal(10000);
e.setComm(8000);
mapper.updateEmp(e);
session.commit();
刪除數(shù)據(jù)
根據(jù)一個元素刪除數(shù)據(jù)
批量刪除
foreach標簽:實現(xiàn)循環(huán)遍歷
-
collection屬性:要遍歷的集合類型 -
item屬性:每次遍歷得到的元素的名稱(隨意定義) -
open屬性:遍歷的結果以什么字符開頭 -
close屬性:遍歷的結果以什么字符結尾 -
separator屬性:遍歷得到的元素以什么字符作為分隔符
xml文件
<delete id="deleteEmps" >
delete from emp where empno in
<foreach collection="array" item="item" open="(" close=")" separator="," >
#{item}
</foreach>
</delete>
組合查詢
在Bean包中:存在兩個實體類(1:有外鍵
關聯(lián)2;2:無外鍵)
1:定義2的成員變量
2:定義1的List<1>的成員變量

一對一查詢 emp==>dept
association標簽:一對一查詢 嵌套結果集處理
-
property屬性:嵌套的Emp的resultMap封裝的結果要賦值給當前實體類Emp的那個屬性 -
column屬性:外鍵列 -
javaType屬性:嵌套的EmpresultMap的返回值類型 -
resultMap屬性:對應的(Dept)要嵌套的resultMap


xml文件
<!-- 一對一查詢 -->
<select id="getEmpInfo" parameterType="int" resultMap="eresult" >
select e.*, d.deptno ddeptno,d.dname,d.loc
from emp e,dept d
where empno=#{empno} and e.deptno=d.deptno
</select>
<resultMap type="Emp" id="eresult">
<result property="empno" column="empno" />
<result property="ename" column="ename" />
<result property="job" column="job" />
<result property="mgr" column="mgr" />
<result property="sal" column="sal" />
<result property="hiredate" column="hiredate" />
<result property="comm" column="comm" />
<result property="deptno" column="deptno" />
<association property="dept" column="deptno" javaType="Dept" resultMap="dresult" />
</resultMap>
<resultMap type="Dept" id="dresult">
<result property="dname" column="dname" />
<result property="loc" column="loc" />
<result property="deptno" column="ddeptno" />
</resultMap>
一對多查詢 dept==>emp
collection標簽:一對多查詢
-
property屬性:構建的集合對象賦值給最終返回值類型中哪個屬性 -
ofType屬性:集合中元素的類型


xml文件
<select id="getDept" parameterType="int" resultMap="dresult" >
select e.*,d.deptno ddeptno,d.dname,d.loc
from emp e,dept d
where d.deptno=#{deptno} and e.deptno=d.deptno
</select>
<resultMap type="Dept" id="dresult">
<result property="deptno" column="ddeptno" />
<result property="dname" column="dname" />
<result property="loc" column="loc" />
<collection property="emps" ofType="Emp">
<result property="empno" column="empno" />
<result property="ename" column="ename" />
<result property="job" column="job" />
<result property="mgr" column="mgr" />
<result property="sal" column="sal" />
<result property="hiredate" column="hiredate" />
<result property="comm" column="comm" />
<result property="deptno" column="deptno" />
</collection>
</resultMap>
多對多查詢 order==>product==>customer
association標簽:一對一查詢 嵌套結果集處理
-
property屬性:嵌套的Emp的resultMap封裝的結果要賦值給當前實體類Emp的那個屬性 -
column屬性:外鍵列 -
javaType屬性:嵌套的EmpresultMap的返回值類型 -
resultMap屬性:對應的(Dept)要嵌套的resultMap

xml文件
<select id="getAll" resultMap="oresult" >
select o.*,c.cid ccid,c.cname,c.tel,c.address,p.pid ppid,p.pname,p.price pprice
from orders o,customer c,product p
where o.cid=c.cid and o.pid=p.pid
</select>
<resultMap type="Order" id="oresult">
<result property="oid" column="oid" />
<result property="pid" column="pid" />
<result property="cid" column="cid" />
<result property="count" column="count" />
<result property="price" column="price" />
<association property="p" column="pid" javaType="Product" resultMap="presult" />
<association property="c" column="cid" javaType="Customer" resultMap="cresult" />
</resultMap>
<resultMap type="Product" id="presult">
<result property="pid" column="ppid" />
<result property="pname" column="pname" />
<result property="price" column="pprice" />
</resultMap>
<resultMap type="Customer" id="cresult">
<result property="cid" column="ccid" />
<result property="cname" column="cname" />
<result property="tel" column="tel" />
<result property="address" column="address" />
</resultMap>
Sql片段
include標簽:用于SQL片段中
- refid屬性:獲取sql片段
//Sql片段
<sql id="sid">
select * from emp
</sql>
<select id="getEmpByName" parameterType="string" resultType="Emp" >
<include refid="sid"/>
where ename like #{ename}
<select>
mybatis緩存:
針對查詢類需求,提高程序性能,減輕數(shù)據(jù)庫訪問壓力,提供的緩存技術
一級緩存:默認開啟的
- 針對同一個SqlSession,如果基于某一個需求查詢到一份數(shù)據(jù),則mybatis會將查詢結果存儲在緩存中,接下來如果再一次做同樣的查詢,則mybatis會直接從緩存中拿數(shù)據(jù),而不訪問數(shù)據(jù)庫,從而提升查詢的性能
- 手動不開啟緩存:flushCache="true"
- 如果過程中執(zhí)行了該表的DML操作,則緩存數(shù)據(jù)自動消失
二級緩存:
- 不限定SqlSession的緩存的使用
- 不是默認開啟的
- 需要手動開啟(在SqlMapConfig.xml中)