mybatis多参数传递小结
来源:互联网 发布:鸟倦飞而知还什么意思 编辑:程序博客网 时间:2024/05/10 03:45
http://blog.sina.com.cn/s/blog_4c6e822d0102dv8l.html
ibatis3如何传递多个参数有两个方法:一种是使用Map,另一种是使用JavaBean。
- <!--
-
使用HashMap传递多个参数 -
parameterType 可以是别名或完全限定名 ,map- -
-- -
id="selectBlogByMap" parameterType="map" resultType="Blog"> -
SELECT t.ID, t.title, t.content -
FROM blog t -
WHERE = #{h_title} -
AND =#{h_content} -
-
使用JavaBean传递多个参数 --> -
id="selectBlogByBean" parameterType="Blog" resultType="Blog"> -
SELECT t.ID, t.title, t.content -
FROM blog t -
WHERE = #{title} -
AND =#{content} -
-
-
-
void testSelectByMap() { -
SqlSession session = sqlSessionFactory.openSession(); -
Map<String, Object> param= HashMap<String, Object>(); -
param.put( "oracle"); -
param.put( "使用序列!"); -
Blog blog = (Blog)session.selectOne( -
session.close(); -
System.out.println( title:" +blog.getTitle()); -
} -
-
-
void testSelectByBean() { -
SqlSession session = sqlSessionFactory.openSession(); -
Blog blog= Blog(); -
blog.setTitle( -
blog.setContent( -
Blog newBlog = (Blog)session.selectOne( -
session.close(); -
System.out.println( Blog +newBlog.getId());ID:" -
}
-
http://blog.csdn.net/liaoxiaohua1981/article/details/6862764
在MyBatis的select、insert、update、delete这些元素中都提到了parameterType这个属性。MyBatis现在可以使用的parameterType有基本数据类型和JAVA复杂数据类型
- 基本数据类型:包含int,String,Date等。基本数据类型作为传参,只能传入一个。通过#{参数名} 即可获取传入的值
- 复杂数据类型:包含JAVA实体类、Map。通过#{属性名}或#{map的KeyName}即可获取传入的值
- 基本数据类型参数示例:
根据班级ID查询教师列表
xml文件
- <select id="selectTeacher" parameterType="int" resultType="com.myapp.domain.Teacher">
- select * from Teacher where c_id=#{id}
- </select>
java代码
- List<Teacher> tList = teacherMapper.selectTeacher(2);
- for (Teacher entityTemp : tList) {
- System.out.println(entityTemp.toString());
- }
- JAVA实体类型参数示例:
- <select id="selectTeacher" parameterType="com.myapp.domain.Teacher" resultType="com.myapp.domain.Teacher">
- select * from Teacher where c_id=#{id}
- </select>
- java代码
- Teacher queryTeacher=new Teacher();
- queryTeacher.setId(2);
- List<Teacher> tList = teacherMapper.selectTeacher(queryTeacher);
- for (Teacher entityTemp : tList) {
- System.out.println(entityTemp.toString()); }
- Map参数示例:
- <select id="selectTeacher" parameterType="Map" resultType="com.myapp.domain.Teacher">
- select * from Teacher where c_id=#{id} and sex=#{sex}
- </select>
- java代码
- Map<String,String> map=new HasMap<String,String>();
- map.put("id","2");
- map.put("sex","男");
- List<Teacher> tList = teacherMapper.selectTeacher(map);
- for (Teacher entityTemp : tList) {
- System.out.println(entityTemp.toString()); }
另外MyBatis还提供了一个使用注解来参入多个参数的方式。这种方式需要在接口的参数上添加@Param注解
示例:
接口方法
- public List<Teacher> selectTeacher(@Param(value="id") String id,@Param(value="sex") String sex);
XML文件
- <select id="selectTeacher" resultType="com.myapp.domain.Teacher">
- select * from Teacher where c_id=#{id} and sex=#{sex}
- </select>
测试代码
- List<Teacher> tList = teacherMapper.selectTeacher("2","男");
- for (Teacher entityTemp : tList) {
- System.out.println(entityTemp.toString());
0 0
- mybatis多参数传递小结
- mybatis多参数传递
- MyBatis多参数传递
- 【Mybatis】多参数传递
- MyBatis多参数传递
- Mybatis多参数传递
- Mybatis传递多个参数
- Mybatis传递多个参数
- Mybatis传递多个参数
- MYbatis 多参数传递方式
- mybatis传递多个参数
- mybatis传递多个参数
- Mybatis传递多个参数
- Mybatis传递多个参数
- MyBatis传递多个参数,
- Mybatis传递多个参数
- Mybatis传递多个参数
- MyBatis传递多个参数
- 一半工作,一半wifi生活
- C++多重继承中的复制继承和共享继承
- VC编程读取文本数据
- 用第三方dylib,在XCode下编译Cocoa程序
- 随机的验证码
- mybatis多参数传递小结
- 互联网思维下的传统企业转型思考
- PHP5.4 + IIS + Win7的配置
- String,StringBuffer与StringBuilder的区别??
- android 圆角边框的实现及shape结构
- 第六周作业
- 宁波钓鱼去处
- 第一个OpenCV4Android
- 树的高度和深度的区别