Mybatis - 2 #{}中可以放的值

#{}中可以放的值

1.1.对象属性
1.2.任意值
1.3.map的key值(key对应的value类型是基本数据类型或者是字符串)
1.4.如果map的key对应的value是一个对象,则放对象的属性
1.5.索引号

public interface StudentMapper {
	
	List<Student> selectByCondition(String name,int age);
	List<Student> selectByCondition1(Map<String, Object> stuMap);
}
<?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.bjsxt.mapper.StudentMapper">
	<select id="selectByCondition1" resultType="Student">
		select id,name,age,score from student 
		where name like '%' #{nameCon} '%'
		and age >#{ageCon}
		and score >#{stu.score}
	</select>
	<!-- #{}中可以放的值
		1.对象属性
		2.任意值
		3.map的key值(key对应的value类型是基本数据类型或者是字符串)
		4.如果map的key对应的value是一个对象,则放对象的属性
		5.索引号
	 -->
</mapper>
@Test
	public void testSeletcByName1(){
		Map<String, Object> stuMap = new HashMap<String,Object>();
		stuMap.put("nameCon", "三");
		stuMap.put("ageCon", 25);
		
		List<Student> list=mapper.selectByCondition1(stuMap);
		for (Student student : list) {
			System.out.println(student);
		}
	}
	@Test
	public void testSeletcByName2(){
		Student stu = new Student("赵六",26,97.4);
		Map<String, Object> stuMap = new HashMap<String,Object>();
		stuMap.put("nameCon", "三");
		stuMap.put("ageCon", 25);
		stuMap.put("stu",stu);
		
		List<Student> list=mapper.selectByCondition1(stuMap);
		for (Student student : list) {
			System.out.println(student);
		}
	}

 

转载于:https://my.oschina.net/nan99/blog/1517923

Java项目,尤其是使用MyBatis Plus处理大量数据插入的情况,为了保证数据的一致性和完整性,通常会采用批量插入并设置一个数据库事务。这里是一个通用的写法: 1. 首先,你需要创建一个批次操作的对象,如`BatchInsertHelper`,这是MyBatis Plus提供的工具类,用于批量插入。 ```java import com.baomidou.mybatisplus.core.toolkit.BatchInsertHelper; ``` 2. 创建一个实体集合,将20万条数据实例化成List。 ```java List<MyEntity> dataList = new ArrayList<>(); for (int i = 0; i < 200000; i++) { MyEntity entity = new MyEntity(); // 设置实体属性... dataList.add(entity); } ``` 3. 使用`BatchInsertHelper`开始事务,并设置批量插入的最大数量。默认情况下,MyBatis Plus的`insert()`方法已经支持批量插入,但如果性能需求更高,可以设置更大的批量(注意,MySQL有最大行数限制,默认5000)。 ```java BatchInsertHelper batchInsertHelper = new BatchInsertHelper(MyMapper.class); batchInsertHelper.insert(dataList, 5000); // 批量插入大小,根据实际调整 // 开始事务 try (Transaction transaction = sqlSession.startTransaction()) { // 执行批量插入 batchInsertHelper.execute(sqlSession); // 提交事务 transaction.commit(); } catch (Exception e) { // 如果发生异常,则回滚事务 transaction.rollback(); logger.error("Insert data failed", e); } ``` 4. 请注意,如果内存不足以一次性加载所有的数据,可以考虑分批处理,但这样可能会增加网络IO次数。另外,频繁的事务提交可能会对数据库性能造成影响,所以最好只在数据插入完成后提交。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值