Mybatis基础
1.什么是mybatis
半自动orm(对象映射关系)框架;
1.1 相对于传统jdbc的好处
不必考虑与数据库连接资源的连接与释放,注重于业务即可;
提供了动态sql语句不需要考虑拼接问题;
sql与代码分离;
1.2 和全自动ORM的异同
需要自己编写sql,工作量大;
自己编写sql所以更灵活;尤其是针对需要优化的复杂sql;
1.3 mybatis的优缺点
优点:
- sql与代码分离,方便管理;
- 基于jdbc,只要能使用jdbc连接的数据库都能用mybatis;
- 容易与Spring集成;
- 提供映射标签,支持对象与数据库的ORM字段关系映射;
缺点: - QL语句的编写工作量较大,尤其当字段多、关联表多时,对开发人员编写SQL语句的功底有一定要求
- SQL语句依赖于数据库,导致数据库移植性差,不能随意更换数据库
2.参数传递
2.1 顺序传参
public User selectUser(String name, int deptId);
<select id="selectUser" resultMap="UserResultMap">
select * from user
where user_name = #{0} and dept_id = #{1}
</select>
2.2 @Param注解传参法
public User selectUser(@Param("userName") String name, int @Param("deptId") deptId);
<select id="selectUser" resultMap="UserResultMap">
select * from user
where user_name = #{userName} and dept_id = #{deptId}
</select>
2.3 Map传参法
public User selectUser(Map<String, Object> params);
<select id="selectUser" parameterType="java.util.Map" resultMap="UserResultMap">
select * from user
where user_name = #{userName} and dept_id = #{deptId}
</select>
2.4 Java Bean传参法
public User selectUser(User user);
<select id="selectUser" parameterType="com.jourwon.pojo.User" resultMap="UserResultMap">
select * from user
where user_name = #{userName} and dept_id = #{deptId}
</select>
3.常用标签
先偷个图
1.resultMap标签;一般的mapper.xml 最上面应该是resultMap标签,它表示了数据库和实体对应关系;
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.story.storyadmin.domain.entity.ceshi.Ceshi">
<result column="id" property="id" />
<result column="name" property="name" />
</resultMap>
2.sql标签和include ;接下来是sql片段:常用的片段可以封装起来,然后使用include 引用;
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id,
name
</sql>
引用片段使用include 标签:
<select id="select" resultMap="BaseResultMap" parameterType="Ceshi">
select <include refid="Base_Column_List"/> from ceshi where id = #{id}
</select>
3.再接下来是增删改查;
4.if标签;查询时where 的条件可能为空,所以用if判断;
<select id="select2" resultMap="BaseResultMap" parameterType="Ceshi">
select * from ceshi where
1=1
<if test="name != null and name != ''">
and id = #{name}
</if>
<if test="id != null and id != ''">
and id = #{id}
</if>
</select>
5.where标签;4中使用了 1=1来防止where 后全部为null,这里可以使用where标签代替;
<select id="select2" resultMap="BaseResultMap" parameterType="Ceshi">
select * from ceshi
<where>
<if test="name != null and name != ''">
and id = #{name}
</if>
<if test="id != null and id != ''">
and id = #{id}
</if>
</where>
</select>
6.foreach标签主要用于构建in条件,可在sql中对集合进行迭代。也常用到批量删除、添加等操作中。
构建in:
<select id="select2" resultMap="BaseResultMap" parameterType="list">
select * from ceshi
<where>
id in <foreach collection="list" open="(" close=")" separator="," item="item" index="index">
#{item.id}
</foreach>
</where>
</select>
批量插入:
<insert id="insert">
INSERT INTO ceshi(name)
VALUES
<foreach collection="list" item="item" separator=",">
(#{item,name})
</foreach>
</insert>
7.choose标签;有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。
<choose>
<when test="Name!=null and xxxx ">
A代码
</when>
<when test="Name= xxx">
B代码
</when>
<otherwise>
C代码
</otherwise>
</choose>
8.set标签;和where标签类似,也是防止出现空值;用于update中
<update id="update" parameterType="Ceshi">
UPDATE Ceshi
<set>
<if test="name!=null and name!='' ">
NAME = #{name},
</if>
</set>
WHERE ID = #{id};
</update>
4. 对应关系
xx 对多关联映射:collection
x’x 对一关联映射:association (一骚谁A深)
4.1 xx对一
property 时对返回值 ;javaType时另一个表的实体(orm)
<association property="clazz" javaType="org.zang.domain.Clazz">
<id property="id" column="id"/>
<result property="code" column="code"/>
<result property="name" column="name"/>
</association>
4.2 xx对多
property 时对返回值 ;javaType时另一个表的实体(orm)
<collection property="emps" ofType="com.mybatis.bean.Employee">
<!-- 定义这个集合中元素的封装规则 -->
<id column="eid" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</collection>