Mybatis学习总结(三) 实战

前面两篇文章已经简单介绍了Mybatis并且讲述了如何快速搭建Mybatis环境,本文简单介绍项目开发中主要用到的功能点,包括以下知识点:

1. ResultMap的编写
2. 基本select
3. update语句,<set>标签
4. 单层foreach的用法
5. delete语句
6. <![CDATA[需要转义的段落]]>,转义 <> & 符号
7. <if test="nodeType==1"></if> 动态sql
8. 双层foreach
9. 分层List,一对多映射 collection,一对一映射 association
10. resultMap 中调用外部select单元
11. <sql> 标签定义代码段,<include>调用,实现SQL复用

本项目包括所有文件和数据库脚本本人都已上传到码云:https://gitee.com/nonkey/Mybatis-Practise 

欢迎各位大神批评指正,共同成长。

<?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.jnk.mybatis.practise.service.mapper.IPlayerMapper">
	<resultMap type="com.jnk.mybatis.practise.service.entity.PlayerEntity"
		id="playerInfo">
		<result property="id" column="ID" />
		<result property="pNameCn" column="P_NAME_CN" />
		<result property="pNameEn" column="P_NAME_EN" />
		<result property="pNo" column="P_NO" />
		<result property="birthDay" column="BIRTH_DAY" />
		<result property="age" column="AGE" />
		<result property="teamId" column="TEAM_ID" />
	</resultMap>
	<sql id="commonPlayerInfo">
		select
		ID,
		P_NAME_CN,
		P_NAME_EN,
		P_NO,
		BIRTH_DAY,
		YEAR(CURRENT_DATE()) - YEAR(BIRTH_DAY) as AGE,
		TEAM_ID
		from tbl_player
	</sql>
	<select id="queryPlayerInfo" resultMap="playerInfo">
		<include refid="commonPlayerInfo"></include>
		where id = #{id}
	</select>

	<select id="queryAllPlayerInfo" resultMap="playerInfo">
		<include refid="commonPlayerInfo"></include>
		where id <![CDATA[<>]]>
		6 <!-- CDATA 转义的用法 -->
	</select>

	<update id="updatePlayerById">
		update tbl_player
		<set>
			P_NAME_EN = #{pNameEn}
		</set>
		where id=#{id}
	</update>

	<update id="updatePlayerByIds">
		<foreach collection="players" item="item" index="index" open=""
			close="" separator=";">
			update tbl_player
			<set>
				P_NAME_EN=#{item.pNameEn}
			</set>
			where id=#{item.id}
		</foreach>
	</update>

	<update id="updatePlayerByCondition">
		<foreach collection="players" item="item" separator=";">
			update tbl_player
			<set>
				<if test="condition == 1">
					P_NAME_EN = #{item.pNameEn},
				</if>
				<if test="condition == 2">
					P_NAME_CN = #{item.pNameCn}
				</if>
			</set>
			where id=#{item.id}
		</foreach>
	</update>


	<resultMap id="totalZone"
		type="com.jnk.mybatis.practise.service.vo.TotalZoneInfoVo">
		<result property="zoneId" column="ZONE_ID"></result>
		<result property="zoneName" column="ZONE_NAME_CN"></result>
		<collection property="teams"
			ofType="com.jnk.mybatis.practise.service.vo.TeamVo">
			<result property="teamId" column="TEAM_ID"></result>
			<result property="teamNameCn" column="TEAM_NAME_CN"></result>
			<result property="location" column="LOCATION"></result>
			<collection property="players"
				ofType="com.jnk.mybatis.practise.service.entity.PlayerEntity"
				column="{teamId=TEAM_ID}" select="queryPlayerInfoByTeamId"></collection>
		</collection>
	</resultMap>
	<select id="queryAllZoneInfo" resultMap="totalZone">
		SELECT
		zone.ZONE_ID,
		zone.ZONE_NAME_CN,
		team.TEAM_ID,
		team.TEAM_NAME_CN,
		team.LOCATION
		FROM
		tbl_zone zone
		INNER JOIN tbl_team team ON zone.ZONE_ID = team.ZONE_ID
	</select>

	<!-- association 感觉用处不大,这里有两个分区,返回值也是List,但是只会返回一个 -->
	<resultMap id="zoneMap" type="com.jnk.mybatis.practise.service.vo.ZoneVo">
		<association property="zones"
			javaType="com.jnk.mybatis.practise.service.entity.ZoneEntity">
			<result property="zoneId" column="ZONE_ID"></result>
			<result property="zoneName" column="ZONE_NAME_CN"></result>
		</association>
	</resultMap>
	<select id="queryZoneInfo" resultMap="zoneMap">
		SELECT
			zone.ZONE_ID,
			zone.ZONE_NAME_CN
		FROM
		tbl_zone zone
	</select>

	<select id="queryPlayerInfoByTeamId" resultMap="playerInfo">
		<include refid="commonPlayerInfo"></include>
		where TEAM_ID = #{teamId}
	</select>
	
	<!-- 多层foreach循环 -->
	<update id="updateTeamPlayer">
		<foreach collection="teams" item="items">
			<foreach collection="items.players" item="player">
				update tbl_player
				set P_NAME_EN = CONCAT(#{player.pNameCn},' NEW')
				where ID = #{player.id};
			</foreach>
		</foreach>
	</update>

	<delete id="deletePlayerById">
		delete from tbl_player
		where id = #{id};
	</delete>
	
</mapper>

参考文献:
http://www.yiibai.com/mybatis/
http://www.mybatis.org/mybatis-3/zh/index.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一个流行的Java持久化框架,它提供了一种简单且灵活的方式来访问数据库。MyBatis的配置文件对整个框架的使用产生深远的影响,因此我们需要认真学习它。[1]配置文件中包含了数据库连接信息、映射器配置、SQL语句等重要内容,通过配置文件可以实现对数据库的增删改查操作。 除了配置文件,MyBatis最强大的工具之一是映射器。映射器是用于定义SQL语句和Java方法之间映射关系的工具,我们在使用MyBatis时会经常使用到它。[1]映射器可以将数据库表的字段映射到Java对象的属性上,使得我们可以方便地进行对象与数据库之间的转换。 在实际工作中,我们经常会遇到一些特殊的场景,需要灵活运用MyBatis来解决问题。比如处理数据库的BLOB字段的读写、批量更新、调用存储过程、分页、使用参数作为列名、分表等等。[2]这些场景都是通过实战总结出来的,具有较强的实用价值,可以帮助我们更好地应对实际开发中的需求。 此外,MyBatis和Spring框架的结合也是非常常见的。Spring框架是Java世界最流行的IOC和AOP框架之一,而MyBatis和Spring的结合可以构建高性能的大型网站。[3]通过使用Spring MVC和MyBatis,我们可以充分发挥它们的优势,实现灵活可配置的SQL操作,从而构建高性能的Java互联网应用。 总结起来,深入浅出地学习MyBatis的技术原理和实战经验,可以帮助我们更好地理解和应用这个持久化框架,提高开发效率和代码质量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值