mapper配置文件常用标签以及函数和存储过程的调用

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

一、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);
        }
    }

其他简单的增删改查就如上述所示进行调用。
注意:简单的查询所有语句,单个增加以及删除可以通过注释来进行较为简略的书写

### 调用 SQL Server 存储过程MyBatis调用 SQL Server 的存储过程可以通过配置 `mapper` 文件来实现。为了成功执行并处理来自数据库的结果,需遵循特定的语法结构。 #### 配置 Mapper XML 文件 创建或编辑相应的 `mapper.xml` 文件以定义如何调用存储过程: ```xml <mapper namespace="com.example.BigScreenJobMapper"> <!-- 定义参数类型 --> <parameterMap id="proHbjtFundParams" type="java.util.Map"> <parameter property="paramName" javaType="String" jdbcType="VARCHAR"/> <parameter property="result" mode="OUT" javaType="String" jdbcType="VARCHAR"/> </parameterMap> <!-- 使用 call 语句调用存储过程 --> <select id="callProHbjtFund" parameterMap="proHbjtFundParams" statementType="CALLABLE"> { CALL pro_hbjt_fund(#{paramName}, #{result,mode=OUT,jdbcType=VARCHAR}) } </select> </mapper> ``` 上述代码展示了如何设置输入输出参数以及指定要调用的具体存储过程名称[^1]。 #### Java 接口方法声明 对应于上面的 XML 映射器,在接口类中添加相应的方法签名: ```java public interface BigScreenJobMapper { void callProHbjtFund(Map<String, Object> params); } ``` 此方法接受一个包含所有必要参数的地图作为其唯一参数,并且不返回任何值因为结果是从 OUT 参数获得[^4]。 #### 获取返回值 当从应用程序层面调用了这个映射函数之后,就可以像下面这样访问由存储过程产生的输出数据了: ```java // 准备传给存储过程的数据 Map<String, Object> map = new HashMap<>(); map.put("paramName", "someValue"); // 执行存储过程 bigScreenJobMapper.callProHbjtFund(map); // 取得存储过程中设定的结果 String result = (String) map.get("result"); System.out.println(result); ``` 这段程序片段说明了怎样准备输入参数、触发存储过程及其后的结果检索操作。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值