mybatis中怎麼通過外來鍵查詢_Mybatis【8】 Mybatis返回List或者Map以及模糊查詢怎麼搞?...
技術標籤:mybatis中怎麼通過外來鍵查詢
程式碼直接放在Github倉庫【https://github.com/Damaer/Mybatis-Learning 】,可直接執行,就不佔篇幅了。
1.返回List(查詢所有學生)
2.返回Map(查詢所有學生,key是名字,value是學生物件)
3.模糊查詢
sqlSession
例項的工具類如下:
importorg.apache.ibatis.io.Resources;
importorg.apache.ibatis.session.SqlSession;
importorg.apache.ibatis.session.SqlSessionFactory;
importorg.apache.ibatis.session.SqlSessionFactoryBuilder;
importjava.io.IOException;
importjava.io.InputStream;
publicclassMyBatisUtils{
staticprivateSqlSessionFactorysqlSessionFactory;
staticpublicSqlSessiongetSqlSession(){
InputStreamis;
try{
is=Resources.getResourceAsStream("mybatis.xml");
if(sqlSessionFactory==null){
sqlSessionFactory=newSqlSessionFactoryBuilder().build(is);
}
returnsqlSessionFactory.openSession();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnnull;
}
}
1.返回List(查詢所有學生)
定義介面:
//返回所有學生的資訊List
publicListselectAllStudents();
使用SqlSession.selectList()這個方法對sql進行呼叫:
publicListselectAllStudents(){
Liststudents;try{
sqlSession=MyBatisUtils.getSqlSession();
students=sqlSession.selectList("selectAllStudents");//查詢不用修改,所以不用提交事務
}finally{if(sqlSession!=null){
sqlSession.close();
}
}returnstudents;
}
sql
語句如下,返回型別是Student
,這裡寫的是別名,這是因為定義過別名,否則需要寫全路徑名:
"selectAllStudents"resultType="Student">
selectid,name,age,scorefromstudent
2.返回Map(查詢所有學生,key是名字,value是學生物件)
定義介面:
//返回所有學生的資訊Map
publicMapselectAllStudentsMap();
介面實現類,使用selectMap(),裡面有兩個引數,一個是sql的id,一個是需要當成key的欄位,注意,如果這個key在資料表裡面有重複的,那麼後面查出來的value會覆蓋掉前面的value:
publicMapselectAllStudentsMap(){
Mapmap=newHashMap();
/**
*可以寫成Mapmap=newHashMap();
*/
try{
sqlSession=MyBatisUtils.getSqlSession();
map=sqlSession.selectMap("selectAllStudents","name");
//查詢不用修改,所以不用提交事務
}finally{if(sqlSession!=null){
sqlSession.close();
}
}returnmap;
}
sql語句:用的同樣是返回List的sql語句,其實這個map的處理是map=sqlSession.selectMap("selectAllStudents", "name");
這句話幫我們處理的。
<selectid="selectAllStudents"resultType="Student">
selectid,name,age,scorefromstudent
select>
3.模糊查詢
我們需要查詢名字的時候一般是模糊查詢。那麼使用下面的sql即可:
<selectid="selectStudentsByName"resultType="Student">
selectid,name,age,scorefromstudentwherenamelike'%${value}%'
select>
值得注意的是關於佔位符的問題,如果只是一個int型別傳進來,如果使用#,我們不需要和傳入的名字一樣,比如#{}裡面寫xxx都可以:
"deleteStudentById">
deletefromstudentwhereid=#{xxx}
當傳入的是物件,那麼我們就不能隨便寫了,因為隨便寫就不知道用哪一個屬性了。
"updateStudent">
updatestudentsetname=#{name},age=#{age},score=#{score}whereid=#{id}
如果我們使用,那麼當傳進來一個型別的時候我們需要使用{value},裡面寫id都是不可以的,必須寫value
"selectStudentById"resultType="Student">
select*fromstudentwhereid=${value}
模糊查詢的時候,以下方式是拼接的模式,容易被sql
注入,所以我們一般不推薦:
<selectid="selectStudentsByName"resultType="Student">
selectid,name,age,scorefromstudentwherenamelike'%${value}%'
select>
注意不可以寫成 '%#{name}%' ,拼接的必須使用 $ 符號。可以使用函式進行連線:
"selectStudentsByName"resultType="Student">
selectid,name,age,scorefromstudentwherenamelikeconcat('%',#{xxx},'%')
當然也可以不使用函式,注意 '%' 與 #{name} 之間是有空格的,要不會報錯,這種是我們比較推薦的,也就是動態引數,可以極大減少sql
注入的風險:
"selectStudentsByName"resultType="Student">
selectid,name,age,scorefromstudentwherenamelike'%'#{name}'%'
【作者簡介】:
秦懷,公眾號【秦懷雜貨店】作者,技術之路不在一時,山高水長,縱使緩慢,馳而不息。這個世界希望一切都很快,更快,但是我希望自己能走好每一步,寫好每一篇文章,期待和你們一起交流。
此文章僅代表自己(本菜鳥)學習積累記錄,或者學習筆記,如有侵權,請聯絡作者核實刪除。人無完人,文章也一樣,文筆稚嫩,在下不才,勿噴,如果有錯誤之處,還望指出,感激不盡~