一、mybatis的优点
(1)基 于 SQL 语句编程,相当灵活,不会对应用程序或者数据库的现有设计造成任何影响,SQL 写在 XML 里,解除 sql 与程序代码的耦合,便于统一管理;提供 XML标签,支持编写动态 SQL 语句,并可重用。
(2)与 JDBC 相比,减少了 50%以上的代码量,消除了 JDBC 大量冗余的代码,不需要手动开关连接;
(3)很好的与各种数据库兼容(因为 MyBatis 使用 JDBC 来连接数据库,所以只要JDBC 支持的数据库 MyBatis 都支持)。
(4)能够与 Spring 很好的集成;
-
mapper层
其中的方法语句时直接对数据库中的数据进行操作的,mapper层的方法在mybatis中与 .xml文件一一对应
<mapper namespace="com.qcby.mapper.UserMapper">
-
xml层
<resultMap id="ResultMap" type="com.qcby.entity.User"> <id column="id" property="id" jdbcType="INTEGER"/> <result column="username" property="username" jdbcType="VARCHAR"/> <result column="birthday" property="birthday" jdbcType="VARCHAR"/> <result column="sex" property="sex" jdbcType="VARCHAR"/> <result column="address" property="address" jdbcType="VARCHAR"/> </resultMap> <select id ="findAllResultMap" resultMap="ResultMap"> select * from user </select>
column是数据库中的字段
property是要映射成的实体类中的字段
jdbcType是字段类型
二、 获取参数值
- #{} 和 ${}
${}的本质是字符串拼接,#{}的本质是占位符赋值
第一种${},采用sql拼接,不能防止sql注入
<!--通过$进行查询-->
<select id="selectBy$" parameterType="java.lang.String" resultType="com.qcby.entity.User">
select * from user where username = '${value}'
</select>
第二种#{},采用预编译,防止sql注入,传入数据都会加引号‘,作为参数不作为指令去执行
<!--通过#进行查询-->
<select id="select" parameterType="java.lang.String" resultType="com.qcby.entity.User">
select * from user where username = #{username}
</select>
传入"熊大" ==>处理后\"熊大\" 作为一个参数来执行
- 说明
- 不论是单个参数,还是多个参数,一律都建议使用注解@Param("key"),
- xml取#{key}
- 能用 #{} 的地方就用 #{},不用或少用 ${}
- $符号 =》 sql注入
- 表名作参数时,必须用 ${}。如:select * from ${tableName}
- order by 时,必须用 ${}。如:select * from t_user order by ${columnName}
- 使用 ${} 时,要注意何时加或不加单引号
三、CRUD
- 批量插入 => foreach
<insert id="insertBatch">
insert into student (name, img, sex, create_time)
values
<foreach collection="studentList" item="student" separator=",">
(
#{student.name,jdbcType=VARCHAR},
#{student.img,jdbcType=VARCHAR},
#{student.sex,jdbcType=INTEGER},
#{student.createTime,jdbcType=TIMESTAMP}
)
</foreach>
</insert>
int insert(@Param("studentList") List<Student> studentList);
- 批量删除
<delete id="deleteByIds" parameterType="java.lang.Long" >
delete from student where id in
<foreach collection="idList" index="index" item="item"
open="(" separator="," close=")">
#{item}
</foreach>
</delete>
int delete(@Param("idList") Integer[] idList);
- 动态sql
- <where>--<if>标签
用<where>
和<if>
进行组合,当条件不成立时,if条件后的内容包括and也不会存在,因此不会对整个sql语句产生影响。注意and关键字要放在每个<if>
语句中的库表字段赋值的前面。因为,一旦判断不成功,<where>
会把对应的and关键字去掉(还有or关键字)。
<select id="selectUserByUsernameAndSex" parameterType="com.qcby.entity.User"
resultType="com.qcby.entity.User">
select * from user
<where>
<if test="username != null">
username=#{username}
</if>
<if test="sex != null">
and sex=#{sex}
</if>
</where>
</select>
- <set> <if>标签
<!--注意字段判断-->
<update id="updateStuddent" >
update student
<set>
<if test="student.name != null">
name = #{student.name,jdbcType=VARCHAR},
</if>
<if test="student.img != null">
img = #{student.img,jdbcType=VARCHAR},
</if>
<if test="student.sex != null">
sex = #{student.sex,jdbcType=INTEGER},
</if>
<if test="student.createTime != null">
create_time = #{student.createTime,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{student.id,jdbcType=BIGINT}
</update>
<set> <if>标签 if里面不用加and
<choose>、<when>和<otherwise>标签
相当于java中的if.....else if.....else
<choose>标签是这个标签组合当中的父标签和标签都在标签内部。
<when>标签就相当于是我们的 if 和 elseif
<otherwise>标签相当于是我们的 else
<select id="selectUserByChoose" resultType="com.qcby.entity.User"
parameterType="com.qcby.entity.User">
select * from user
<where>
<choose>
<when test="id !='' and id != null">
id=#{id}
</when>
<when test="username !='' and username != null">
and username=#{username}
</when>
<otherwise>
and sex=#{sex}
</otherwise>
</choose>
</where>
</select>
四、联表查询
mybatis关联映射 (一对一,多对一,多对多)
详情请看另一篇文章~mybatis关联映射(表的三种关系,含具体代码)_叫我老伯的博客-CSDN博客_mybatis 映射表