Mybatis 多参数传递、parameterType=“java.util.List“自动查找实体类参数 - 具体方法

1、多参数传递

@Param(传递的形参参数名1) 实参参数1, ... @Param(传递的形参参数名n-1) 实参参数n-1, @Param(传递的形参参数名n) 实参参数n

参考链接

1. 五、深入理解Mybatis中的参数parameterType (传递一个简单参数,传递多个参数:@Param、使用自定义对象、按位置、使用Map)

2、 parameterType=“java.util.List” 自动查找实体类参数

mapper.xml 文件内的参数格式:parameterType="java.util.List"

可以传递任意的实体类数组 List<Object>

具体的例子如下:

TestMapper.xml 批量更新操作演示:更新表是 test_table

<mapper namespace="com.test.mapper.TestMapper">

    <update id="testUpdate" parameterType="java.util.List">
        <choose>
            <when test="list != null and list.size() > 0">
                <foreach collection="list" item="item" separator=";">
                    UPDATE test_table 
                    <set>
                    name=#{item.testname}
                    </set>
                    WHERE id=#{item.id}
                </foreach>
            </when>
            <otherwise>
                select ""
            </otherwise>
        </choose>
    </update>
</mapper>

Java 调用演示:

1. mytestEntity 实体类有 id 和 testname 字段

2. TestMapper.java 设计
import org.apache.ibatis.annotations.Mapper;
import com.test.entity.mytestEntity;
import java.util.List;

@Mapper
public interface TestMapper{
	// 查询数据
    List<mytestEntity> getMyTestList();
	// 批量更新数据
	Integer testUpdate(List<mytestEntity> testList);
}

4. testUpdate 接口设计:
testUpdate(List<mytestEntity> testList);

5. getMyTestList是获取一个实体类为mytestEntity的 List 数组数据

############################################# 具体实际演示如下

import com.test.entity.mytestEntity;
import com.test.mapper.TestMapper;
import java.util.List;

public class Mytest{

    @Autowired
    private TestMapper testMapper;
    
    public void test() {

	// 查询数据
	List<mytestEntity> testEntity = testMapper.getMyTestList();
	// 批量更新数据
	Integer result = testMapper.testUpdate(testEntity);
	}
}


参考链接

1. @Param注解的用法解析与parameterType

2. MyBatis学习之路——获取参数值和各种查询功能(查询实体类、List集合、Map集合)

3. Mybatis(三)各种查询功能

4. Mybatis insert报错 Cause: java.sql.SQLException: SQL String cannot be empty

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用 MyBatis 操作 Oracle 数据库的 Java 代码示例,包含了使用 `foreach` 标签实现批量插入的功能。 首先,需要在 `mybatis-config.xml` 文件中配置数据库连接信息和扫描映射器的包等信息,示例配置如下: ```xml <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@//localhost:1521/orcl"/> <property name="username" value="your_username"/> <property name="password" value="your_password"/> </dataSource> </environment> </environments> <mappers> <mapper class="com.example.mapper.YourMapper"/> </mappers> </configuration> ``` 其中,`com.example.mapper.YourMapper` 是你自己定义的映射器接口。 接下来,定义实体类 `YourEntity` 和映射器接口 `YourMapper`,示例代码如下: ```java public class YourEntity { private Long orderNum; private Long skuNum; // 省略 getter 和 setter 方法 } public interface YourMapper { void saveSyncSettleInfo(List<YourEntity> list); } ``` 其中,`YourMapper` 中的 `saveSyncSettleInfo` 方法使用了 `List` 类型的参数,并使用 MyBatis 中的 `foreach` 标签实现了批量插入的功能。 最后,编写 mapper.xml 文件,示例代码如下: ```xml <mapper namespace="com.example.mapper.YourMapper"> <insert id="saveSyncSettleInfo" parameterType="java.util.List"> insert into stludr.SYNCSETTLEINFO(orderNum, skuNum) select cd.* from ( <foreach collection="list" item="item" index="index" open="(" close=")" separator="union all"> select #{item.orderNum}, #{item.skuNum} from DUAL </foreach> ) cd </insert> </mapper> ``` 在 mapper.xml 文件中,定义了一个名为 `saveSyncSettleInfo` 的插入语句,并使用了 `foreach` 标签实现了批量插入的功能。 最后,编写测试代码,示例代码如下: ```java public class YourTest { public static void main(String[] args) { SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml")); SqlSession sqlSession = sqlSessionFactory.openSession(); try { YourMapper mapper = sqlSession.getMapper(YourMapper.class); List<YourEntity> list = new ArrayList<>(); YourEntity entity1 = new YourEntity(); entity1.setOrderNum(1L); entity1.setSkuNum(101L); list.add(entity1); YourEntity entity2 = new YourEntity(); entity2.setOrderNum(2L); entity2.setSkuNum(102L); list.add(entity2); mapper.saveSyncSettleInfo(list); sqlSession.commit(); } finally { sqlSession.close(); } } } ``` 在测试代码中,首先通过 `SqlSessionFactoryBuilder` 类加载 `mybatis-config.xml` 配置文件,然后通过 `SqlSessionFactory` 类创建 `SqlSession` 对象。接着,使用 `SqlSession` 对象获取你自己定义的映射器接口 `YourMapper` 的实例,然后创建一些实体对象并添加到列表中。最后,调用 `YourMapper` 中的 `saveSyncSettleInfo` 方法进行批量插入,并提交事务。注意,在 `SqlSession` 对象使用完成后需要调用 `close` 方法关闭连接。 希望这个示例能够帮到你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值