2 MyBatis - 對映配置檔案:SQL編寫(案例)
阿新 • • 發佈:2020-11-27
MyBatis - 對映配置檔案:SQL編寫
一般性CRUD
1 查詢所有:selectAll
<mapper namespace="StudentMapper"> <!-- select:查詢功能的標籤 id屬性:唯一標識 resultType屬性:指定結果對映物件型別,可以不寫 parameterType屬性:指定引數對映物件型別,可以不寫 --> <select id="selectAll" resultType="student"> SELECT * FROM student </select> </mapper> 對應java語句: List<Student> list = sqlSession.selectList("StudentMapper.selectAll"); sqlSession.close();
2 依據id查詢:selectById
<mapper namespace="StudentMapper"> <select id="selectById" resultType="student" parameterType="int"> SELECT * FROM student WHERE id = #{id} </select> </mapper> 對應java語句: Student stu = sqlSession.selectOne("StudentMapper.selectById", 3); sqlSession.close();
3 模糊查詢:姓名含有“張” - 三種方式
<mapper namespace="StudentMapper"> <select id="findByName" resultType="com.itheima.domain.Student" parameterType="String"> <!-- 1 --> select * from student where name like '%${value}%' <!-- 2 --> select * from student where name like concat{'%', #{name}, '%'} <!-- 3 --> select * from student where name like #{name} </select> </mapper> 對應java語句: <!-- 1 --> List<Student> list = sqlSession.selectList("StudentMapper.findByName", "張"); <!-- 2 --> List<Student> list = sqlSession.selectList("StudentMapper.findByName", "張"); <!-- 3 --> List<Student> list = sqlSession.selectList("StudentMapper.findByName", "%張%"); sqlSession.close();
4 查詢student表記錄數
<mapper namespace="StudentMapper">
<select id="findCount" resultType="int">
select count(*) from student
</select>
</mapper>
對應java語句:
int count = sqlSession.selectOne("StudentMapper.findCount");
sqlSession.close();
5 插入資料
<mapper namespace="StudentMapper">
<insert id="insert" parameterType="student">
INSERT INTO student VALUES (#{id},#{name},#{age})
</insert>
</mapper>
對應java語句:
int result = sqlSession.insert("StudentMapper.insert", stu);
sqlSession.commit();
sqlSession.close();
6 插入資料,並返回新增資料的主鍵id
<mapper namespace="StudentMapper">
<!--
keyColumn: 獲取哪個欄位的值
keyProperty: 將查詢結果封裝到物件的哪個屬性
useGeneratedKeys: 是否返回最後新增資料的主鍵id值
-->
<insert id="saveUserReturnId2" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
insert into student values(#{id},#{name},#{age})
</insert>
</mapper>
對應java語句:
sqlSession.insert("StudentMapper.saveUserReturnId2", student);
sqlSession.commit();
sqlSession.close();
7 修改資料:依據id設定student 的 name、age
<mapper namespace="StudentMapper">
<update id="update" parameterType="student">
UPDATE student SET name = #{name},age = #{age} WHERE id = #{id}
</update>
</mapper>
對應java語句:
int result = sqlSession.update("StudentMapper.update",stu);
sqlSession.commit();
sqlSession.close();
8 依據id刪除資料
<mapper namespace="StudentMapper">
<delete id="delete" parameterType="int">
DELETE FROM student WHERE id = #{id}
</delete>
</mapper>
對應java語句:
int result = sqlSession.delete("StudentMapper.delete",5);
sqlSession.commit();
sqlSession.close();
動態SQL
9 多條件查詢(可缺少部分條件)
<mapper namespace="StudentMapper">
<!--第一個AND會被自動去除-->
<select id="selectCondition" resultType="student" parameterType="student">
<include refid="select"/>
<where>
<if test="id != null">
AND id = #{id}
</if>
<if test="name != null">
AND name = #{name}
</if>
<if test="age != null">
AND age = #{age}
</if>
</where>
</select>
</mapper>
對應java程式碼:
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student stu = new Student();
stu.setId(2);
stu.setName("李四");
List<Student> list = mapper.selectCondition(stu);
for (Student student : list) {
System.out.println(student);
}
sqlSession.close();
inputStream.close();
10 實現 SELECT * FROM student WHERE id IN (1,2,5) - 引數型別list
<mapper namespace="StudentMapper">
<!--
collection:引數容器型別, (list-集合, array-陣列)。
open:開始的 SQL 語句。
close:結束的 SQL 語句。
item:引數變數名。
separator:分隔符。
-->
<!-- 範圍查詢: 查詢條件是List -->
<select id="selectByIds" resultType="student" parameterType="list">
<include refid="select"/>
<where>
<foreach collection="list" open="id IN (" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
</mapper>
對應java程式碼:
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Integer> ids = new ArrayList<>();
ids.add(1);
ids.add(2);
ids.add(5);
List<Student> list = mapper.selectByIds(ids);
for (Student student : list) {
System.out.println(student);
}
sqlSession.close();
inputStream.close();
11 實現 SELECT * FROM student WHERE id IN (1,2,5) - 引數型別array
<mapper namespace="StudentMapper">
<!--注意: 引數型別為陣列, parameterType型別必須設定為list-->
<select id="findByIdsArray" resultType="Student" parameterType="list">
SELECT * FROM student
<where>
<foreach collection="array" open="id in (" item="id" separator="," close=")">
#{id}
</foreach>
</where>
</select>
</mapper>
對應java程式碼:
Integer[] ids = {1,2,3};
List<Student> students = dao.findByIdsArray(ids);
for (Student s : students) {
System.out.println(s);
}
sqlSession.close();
inputStream.close();
12 動態更新(修改)資料(可缺少部分引數)
<mapper namespace="StudentMapper">
<update id="updateStu">
update student set
<set>
<!-- 判斷條件, 控制set後面的引數 逗號會自動控制 建議都寫-->
<if test="name !=null and name!=''">
name = #{name},
</if>
<if test="age !=null and age!=0">
age = #{age},
</if>
</set>
<where>
<if test="id!=null and id!=0">
id=#{id}
</if>
</where>
</update>
</mapper>
對應java程式碼:
Student stu = new Student();
stu.setId(1);
stu.setName("武2郎");
stu.setAge(18);
dao.updateStu(stu);
sqlSession.commit();
sqlSession.close();
inputStream.close();
抽取sql片段簡化編寫
<mapper namespace="StudentMapper">
<select id="selectById" resultType="student" parameterType="int">
SELECT * FROM student WHERE id = #{id}
</select>
</mapper>
等價於:
<mapper namespace="StudentMapper">
<sql id="selectStudent">select * from student</sql>
<select id="findById" parameterType="int" resultType="student">
<include refid="selectStudent"></include> where id=#{id}
</select>
</mapper>
資料表字段 與 JavaBean類屬性 名稱不一致
<mapper namespace="StudentMapper">
<!--
查詢的【資料庫中表列名】和實體類的【JavaBean類屬性】名稱不一致情況
id標籤:主鍵
result標籤:非主鍵
property: 配置為實體類的屬性名
column: 配置為SQL列名
若物件屬性和列名一致時, 可以不配置. 但是為了程式碼的閱讀性,建議都配置上.
-->
<resultMap id="userMap" type="com.itheima.domain.User">
<id property="userId" column="id"></id>
<result property="userName" column="username"></result>
<result property="userAddress" column="address"></result>
<result property="userSex" column="sex"></result>
<result property="userBirthday" column="birthday"></result>
</resultMap>
對應:
<select id="findAll" resultMap="userMap">
select * from user
</select>
</mapper>