Mybatis自定義SQL的關係對映、分頁、排序
技術標籤:Mybatis-plus
目的: 記錄資料庫表與實體物件之間不同的對映關係如何用mybatis的自定義sql和結果返回集處理。
1、三種物件對映關係
1.1 一對一
一個人對應一個身份證,一位同學對應一個班級,每個房間都有自己的房間號,當一個事物它對應另一個事物是唯一的,那麼它們之間的關係就是一對一的。
這裡我演示的案例是,一個學生有著一位老師
老師基礎資訊:
學生詳細資訊:
如果說,我們需要將兩個表一起查出來,我們可以這麼做:
問題: 如果物件的列重複了,必須要使用到別名
1、先定義實體結構,也就是我們返結果的實體類
public class Student {
@TableId
private int id;
private String name;
private int tid;
@TableField(exist = false)
private Teacher teacher;
}
Teacher:
public class Teacher {
@TableId
private int id;
private String name;
}
2、 編寫xml檔案
這裡有兩種方式,使用association時的關鍵在於告訴mybatis如何載入關聯(assocition)。
- 巢狀查詢:通過執行另外一個 SQL 對映語句來返回預期的複雜型別。
- 巢狀結果:使用巢狀結果對映來處理重複的聯合結果的子集。
第一種: 使用巢狀查詢,也就是使用另一個sql
// teacherMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace=" com.lll.mybatisplusdemo.mapper.TeacherMapper">
<select id="getTeacher" parameterType="int" resultType="teacher">
select * from teacher where id = #{id};
</select>
</mapper>
// studentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<select id="getStudent2" parameterType="int" resultMap="getStudentMap2">
select * from student where id =#{id};
</select>
<resultMap id="getStudentMap2" type="student">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<result column="tid" property="tid"></result>
<association property="teacher" javaType="Teacher" column="tid" select="com.lll.mybatisplusdemo.mapper.TeacherMapper.getTeacher">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
</association>
</resultMap>
</mapper>
巢狀查詢的方式很簡單,但是對於大型資料集合和列表將不會表現很好。問題就是我們熟知的
“N+1 查詢問題”。概括地講, N+1 查詢問題可以是這樣引起的:
- 你執行了一個單獨的 SQL 語句來獲取結果列表(就是“+1”)。
- 對返回的每條記錄,你執行了一個查詢語句來為每個載入細節(就是“N”)。
第二種: 使用巢狀結果來對映聯合查詢來的資料
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lll.mybatisplusdemo.mapper.StudentMapper">
<select id="getStudent" parameterType="int" resultMap="getStudentMap">
SELECT a.*,b.id as cid,b.name as cname FROM `student` as a,teacher as b WHERE a.id =#{id} and a.tid = b.id;
</select>
<resultMap id="getStudentMap" type="student">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<result column="tid" property="tid"></result>
<association property="teacher" javaType="Teacher">
<id column="cid" property="id"></id>
<result column="cname" property="name"></result>
</association>
</resultMap>
</mapper>
我們在相應的mapper中新增方法介面便可以使用了。
1.2 一對多
案例:一個老師有多個學生
1、實體類
public class Teacher {
@TableId
private int id;
private String name;
@TableField(exist = false)
private List<Student> students;
}
2、編寫xml
同樣還是,我們先來個巢狀結果對映
巢狀結果:
// teacherMapper.xml
<select id="getStudent" parameterType="int" resultMap="getStudentMap">
SELECT a.*,b.id as cid,b.name as cname,b.tid
from teacher as a , student as b
where b.tid = a.id and a.id =#{id};
</select>
<resultMap id="getStudentMap" type="teacher">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<collection property="students" ofType="Student">
<id column="cid" property="id"></id>
<result column="cname" property="name"></result>
<result column="tid" property="tid" ></result>
</collection>
</resultMap>
巢狀查詢:
// teacherMapper.xml
<select id="getStudent2" parameterType="int" resultMap="getStudentMap2">
select * from teacher as a where a.id = #{id}
</select>
<resultMap id="getStudentMap2" type="teacher">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<collection property="students" column="id" ofType="Student" select="com.lll.mybatisplusdemo.mapper.StudentMapper.getStudentByTid">
</collection>
</resultMap>
// studentMapper.xml
<select id="getStudentByTid" parameterType="int" resultType="student">
select * from student as a where a.tid = #{id}
</select>
1.3 多對多
學生與課程是多對多的關係,與上面的一對多的操作方式是類似的
2、自定義sql如何做分頁
- mapper定義方法,方法傳入page引數
public interface UserMapper{
IPage<User> selectPageVo(Page<User> page);
}
- userMapper.xml檔案編寫一個普通的返回結果是list的方法,mybatis會自動幫你做分頁
<select id="selectPage" resultType="com.baomidou.cloud.entity.UserVo">
SELECT * FROM user
</select>
3、自定義sql如何做排序
結論:使用order by,記住要使用’$’,不能使用’#’
<select id="selectPage" resultType="com.baomidou.cloud.entity.UserVo">
SELECT * FROM user order by ${sortColumn} ${sortOrder}
</select>
4、自定義sql中的#{}和${}的區別
1、傳入的引數在SQL中顯示不同
#傳入的引數在SQL中顯示為字串(當成一個字串),會對自動傳入的資料加一個雙引號。
例:使用以下SQL
select id,name,age from student where id =#{id}
當我們傳遞的引數id為 “1” 時,上述 sql 的解析為:
select id,name,age from student where id ="1"
$傳入的引數在SqL中直接顯示為傳入的值
例:使用以下SQL
select id,name,age from student where id =${id}
當我們傳遞的引數id為 “1” 時,上述 sql 的解析為:
select id,name,age from student where id =1
2、#可以防止SQL注入的風險(語句的拼接);但$無法防止Sql注入。
3、$方式一般用於傳入資料庫物件,例如傳入表名。
4、大多數情況下還是經常使用#,一般能用#的就別用$
;但有些情況下必須使用$
,例:MyBatis排序時使用order by 動態引數時需要注意,用$而不是#。