輸入參數
在Mybatis中輸入參數parameterType有兩種類型,分別是簡單類型和對象類型。表達方式也分兩種,分別是#{} 和 ${}。
#{}自動給String類型加上’’(自動類型轉換)
${}原樣輸出,但是適合於 動態排序(動態字段),如果輸入類型是String類型,需要在外部加單引號
簡單類型(8個基本類型+Sring)使用#{}表示時可以在{}裏面寫任意值,使用${}表示時{}裏面只能爲value。
對象類型使用#{}和${}都需要在{}裏面填寫屬性值
主要代碼
//mapper.xml代碼
<select id="queryStudentOrderByColumn" resultType="student" parameterType="string">
select stuno,stuname,stuage from student order by ${value} desc
</select>
//java代碼
List<Student> student = studentMapper.queryStudentOrderByColumn("stuname");
在queryStudentOrderByColumn中寫入要查詢的字段名(只有數據庫中存在的即可),然後會從數據庫中獲取數據並進行排序。這樣不需要查詢特定的字段名。
輸出參數
輸出參數resultType分簡單類型(8個基本類型+String)、實體對象類型、實體對象類型的集合和HashMap。
1、輸出參數爲簡單類型
<select id="queryStudentCount" resultType="int">
select count(*) from student
</select>
2、輸出參數爲實體對象類型
<select id="queryPersonByStuno" resultType="student" parameterType="int">
select * from student where stuno = #{stuno}
</select>
3、輸出參數爲實體對象類型的集合
mapper.xml中的返回值爲該類的類型,但在mapper接口中需定義
爲集合類型,即List<Student>
<select id="queryAllStudents" resultType="student">
select * from student
</select>
4、輸出參數爲HashMap
<select id="queryStudentOutByHashMap" resultType="HashMap">
<!-- 別名作爲Map的key,如下列代碼的key爲"no"和"name" -->
<!-- 使用HashMap查詢單個學生 -->
select stuno "no",stuname "name" from student where stuno=1
</select>
<select id="queryAllStudentsOutByHashMap" resultType="HashMap">
<!-- 使用HashMap實現查詢多個學生 -->
select stuno "no",stuname "name" from student
</select>
5、屬性名 和 字段名不一致。在resultMap中,子元素id用於設置主鍵字段與領域模型屬性的映射關係,子元素result用於設置普通字段與領域模型屬性的映射關係。
<!-- 1)、使用resultMap指定輸出類型及映射關係 -->
<select id="queryStudentByid" parameterType="int" resultMap="queryStudentByidMap">
select id,sname from student where id=#{id}
</select>
<resultMap id="queryStudentByidMap" type="student">
<!-- 指定類中的屬性 和 表中的字段對應關係 -->
<id property="stuNo" column="id"/>
<result property="stuName" column="sname"/>
</resultMap>
<!-- 2)、使用使用resultType+HashMap別名 -->
<select id="queryStudentByIdWithHashMap" parameterType="int" resultType="student">
<!-- select 表的字段名 "類的屬性名" ... 來指定字段名和屬性名的對應關係-->
select id "stuNo",sname "stuName" from student where id=#{id}
</select>
MyBatis調用存儲過程
在oracle中執行
create or replace procedure queryCountByGradeWithProcedure(gName in varchar,scount out number)
as
begin
select count(1) into scount from student where graname = gname;
end;
/
通過調用存儲過程 實現諮詢
statementType=“CALLABLE”:表示調用的方式是存儲過程
存儲過程的輸入參數,在mybatis用Map來傳遞(HashMap)
在Mybatis中執行
<select id="queryCountByGradeWithProcedure"
statementType="CALLABLE"
parameterType="HashMap">
{
CALL queryCountByGradeWithProcedure(
#{gName,jdbcType=VARCHAR,mode=IN},
#{sCount,jdbcType=INTEGER,mode=OUT}
)
}
</select>
動態SQL
通過 if, choose, when, otherwise, trim, where, set, foreach等標籤,可組合成非常靈活的SQL語句,從而在提高 SQL 語句的準確性的同時,也大大提高了開發人員的效率。
if中的test的值爲表字段名對應的類屬性名(區分大小寫)
//if跟where結合
<select id="queryStuByNOrAWithSQLTag" parameterType="student" resultType="student">
select stuno ,stuname ,stuage from student where
<where>
<if test="stuName !=null and stuName!=''">
and stuname = #{stuName}
</if>
<if test="stuAge !=null and stuAge!=0">
and stuage = #{stuAge}
</if>
</where>
</select>
foreach標籤
collection爲迭代的類型:簡單類型數組(String+8個基本類型)、對象數組、集合、屬性(定義屬性時用集合或數組類型)
open:主語句前部分
close:主語句後部分
item:迭代標籤
separator:迭代標籤的分隔符
以迭代類型爲例,當迭代的類型爲屬性時,test和collection都用傳入的屬性名進行表示,parameterType爲要傳入的參數類型 ;當迭代的類型爲簡單類型數組時,test和colletion都用array表示,parameterType爲數組類型;當迭代的類型爲集合時,test和colletion都用list表示,parameterType也爲list;當迭代的類型爲對象數組時,test和colletion都用array表示,parameterType爲Object[];
<select id="queryStudentWithForeach" parameterType="stuid" resultType="student">
select * from student
<where>
<if test="stuNos!=null and stuNos.size>0">
<foreach collection="stuNos" open=" and stuno in (" close=")" item="stuNo" separator=",">
#{stuNo}
</foreach>
</if>
</where>
</select>
sql片段:
1、提取相似代碼:把代碼放入sql標籤裏,一般把sql標籤放在mapper文件最前面
2、引用:使用include標籤,在refid中放入sql標籤的id
<!--
<sql id="ObjectArray2">
<where>
<if test="array!=null and array.length>0">
<foreach collection="array" open=" and stuno in (" close=")" item="student" separator=",">
#{student.stuNo}
</foreach>
</if>
</where>
</sql>
<select id="queryStudentWithObjectArray" parameterType="Object[]" resultType="student">
select * from student
<include refid="ObjectArray2">
</include>
</select>
-->
關聯查詢
分爲一對一、一對多、多對一和多對多。
在Mybatis中一對多、多對一、多對多的實現方法相似。故下面只講述一對一和一對多。能通過業務擴展類實現,也能通過resultMap實現。
使用業務擴展類實現一對一:一般需要三個類,其中兩個類分別寫不同屬性,最後再用一個類統一起來,其核心爲:用resultType指定類的屬性 包含 多表查詢的所有字段
<!--
<select id="queryStudentWithOO" parameterType="int" resultType="StudentBusiness">
select s.*,c.* from student s inner join studentcard c
on s.cardid=c.cardid
where s.stuno = #{stuNo}
</select>
利用resultMap實現一對一:通過 屬性成員 將2個類建立起聯繫,對象成員使用association映射;javaType指定該屬性的類型
<select id="queryStudentWithOO2" parameterType="int" resultMap="student_card_map">
select s.*,c.* from student s inner join studentcard c
on s.cardid=c.cardid
where s.stuno = #{stuNo}
</select>
<resultMap id="student_card_map" type="student">
<id property="stuNo" column="stuNo"/>
<result property="stuName" column="stuName"/>
<result property="stuAge" column="stuAge"/>
<association property="studentCard" javaType="StudentCard">
<id property="cardId" column="cardId"/>
<result property="cardInfo" column="cardInfo"/>
</association>
</resultMap>
-->
利用resultMap實現一對多:對象成員使用collection映射;描述屬性的元素的類型用ofType
<select id="queryOM" parameterType="int" resultMap="class_student_map">
<!-- 查詢g1班的班級信息和g1班的所有學生信息 -->
select c.*,s.* from student s
inner join studentclass c
on c.classid = s.classid
where c.classid = #{classId}
</select>
<resultMap id="class_student_map" type="StudentClass">
<id property="classId" column="classId"/>
<result property="className" column="className"/>
<!-- 配置成員屬性,一對多 -->
<collection property="students" ofType="Student">
<id property="stuNo" column="stuNo"/>
<result property="stuName" column="stuName"/>
<result property="stuAge" column="stuAge"/>
</collection>
</resultMap>