前言
本文主要给大家介绍了关于mybatis注解映射SQL的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍:
结果集分页
有时我们需要处理海量数据,由于数据量太大,所以不能一次取出所有的数据,这时我们就需要使用分页功能。mybatis通过RowBounds对象提供对分页的支持,如下所示:
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
						 | <select id="findAllStudents"resultMap="StudentResult">select * from studdents</select>intoffset=0;//开始位置intlimit=25;//取出的数据条数RowBounds rowBounds=newRowBounds(offset,limit);List<Student> list=studentMapper.findAllStudent(rowBounds); | 
结果处理器
有时我们需要对查询结果做一些特殊的处理,这个时候就需要结果处理器,举例如下,我们通过sql查询学生的stud_id和name,并期望返回一个map,其中key是stud_id,value是name.
新建一个接口:
| 
								1
 
								2
 
								3
 
								4
						 | publicinterfaceResultHandler{voidhandleResult(ResultContext context);} | 
主要处理流程:
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
						 | Map<Integer , String> map=newHashMap<Integer,String>();SqlSession sqlSession=MyBatisUtil.openSession();sqlSession.select("com.mybatis3.mappers.StudentMapper.findAllStudents",newResultHandler(){publicvoidhandlerResult(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
举例说明注解的用法:
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
 
								26
						 | publicinterfaceStudentMapper{@Insert("insert into student (stud_id, name, email, addr_id, phone)values(#{studId},#{name},#{email},#{address.addrId},#{phone})")intinsertStudent(Student student);}publicinterfaceStudentMapper{@Insert("insert into student (name,email,addr_id,phone)values(#{name},#{email},#{address.addrId},#{phone})")@Options(useGeneratedKeys=true,keyProperty="studId")intinsertStudent(Student student);}publicinterfaceStudentMapper{@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)intinsertStudent(Student student);}@Update("update students set name=#{name},email=#{email}")intupdateStudent(Student student);@Delete("delete form students where stud_id=#{studId}")intdeleteStudent(intstudId)@Select("select name,email,phone from students where stud_id=#{studId}")Student findStudentById(Integer studId); | 
结果注解
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
						 | @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); | 
结果注解有一个缺点,就是在一个查询方法前面都要写一遍,不能重用。解决这个问题方案是:
定义一份结果映射文件如下所示:
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
						 | <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类:
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
						 | publicclassSqlProvider{publicString findTutorById(inttutorId){return"select tutorId,name,email from tutors where tutorId="+tutorId;}} | 
使用provider类:
| 
								1
 
								2
						 | @SelectProvider(type=SqlProvider.class,method="findTutorById")Tutor findTutorById(inttutorId); | 
但是使用字符串连接创建sql语句容易出现问题,所以mybatis提供了一个SQL工具,简化了构建动态Sql的方式;
如下所示:
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
						 | publicclassSqlProvider{publicString findTutorById(inttutorId){returnnewSQL(){{SELECT("tutorid,name,email")FROM("tutors")WHERE("tutorid="+tutorId)}}.toString();}} | 
或者
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
						 | publicclassSqlProvider{publicString findTutorById(){returnnewSQL(){{SELECT("tutorid,name,email")FROM("tutors")WHERE("tutorid=#{tutorId}")}}.toString();}} | 
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。
原文链接:https://segmentfault.com/a/1190000010784360
相关文章
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
- 
            2025-05-25 22
- 
            2025-06-04 30
- 
            2025-05-27 37
- 
            2025-05-29 61
- 
            2025-06-04 69
 
        
 
    		 
            	 
															 
         
         
        
 
                        