mybatis中注解映射SQL示例代碼
前言
本文主要給大家介紹了關(guān)于mybatis注解映射SQL的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細的介紹:
結(jié)果集分頁
有時我們需要處理海量數(shù)據(jù),由于數(shù)據(jù)量太大,所以不能一次取出所有的數(shù)據(jù),這時我們就需要使用分頁功能。mybatis通過RowBounds對象提供對分頁的支持,如下所示:
<select id="findAllStudents" resultMap="StudentResult"> select * from studdents </select> int offset=0;//開始位置 int limit=25;//取出的數(shù)據(jù)條數(shù) RowBounds rowBounds=new RowBounds(offset,limit); List<Student> list=studentMapper.findAllStudent(rowBounds);
結(jié)果處理器
有時我們需要對查詢結(jié)果做一些特殊的處理,這個時候就需要結(jié)果處理器,舉例如下,我們通過sql查詢學(xué)生的stud_id和name,并期望返回一個map,其中key是stud_id,value是name.
新建一個接口:
public interface ResultHandler
{
void handleResult(ResultContext context);
}
主要處理流程:
Map<Integer , String> map=new HashMap<Integer,String>();
SqlSession sqlSession=MyBatisUtil.openSession();
sqlSession.select("com.mybatis3.mappers.StudentMapper.findAllStudents",new ResultHandler(){
public void handlerResult(ResultContext context)
{
Student student=(Student)context.getResultObject();
map.put(student.getStudId(),student.getName());
}
})
緩存
緩存對于很多應(yīng)用來說都是很重要的,因為它能提高系統(tǒng)的性能。mybatis內(nèi)建了緩存支持,默認情況下,一級緩存是打開的,即如果你使用相同的sqlSession接口調(diào)用相同的select查詢,查詢結(jié)果從緩存中取得而不是去查詢數(shù)據(jù)庫。
也可以通過<cache>標(biāo)簽配置二級緩存。當(dāng)配置了二級緩存后,也就意味著所有的查詢結(jié)果都會被緩存,insert,update,delete語句會更新緩存,cache的緩存管理算法是LRU。除了內(nèi)建的緩存之外,mybatis還整合了第三方緩存框架例如Ehcache等。
注解@Insert @Update @Select @ Delete
舉例說明注解的用法:
public interface StudentMapper
{
@Insert("insert into student (stud_id, name, email, addr_id, phone)values(#{studId},#{name},#{email},#{address.addrId},#{phone})")
int insertStudent(Student student);
}
public interface StudentMapper
{
@Insert("insert into student (name,email,addr_id,phone)values(#{name},#{email},#{address.addrId},#{phone})")
@Options(useGeneratedKeys=true,keyProperty="studId")
int insertStudent(Student student);
}
public interface StudentMapper
{
@Insert("insert into student (name,email,addr_id,phone)values(#{name},#{email},#{address.addrId},#{phone})")
@SelectKey(statement="select stud_id_seq.nextval from dual",keyProperty="studId",resultType=int.calss,before=true)
int insertStudent(Student student);
}
@Update("update students set name=#{name},email=#{email}")
int updateStudent(Student student);
@Delete("delete form students where stud_id=#{studId}")
int deleteStudent(int studId)
@Select("select name,email,phone from students where stud_id=#{studId}")
Student findStudentById(Integer studId);
結(jié)果注解
@Select("select name,email,phone from students where stud_id=#{studId}")
@Results({
@Result(id=true,column="stud_id",property="studId"),
@Result(column="name",property="name"),
@Result(column="email",property="email"),
@Result(column="phone",property="phone")
})
Student findStudentById(Integer studId);
結(jié)果注解有一個缺點,就是在一個查詢方法前面都要寫一遍,不能重用。解決這個問題方案是:
定義一份結(jié)果映射文件如下所示:
<mapper namespace="com.mybatis3.mappers.StudentMapper">
<resultMap type="Student" id="StudentResult">
.......
</resultMap>
@Select("select name,email,phone from students where stud_id=#{studId}")
@ResultMap("com.mybatis3.mappers.StudentMapper.StudentResult")
Student findStudentById(Integer studId);
動態(tài)Sql的注解
對于動態(tài)sql,mybatis提供了不同的注解,@InsertProvider @UpdateProvider @DeleteProvider @SelectProvider
用法如下所示:
首先創(chuàng)建一個provider類:
public class SqlProvider
{
public String findTutorById(int tutorId)
{
return "select tutorId,name,email from tutors where tutorId="+tutorId;
}
}
使用provider類:
@SelectProvider(type=SqlProvider.class,method="findTutorById") Tutor findTutorById(int tutorId);
但是使用字符串連接創(chuàng)建sql語句容易出現(xiàn)問題,所以mybatis提供了一個SQL工具,簡化了構(gòu)建動態(tài)Sql的方式;
如下所示:
public class SqlProvider
{
public String findTutorById(int tutorId)
{
return new SQL(){{
SELECT("tutorid,name,email")
FROM("tutors")
WHERE("tutorid="+tutorId)
}}.toString();
}
}
或者
public class SqlProvider
{
public String findTutorById()
{
return new SQL(){{
SELECT("tutorid,name,email")
FROM("tutors")
WHERE("tutorid=#{tutorId}")
}}.toString();
}
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Jmeter命令行執(zhí)行腳本如何設(shè)置動態(tài)參數(shù)
這篇文章主要介紹了Jmeter命令行執(zhí)行腳本如何設(shè)置動態(tài)參數(shù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08

