Mybatis学习笔记

1. mybatis的历史

MyBatis最初是Apache的一个开源项目iBatis,iBatis3.x正式更名为MyBatis。代码于 2013年11月迁移到Github。
iBatis一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。 iBatis提供的持久层框架 包括SQLMaps和Data Access Objects(DAO

2. mybatis特性

  1. MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架
  2. MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集
  3. MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,
    普通的Java对象)映射成数据库中的记录
  4. MyBatis 是一个 半自动的ORM(Object Relation Mapping)框架

3. mybatis标签

3.1. if标签

if 标签可通过test属性的表达式进行判断,若表达式的结果为true,则拼接标签中的内容;反之标签中的内容不会被拼接

//条件查询
List<User> queryByCondition(User user);
<select id="queryByCondition" parameterType="user" resultType="user">
  <!--if标签-->
  SELECT * from tb_user where 1=1
  <if test="username != null and username != ''">
    and username = #{username}
  </if>
  <if test="age != 0 and age != null">
    and age = #{age}
  </if>
  <if test="tel != null and tel != ''">
    and tel like  "%"#{tel}"%"
  </if>
</select>

3.2. where标签

where和if一般结合使用:

若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字

若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的and或or去掉

注意:where标签不能将其中内容后面多余的and或or去掉

<!--if标签与where标签结合-->
SELECT * from tb_user
<where>
  <if test="username != null and username != ''">
    and username like "%"#{username}"%"
  </if>
  <if test="age != 0 and age != null">
    and age = #{age}
  </if>
  <if test="tel != null and tel != ''">
    and tel like  "%"#{tel}"%"
  </if>
</where>

3.3. foreach标签

3.3.1. 批量添加
<!--批量添加-->
<!--void addBatch(@Param("users") List<User> users);-->
<insert id="addBatch" parameterType="list">
	insert into tb_user values 
	<foreach collection="users" item="user" separator=",">
		(null,#{user.username},#{user.password},#{user.age},#{user.tel})
	</foreach>
</insert>

collection:要遍历的集合,不需要使用#{}获取

item:遍历的每个元素,变量名

separator:分隔符

3.3.2. 批量删除
<!--批量删除-->
<!--void delBatch( @Param("ids") List<Integer> ids);-->
<delete id="delBatch" parameterType="list">
  delete from tb_user
  <foreach collection="ids" item="id" separator="," open="where id in (" close=")">
    #{id}
  </foreach>
</delete>

open:语句的开始部分

close:语句的结束部分

3.4. Set标签,局部更新

<!--局部更新-->
<update id="update" parameterType="user">
	update tb_user
	<set>
		<if test="username != null and username != ''">
			username = #{username},
		</if>
		<if test="password != null and password != ''">
			password = #{password},
		</if>
		<if test="age != null and age != 0">
			age = #{age},
		</if>
		<if test="tel != null and tel != ''">
			tel = #{tel},
		</if>
	</set>
	where id = #{id}
</update>
  • 自动加上set语句
  • 去掉最后的","

3.5. sql标签和include标签

SQL 标签是用于抽取可重用的 SQL 片段,将相同的,使用频繁的 SQL 片段抽取出来,单独定义,方便多次引用

include标签引用已经抽取的sql片段

<sql id="empColumns">
  id, last_name lastName, email email, gender gender from tbl_employee
</sql>
<!--查询-->
<select id="getEmpByEid" resultType="Emp">
  select
  <include refid="empColumns">
    <property name="testColomn" value="abc"/>  //还可以在这里设置属性
  </include> 
  where eid = #{eid}
</select>

3.6. resultMap标签

resultMap标签是封装返回值集合的标签,标签中id属性为该标签的名称,type为封装集合的类型,通常为实体类类型。id标签对应实体id,column为该字段在数据库中定义的名称,jdbcType为该字段在数据库中的类型,property为该字段在Java程序中实体类中的属性名。result标签则对应该实体类中的各个属性,此处应注意实体类中属性定义的名称与数据库中定义的名称的区别

<resultMap id="YwResultMap" type="com.java.pojo.Yw">
  <id column="yw_id" jdbcType="INTEGER" property="ywId" />
  <result column="yw_no" jdbcType="VARCHAR" property="ywNo" />
  <result column="gk_id" jdbcType="VARCHAR" property="gkId" />
  <result column="yw_name" jdbcType="VARCHAR" property="ywName" />
  <result column="yw_lrtime" jdbcType="TIMESTAMP" property="ywLrtime" />
  <result column="yw_xgtime" jdbcType="TIMESTAMP" property="ywXgtime" />
  <result column="jy_state" jdbcType="INTEGER" property="jyState" />
  <result column="yw_price" jdbcType="DOUBLE" property="ywPrice" />
</resultMap>

注意在mybatis中写自定义sql时,"<"符号需要使用"&lt;"替换

3.7. mybatis获取参数值的方法

MyBatis获取参数值有两种方式:${}和#{}

${}的本质是字符串拼接,#{}的本质就是占位符赋值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值