mapper的常用标签,函数和存储过程的调用
一、Mapper配置文件常用标签
<select id=”Mapper接口中方法的名称”resultType=”返回自定义类型/resultMap=””>...</select>
当数据库字段名称,类型和java实体类的属性名称,类型不匹配时用resultMap
<insert id=”Mapper接口中方法的名称”>...</insert>
<delete id=”Mapper接口中方法的名称”>...</delete>
<update id=”Mapper接口中方法的名称”>...</update>
<foreachcollection=”array/list/map” item=”alias”
open=”开始符号”close=”结束符号”seperator=”分隔
符”>...</foreach>
<where></where>去掉第一个and,以where代替
<set></set> 去掉最后一个
<if test=”null != field”>...</if> 条件判断
二、简单SQL命令
注解Mapper:简单SQL命令
1)@Select(“SQL COMMAND”)
2)@Insert(“SQL COMMAND”)
3)@Update(“SQL COMMAND”)
4)@Delete(“SQL COMMAND”)
接口多个参数入参规则
1个以上,3个以内:以注解@Param(“paramName”)方式直接入参
3个以上:封装为Entity实体对象入参
调用函数,存储过程
1)select functionName(#{paramName},...)
2)必须在xxxMapper.xml中
<select id="findStuByPage" parameterType="map/EntityAlias" resultType="EntityAlias" statementType="CALLABLE">
{call proName(#{outParam,mode=OUT,jdbcType=java.sql.Types.XXX},...,
#{inParam},...) }
</select>
三、函数和存储过程的调用
sql语句
------函数--------
delimiter //
drop function if exists totalStu;
create function totalStu(pageSize int) returns int
begin
declare total int;
select ceil(count(1)/pagesize) from studentinfo into total;
return total;
end;
//
------存储过程---------
drop procedure if exists proPageStu;
//
create procedure proPageStu(out total int,in pageNo int,in pageSize int)
begin
declare _total int default 0;
declare _begin int default (pageNo-1)*pageSize;
select ceil(count(1)/pageSize) from studentinfo into _total;
set total=_total;
select * from studentinfo limit _begin,pageSize;
end;
//
delimiter ;
VStudentInfoMapper 接口
(函数)
@Select("select totalStu(#{pageSize})")
int findStuTotal(int pageSize);
(存储过程通过map实现)
List<VStudentInfo> findStuByPage(Map<String,Integer> map);
(创建实体类实现)
List<VStudentInfo> findStuByPage2(PageParam param);
vStudentInfoMapper.xml
<select id="findStuByPage" parameterType="map" resultType="VStudentInfo" statementType="CALLABLE">
{ call proPageStu(#{total,mode=OUT,jdbcType=INTEGER},#{pageNo},#{pageSize}) }
</select>
<select id="findStuByPage2" parameterType="PageParam" resultType="VStudentInfo" statementType="CALLABLE">
{ call proPageStu(#{total,mode=OUT,jdbcType=INTEGER},#{pageNo},#{pageSize}) }
</select>
</mapper>
AppTest
@Test
public void testfindStuTotal(){
int total = mapper.findStuTotal(10);
System.out.println(total);
}
@Test
public void testfindStuByPage(){
Map<String,Integer> map = new HashMap<>(3);
map.put("pageNo",2);
map.put("pageSize",8);
List<VStudentInfo> list = mapper.findStuByPage(map);
System.out.println(map.get("total"));
for (VStudentInfo vstu : list) {
System.out.println(vstu);
}
}
@Test
public void testfindStuByPage2(){
PageParam param = new PageParam(5, 10);
List<VStudentInfo> list = mapper.findStuByPage2(param);
System.out.println(param.getTotal());
for (VStudentInfo vstu : list) {
System.out.println(vstu);
}
}
其他简单的增删改查就如上述所示进行调用。
注意:简单的查询所有语句,单个增加以及删除可以通过注释来进行较为简略的书写

本文介绍了Mybatis中Mapper配置文件的常用标签,包括简单SQL命令的使用,以及如何调用函数和存储过程。通过实例展示了VStudentInfoMapper接口、vStudentInfoMapper.xml配置及AppTest的测试过程,详细阐述了Mybatis中的增删改查操作。
1496

被折叠的 条评论
为什么被折叠?



