栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis 接口的单/多条件查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis 接口的单/多条件查询

规则

在MyBatis进行操作:

  • 如果操作方法只有一个简单类型或者字符串类型的参数,在Mapper配置中可以直接通过#{key}直接获取
  • 如果操作方法有一个对象类型的参数,在Mapper配置中可以直接通过#{attrName}获取对象的指定属性值(attrName必须是参数对象的属性)
  • 如果操作方法有一个Map类型的参数,在Mapper配置中可以直接通过#{key}获取key对应的value
  • 如果操作方法有多个参数:
    • 在mapper里使用特殊参数(arg0,arg1,arg2…)或(param1,param2,param3…),需要按顺序
    • 使用@Param(“start”)定义别名,在mapper中使用别名获取指定参数
DAO文件
Student queryStudentById(String stuNum);
int insertStudent(Student student);
List queryStudentListByPage(HashMap map);
List queryStudentListByPage(int start, int length);
List queryStudentListByPage(@Param("sta") int start, @Param("len") int length);
mapper文件

mapper.xml 标签使用



    select 
    from students
    where stu_num=#{stuNum}



    insert into students(stu_num, stu_name, stu_sex, stu_age)
    values(#{stuNum}, #{stuName}, #{stuSex}, #{stuAge})



	
	
    select 
    from students
    limit #{sta}, #{len}

测试类
// Map类型的传参
public void queryStudentListByMap() {
	StudentDao studentDao = MyBatisUtil.getMapper(StudentDao.class);
	HashMap map = new HashMap();
	map.put("start", 0);
	map.put("length", 2);
	List students = studentDao.queryStudentListByPage(map);
}
// 多个参数
public void queryStudentListByPage() {
	StudentDao studentDao = MyBatisUtil.getMapper(StudentDao.class);
	List students = studentDao.queryStudentListByPage(1, 2);
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/978742.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号