MyBatis多参数传递之注解方式示例--转
原文地址:http://legend2011.blog.51cto.com/3018495/1015003
若映射器中的方法只有一个参数,则在对应的SQL语句中,可以采用#{参数名}的方式来引用此参数,以前的例子多属于此类。但这种方法却不适用于需要传递多个参数的情况,今天就来介绍如何使用注解传递多个参数(示例源码下载地址:http://down.51cto.com/data/537051)。
一、使用注解实现多参数传递
首先应引入“org.apache.ibatis.annotations.Param”,我们在接口TeacherMapper中引入,并增加一个教师分页查询的方法findTeacherByPage的声明。如下所示:
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
|
package com.abc.mapper; import com.abc.domain.Teacher; import org.springframework.stereotype.Component; import java.util.List; //使用@Param注解需要先引入Param import org.apache.ibatis.annotations.Param; //@Component指定映射器名称为myTeacherMapper //相关内容,可参考笔者博客: //http://legend2011.blog.51cto.com/3018495/980150 @Component ( "myTeacherMapper" ) public interface TeacherMapper { public Teacher getById( int id); //分页查询教师信息 public List<Teacher> findTeacherByPage( //使用@Param("sort")注解,即可在SQL语句中 //以“#{sort}”的方式引用此方法的sort参数值。 //当然也可以在@Param中使用其他名称, //如@Param("mysort") @Param ( "sort" ) String sort, //排序字段 //以下三个注解同理 @Param ( "dir" ) String dir, //排序方向 @Param ( "start" ) int start, //起始记录 @Param ( "limit" ) int limit //记录条数 ); } |
对应的映射文件TeacherMapper.xml的内容如下:
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
|
<? xmlversion = "1.0" encoding = "utf8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--与以前一样,namespace的值是对应的映射器接口的完整名称--> < mapper namespace = "com.abc.mapper.TeacherMapper" > <!--教师实体映射--> < resultMap id = "supervisorResultMap" type = "Teacher" > < id property = "id" /> < result property = "name" /> < result property = "gender" /> < result property = "researchArea" column = "research_area" /> < result property = "title" /> <!--collection元素映射教师的指导学生集合的属性。这里采用了 “命名空间名.select语句id”的形式来引用StudentMapper.xml中的 select语句getStudents。关于这种collection元素使用嵌套的 select语句的详情,请参考笔者博客: http://legend2011.blog.51cto.com/3018495/985907 --> < collection property = "supStudents" column = "id" ofType = "Student" select = "com.abc.mapper.StudentMapper.getStudents" /> </ resultMap > < select id = "findTeacherByPage" resultMap = "supervisorResultMap" > select * from teacher order by ${sort} ${dir} limit #{start},#{limit} </ select > </ mapper > |
运行主程序如下:
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package com.demo; import org.springframework.context.ApplicationContext; import com.abc.mapper.StudentMapper; import com.abc.mapper.TeacherMapper; import com.abc.domain.Teacher; import com.abc.domain.Student; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class CollectionDemo { private static ApplicationContext ctx; static { //在类路径下寻找resources/beans.xml文件 ctx = new ClassPathXmlApplicationContext( "resources/beans.xml" ); } public static void main(String[] args) { //从Spring容器中请求映射器 TeacherMapper mapper = (TeacherMapper)ctx.getBean( "myTeacherMapper" ); Teacher teacher = null ; //查询教师分页信息 List<Teacher> teachers = //以name字段升序排序,从第0条记录开始查询。 //查询2条记录 mapper.findTeacherByPage( "name" , "asc" , 0 , 2 ); if (teachers == null ) { System.out.println( "未找到相关教师信息。" ); } else { Object[] t = teachers.toArray(); System.out.println( "**********************************************" ); for ( int i = 0 ; i < t.length; i++) { teacher = (Teacher)t[i]; System.out.println( "教师姓名:" + " " + teacher.getName()); System.out.println( "教师职称:" + " " + teacher.getTitle()); System.out.println( "指导学生信息:" ); //遍历指导的学生 for (Student s : teacher.getSupStudents()) { System.out.println( s.getName() + " " + s.getGender() + " " + s.getGrade() + " " + s.getMajor()); } System.out.println( "**********************************************" ); } } } } |
运行结果如下:
二、可能会遇到的错误
1、关于order by
一般而言,我们会使用#{参数名}的形式来引用方法中的参数,但这种方式对于order by子句无效或报错。例如,当TeacherMapper.xml的select语句findTeacherByPage中的order by子句以#{sort}的形式引用方法中的sort参数的值时,是无效的(读者可自行验证);以#{dir}的形式引用方法中的dir参数的值时,会报MySQLSyntaxErrorException,如下图所示:
因此,在这里使用了${参数名}的形式引用了相应的参数值。
2、invalid XML character错误
这是一个诡异的错误。当在映射文件内的注释中,汉字“错”后紧跟中文的句号时即报此错误,如下图所示:
在Spring的配置文件beans.xml中,也是一样。类似地,汉字“错”后紧跟中文的逗号时也会报此错误。此时若在“错”字后面加一汉字,即不再报错;然而加“啊”字却仍然报错。读者可自行尝试,说不定还能找出其他错误的情形。
报出的异常都是org.xml.sax.SAXParseException(如上面错误图片中红框左边所示),这也许意味着它们都是使用同样的xml解析组件。而这种错误,会不会是此组件的bug?
MyBatis多参数传递之注解方式示例--转的更多相关文章
- Spring Boot入门(六):使用MyBatis访问MySql数据库(注解方式)
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 本篇博客我们讲解下在Spring Boot中使用MyBatis访问MySql数据库的简单用法. 1.前期 ...
- 从零开始学JAVA(09)-使用SpringMVC4 + Mybatis + MySql 例子(注解方式开发)
项目需要,继续学习springmvc,这里加入Mybatis对数据库的访问,并写下一个简单的例子便于以后学习,希望对看的人有帮助.上一篇被移出博客主页,这一篇努力排版整齐,更原创,希望不要再被移出主页 ...
- mybatis之注解方式实现
* 使用mybatis举例,使用注解方式实现* 不需要针对UserMapperI接口去编写具体的实现类代码,这个具体的实现类由MyBatis帮我们动态构建出来,我们只需要直接拿来使用即可.* 1.导入 ...
- MyBatis:参数传递 [转]
一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...
- springboot整合mybatis完整示例, mapper注解方式和xml配置文件方式实现(我们要优雅地编程)
一.注解方式 pom <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId& ...
- MyBatis从入门到精通(第3章):MyBatis注解方式的基本使用
MyBatis 注解方式就是将 SQL 语句直接写在DAO层的接口上. 在黑马录制的2018年双元视频课:\08 SSM整合案例[企业权限管理系统]\07.订单操作 有使用MyBatis注解进行多表 ...
- mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类
相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...
- SpringBoot入门教程(四)MyBatis generator 注解方式和xml方式
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 XML ...
- Hibernate search使用示例(基础小结-注解方式)
(对于项目环境配置,一直没怎么看过.这次经历里从基础环境搭建到hibernate search示例的完成) 1.首先创建project,选择了web project. 2.导入hibernate se ...
随机推荐
- atitit。企业组织与软件project的策略 战略 趋势 原则 attilax 大总结
atitit. 企业组织与软件project的策略 战略 趋势 原则 attilax 大总结 1. 战略规划,适当的过度设计 1 2. 跨平台化 1 3. 可扩展性高于一切 1 4. 界面html5化 ...
- C++基础学习教程(三)
承接上一讲. 2.7文件I/O 关于读写文件,C++中有一个专门的头文件<fstream>. 首先是读文件演示样例,例如以下: </pre><pre> /***** ...
- 【从零之六&完结】android口语对话系统(RavenClaw java版 含所有源代码)
! !! 更新:最新源码放到了github上,认为还不错点个星啊! 点击打开链接 做了一个半月最终完毕了,以下这个就是我參考Olympus/RavenClaw系统编写的对话管理系统.眼下实现了一个简单 ...
- OC 自己定义 setDateFormat 显示格式
-(NSString *)getStringFromDate:(NSDate *)aDate { NSDateFormatter *dateFormater=[[NSDateFormatter all ...
- DGA ngram kmeans+TSNE用于绘图
# -*- coding:utf-8 -*- import sys import re import numpy as np from sklearn.externals import joblib ...
- ubuntu12.04下NFS链接开发板并测试交叉编译的第一个应用
思路:配置网络->安装NFS->配置NFS->挂载NFS服务->Down文件执行.Okay lets go! 配置网络: 在配置网络之前,首先咱得搞定与开发板的交互工作,那么这 ...
- [codeforces 859 E] Desk Disorder 解题报告 (并查集+思维)
题目链接:http://codeforces.com/problemset/problem/859/E 题目大意: 有$n$个人,$2n$个座位. 给出这$n$个人初始的座位,和他们想坐的座位. 每个 ...
- Resolving Problems installing the Java JCE Unlimited Strength Jurisdiction Policy Files package--转
原文地址:https://www.ca.com/us/services-support/ca-support/ca-support-online/knowledge-base-articles.tec ...
- echarts中国地图
echarts中国地图效果图: =================== 需要引入echarts的js文件:(1.echarts.min.js:2.china.js) 下载地址: echarts.min ...
- 使用JS方法使页面滚动到指定元素+优化+API介绍(动画)
前言 当页面最上部有顶部菜单是,使用锚点跳转的方法很容易挡住想要呈现的内容(如下图技能两个字被挡住了一半),为避免出现这样的问题,故滚动到指定元素使用用JS的方法来实现. 目录 使用的API简介 初版 ...