mybatis常用标签及应用

mybatis动态sql之标签篇

今天分享学习mybatis的标签,首先归纳总结常用的标签
常用的标签

1.sql语句

select

<select id="selectstu" resultType="model.Student">
		select * from students where sid=1
	</select>

insert

<insert id="addstu">
		insert into students values(#{stu.sid},#{stu.sname},#{stu.sage})
	</insert>

uptade

<update id="uptadestu">
	update students set sname=#{name} where sid=#{id}
	</update>

delete

<delete id="deletestu">
		delete from students where sid=#{id}
	</delete>

mybatis基础的增删改查标签的使用

2.映射关系

在写sql语句时,我们经常会遇到这些问题:
实体类属性(pojo,modeld等)与数据库中的字段不对应。
需要用到多表查询时往往要定制实体类。
表的关系是一对多。往往这些问题就需要我们去映射属性和字段的关系,这就需要用到我们的标签
1.一个简单的

<!-- type:是你要映射的实体类 id:sql语句去调用时写的(将sql语句里resultType改为resultMap) -->
<resultMap type="model.Student" id="stu">
	 <id property="sid" column="sid"/>
	 <result property="age" column="sage"/>
	</resultMap>

2.双表查时
先看一下实体类的写法

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Student {
	private int sid;
	private String sname;
	private int sage;
	//实体类成绩
	private Score score;	
}

resultmap的写法:使用了association

<!-- 这里property是属性字段的名字,column是对应的数据库字段名 -->
	<resultMap type="model.Student" id="ss">
		<id property="sid" column="sid"/>
		<result property="sage" column="sage" />
		<result property="sname" column="sname"/>
		<association property="score" javaType="model.Score">
			<result property="bishi" column="bishi"/>
			<result property="jishi" column="jishi"/>			
			<result property="zong" column="zong"/>			
		</association>		
	</resultMap>	

3.一对多的时候
先看实体类

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Student {
	private int sid;
	private String sname;
	private int sage;
	//对应物品实体类	
	private List<WuPin> objs=new ArrayList<WuPin>();
}

resultmap的写法:使用了collection

<resultMap type="model.Student" id="so">
		<id property="sid" column="sid"/>
		<result property="sname" column="sname"/>
		<collection property="objs" ofType="model.WuPin">
			<result property="desc" column="desc"/>		
		</collection>		
</resultMap>
3.动态sql

众所周知,mybatis最便捷的就是动态sql,而常用到的就是if choose foreach
1.if
判断是否执行sql语句执行就拼接sql,例如:用于查询条件,用户可以给不确定个数个条件,则需要用到if去判断是否为null,下面有个例子

	<select id="getStudentbyname" resultType="model.Student">
		select * from students where 1=1
		<where>
			<if test="name != null and name != ''">
			   and sname like concat(#{name},'%')
			</if>		
			<if test="age != null and age != ''">
				and sage &lt; #{age}
			</if>		
		</where>		
	</select>

2.choose
choose则有些类似于java中的switch case,当有一个执行成立时就拼接sql语句并不在执行下面的

	<select id="getstudent" resultMap="ss">
		select * from students s join score ss on s.sid = ss.sid where 1=1
		<choose>
			<when test="zong != null ">
				and zong &gt; #{zong}
			</when>
			<when test="bishi != null">
				and bishi &gt; #{bishi}
			</when>
			<otherwise>
				and sname like '张%'
			</otherwise>		
		</choose>	
	</select>

3.foreach
一般用于当sql某个条件有多个数据时,例如:查询一个公司里年纪是23,21,19的人

条件存放在数组中时

	<select id="getStuByAgeArray2" resultMap="stu">
		select * from students_copy where sage in
		<foreach collection="array" item="ages" open="(" close=")" separator=",">
			#{ages}	
		</foreach>
	</select>

条件存放在集合中时

	<select id="getStuByAgeArray2" resultMap="stu">
		select * from students_copy where sage in
		<foreach collection="list" item="ages" open="(" close=")" separator=",">
			#{ages}	
		</foreach>
	</select>

条件存放在map中时

<!-- 这里map里存放了 xuefen 和 ages的集合-->
<select id="getStuByMap" resultMap="stu">
		select * from students_copy where xuefen &gt; #{xuefen} and sage in
		<foreach collection="ages" item="age" open="(" close=")" separator=",">
			#{age}
		</foreach>
</select>

可以看到存放在数组和集合的时候,collection分别写的是array和list,而存放到map时直接写map里的key即可。

这里是因为mybatis底层在传值时是将值放到map中的。

所以在直接传map时,直接写map里的key就可以了

以上,skr~

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值