mybatis存储过程返回list

发布于:2025-02-19 ⋅ 阅读:(19) ⋅ 点赞:(0)

在MyBatis中调用存储过程并返回列表(List)通常涉及以下几个步骤:

  1. 定义存储过程:首先,在数据库中定义存储过程,并确保它返回结果集。
  2. 配置MyBatis映射文件:在MyBatis的映射文件中配置调用存储过程的SQL语句和返回类型。
  3. 编写Mapper接口:定义Mapper接口方法,该方法将调用存储过程并返回List。
  4. 调用Mapper方法:在Service层或Controller层调用Mapper接口方法。

以下是一个详细的示例:

1. 定义存储过程

假设我们有一个名为getUserList的存储过程,它返回用户表中的所有用户信息。

sql复制代码



DELIMITER //  
  
CREATE PROCEDURE getUserList()  
BEGIN  
    SELECT id, name, email FROM users;  
END //  
  
DELIMITER ;

2. 配置MyBatis映射文件

在MyBatis的映射文件(例如UserMapper.xml)中,配置调用存储过程的SQL语句和返回类型。

xml复制代码



<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper  
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  
<mapper namespace="com.example.mapper.UserMapper">  
  
    <!-- 调用存储过程 -->  
    <select id="getUserList" resultType="com.example.domain.User">  
        {CALL getUserList()}  
    </select>  
  
</mapper>

3. 编写Mapper接口

在Mapper接口(例如UserMapper.java)中定义方法,该方法将调用存储过程并返回List。

java复制代码



package com.example.mapper;  
  
import com.example.domain.User;  
import org.apache.ibatis.annotations.Select;  
  
import java.util.List;  
  
public interface UserMapper {  
  
    @Select("CALL getUserList()")  // 也可以使用XML配置,这里只是为了展示注解方式  
    List<User> getUserList();  
}

4. 调用Mapper方法

在Service层或Controller层调用Mapper接口方法。

java复制代码



package com.example.service;  
  
import com.example.domain.User;  
import com.example.mapper.UserMapper;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  
  
import java.util.List;  
  
@Service  
public class UserService {  
  
    @Autowired  
    private UserMapper userMapper;  
  
    public List<User> getAllUsers() {  
        return userMapper.getUserList();  
    }  
}

5. 配置MyBatis和Spring(可选)

如果你使用的是Spring框架,确保你已经正确配置了MyBatis和Spring的集成。这通常包括配置数据源、SqlSessionFactoryBean和Mapper扫描等。

xml复制代码



<!-- Spring配置文件示例 -->  
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"  
       xsi:schemaLocation="  
           http://www.springframework.org/schema/beans   
           http://www.springframework.org/schema/beans/spring-beans.xsd  
           http://www.springframework.org/schema/context   
           http://www.springframework.org/schema/context/spring-context.xsd  
           http://mybatis.org/schema/mybatis-spring   
           http://mybatis.org/schema/mybatis-spring.xsd">  
  
    <!-- 配置数据源 -->  
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">  
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>  
        <property name="url" value="jdbc:mysql://localhost:3306/yourdatabase"/>  
        <property name="username" value="yourusername"/>  
        <property name="password" value="yourpassword"/>  
    </bean>  
  
    <!-- 配置SqlSessionFactory -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource"/>  
        <property name="mapperLocations" value="classpath:mappers/*.xml"/>  
    </bean>  
  
    <!-- 扫描Mapper接口 -->  
    <mybatis-spring:scan base-package="com.example.mapper"/>  
  
</beans>

注意事项

  1. 存储过程权限:确保数据库用户有权限执行存储过程。
  2. MyBatis版本:确保你使用的MyBatis版本支持存储过程的调用。
  3. 返回类型:确保resultType与你的Java实体类匹配。

通过以上步骤,你应该能够成功地在MyBatis中调用存储过程并返回List。


网站公告

今日签到

点亮在社区的每一天
去签到