Mybatis使用(4)

本文详细介绍了MyBatis的批量插入操作,ExecutorType的作用,以及如何处理级联查询。针对关联查询,讨论了association标签在嵌套结果和嵌套查询中的使用,包括property、javaType、resultMap等属性的配置。同时,文章还探讨了一对多关系的处理,discriminator鉴别的应用场景和配置,并分析了多对多关系的实现。此外,文章深入讲解了MyBatis的一级缓存和二级缓存机制,包括缓存配置、缓存刷新策略和使用场景。最后,提到了MyBatis-Spring的整合及其最佳实践。
摘要由CSDN通过智能技术生成

1.批量插入数据:

1)拼接sql语句:

@Test
	// Foreach用于批量插入
	public void testForeach4Insert() {
   
		// 2.获取sqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 3.获取对应mapper
		TUserMapper mapper = sqlSession.getMapper(TUserMapper.class);
		
		TUser user1 = new TUser();
		user1.setUserName("king");
		user1.setRealName("李小京");
		user1.setEmail("li@qq.com");
		user1.setMobile("18754548787");
		user1.setNote("king's note");
		user1.setSex((byte)1);
		TUser user2 = new TUser();
		user2.setUserName("deer");
		user2.setRealName("陈大林");
		user2.setEmail("chen@qq.com");
		user2.setMobile("18723138787");
		user2.setNote("deer's note");
		user2.setSex((byte)1);
		
		
		int i = mapper.insertForeach4Batch(Arrays.asList(user1,user2));
		System.out.println(i);
	}
int insertForeach4Batch(List<TUser> users);

自动生成主键id

<insert id="insertForeach4Batch" useGeneratedKeys="true" keyProperty="id">
		insert into t_user (user_name, real_name,
		sex, mobile,email,note,
		position_id)
		values
		<foreach collection="list" separator="," item="user">
			(
			#{
   user.userName,jdbcType=VARCHAR},
			#{
   user.realName,jdbcType=VARCHAR},
			#{
   user.sex,jdbcType=TINYINT},
			#{
   user.mobile,jdbcType=VARCHAR},
			#{
   user.email,jdbcType=VARCHAR},
			#{
   user.note,jdbcType=VARCHAR},
			#{
   user.position.id,jdbcType=INTEGER}
			)
		</foreach>
	</insert>

2)ExecutorType

@Test
	// 批量更新
	public void testBatchExcutor() {
   
		// 2.获取sqlSession
//		SqlSession sqlSession = sqlSessionFactory.openSession(true);
		SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, true);
		// 3.获取对应mapper
		TUserMapper mapper = sqlSession.getMapper(TUserMapper.class);
		
		TUser user = new TUser();
		user.setUserName("mark");
		user.setRealName("毛毛");
		user.setEmail("xxoo@163.com");
		user.setMobile("18695988747");
		user.setNote("mark's note");
		user.setSex((byte) 1);
		TPosition positon1 = new TPosition();
		positon1.setId(1);
		user.setPosition(positon1);
		System.out.println(mapper.insertSelective(user));
		
		TUser user1 = new TUser();
//		user1.setId(3);
		user1.setUserName("cindy");
		user1.setRealName("王美丽");
		user1.setEmail("xxoo@163.com");
		user1.setMobile("18695988747");
		user1.setNote("cindy's note");
		user1.setSex((byte) 2);
		user.setPosition(positon1);
		System.out.println(mapper.updateIfAndSetOper(user1));
		
		sqlSession.commit();
		System.out.println("----------------");

	}

直到执行sqlSession.commit();时,才提交到数据库,才能拿到这些对象的id。

2.关联查询

在这里插入图片描述
互联网高并发项目中,不建议使用级联,建议只使用单表。

 关联元素
 association 一对一关系
 collection 一对多关系
 discriminator 鉴别器映射
 关联方式
 嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集
 嵌套查询:通过执行另外一个 SQL 映射语句来返回预期的复杂类型
在这里插入图片描述
在这里插入图片描述
嵌套结果:一个sql语句得到所有,传统行业。
嵌套查询:与风控类似,先查询一个表,再根据表的信息,得到另一个表数据,互联网项目中建议使用,在java中对结果进行封装处理。

1)一对一 嵌套结果
 association标签 嵌套结果方式 常用属性:
 property :对应实体类中的属性名,必填项。
 javaType : 属性对应的 Java 类型 。
 resultMap : 可以直接使用现有的 resultMap ,而不需要在这里配置映射关系。
 columnPrefix :查询列的前缀,配置前缀后,在子标签配置 result 的 column 时可以省略前缀

Tips:

  1. resultMap可以通过使用extends实现继承关系,简化很多配置工作量;
  2. 关联的表查询的类添加前缀是编程的好习惯;
  3. 通过添加完整的命名空间,可以引用其他xml文件的resultMap;
<select id="selectUserPosition1" resultMap="userAndPosition1">
		select
		    a.id, 
		    user_name,
			real_name,
			sex,
			mobile,
			email,
			a.note,
			b.id  post_id,
			b.post_name,
			b.note post_note
		from t_user a,
			t_position b
		where a.position_id = b.id

	</select>

在上述sql语句中,从表属性建议写别名,映射做区分。

映射规则:

resultMap="userAndPosition1"
<resultMap id="userAndPosition1" extends="BaseResultMap" type="TUser">
		<association property="position" javaType="TPosition" columnPrefix="post_">
			<id column
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值