温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mybatis中的SQL怎么利用注解进行映射

发布时间:2020-12-02 17:05:44 来源:亿速云 阅读:335 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关mybatis中的SQL怎么利用注解进行映射,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

结果集分页

有时我们需要处理海量数据,由于数据量太大,所以不能一次取出所有的数据,这时我们就需要使用分页功能。mybatis通过RowBounds对象提供对分页的支持,如下所示:

<select id="findAllStudents" resultMap="StudentResult">
 select * from studdents
</select>
int offset=0;//开始位置
int limit=25;//取出的数据条数
RowBounds rowBounds=new RowBounds(offset,limit);
List<Student> list=studentMapper.findAllStudent(rowBounds);

结果处理器

有时我们需要对查询结果做一些特殊的处理,这个时候就需要结果处理器,举例如下,我们通过sql查询学生的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());
 }
})

缓存

缓存对于很多应用来说都是很重要的,因为它能提高系统的性能。mybatis内建了缓存支持,默认情况下,一级缓存是打开的,即如果你使用相同的sqlSession接口调用相同的select查询,查询结果从缓存中取得而不是去查询数据库。

也可以通过<cache>标签配置二级缓存。当配置了二级缓存后,也就意味着所有的查询结果都会被缓存,insert,update,delete语句会更新缓存,cache的缓存管理算法是LRU。除了内建的缓存之外,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);

结果注解

@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);

结果注解有一个缺点,就是在一个查询方法前面都要写一遍,不能重用。解决这个问题方案是:

定义一份结果映射文件如下所示:

<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);

动态Sql的注解

对于动态sql,mybatis提供了不同的注解,@InsertProvider @UpdateProvider @DeleteProvider @SelectProvider
用法如下所示:

首先创建一个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); 

但是使用字符串连接创建sql语句容易出现问题,所以mybatis提供了一个SQL工具,简化了构建动态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();
  }
 } 

看完上述内容,你们对mybatis中的SQL怎么利用注解进行映射有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI