注意点1:namespace:这个里面写的是与这个xml所对应的bookMapper.java的全路径。
//该xml的名字为BookMapper.xml
<?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="cn.lq.booksystem.dao.BookMapper">
````````
</mapper/>
注意点2:id=”findAllBook”,要和bookmapper.java里面所对应的接口的方法名一致
<select id="findAllBook" resultMap="allBookResultMap">
SELECT * FROM book LEFT JOIN category ON book.c_id=category.c_id
</select>
注意点3:当你的数据库表中的列名与对应java类的属性名不一致时,需要使用resultMap进行映射绑定,如果是关联查询,一定要使用resultMap进行设置
//id="allBookResultMap",这个是代表这个resultMap的id,如果在select中返回值是resultMap,其Id要与对应的这个Id相同
<resultMap type="cn.lq.booksystem.bean.Book" id="allBookResultMap">
//column代表的是数据库中列的名字,property代表实体类中的属性名
//主键的映射绑定使用的是id,
<id column="b_id" property="bId"/>
//普通列使用result,
<result column="b_name" property="bName"/>
<result column="price" property="price"/>
<result column="author" property="author"/>
<result column="c_id" property="cId"/>
//绑定关联查询用association,association标签里的property指的是实体类中的属性,javaType是表示这个属性的类型
<association property="category" javaType="cn.lq.booksystem.bean.Category">
<id column="c_id" property="cId"/>
<result column="c_name" property="cName"/>
</association>
</resultMap>
注意点4:进行sql的拼装时,要理解每一项的含义
注意
parameterType=”java.util.List”是传入参数的类型。
collection=”list”,声明循环的是List,一定是list,不能是你传入的参数,例如虽然你实际传入的list的名字是headerList,但是写的时候也是list;
item=”bId”是每个小项,自己取名,下面会用到。 open=”(” close=”)” index=”index”
separator=”,” :这个是用来拼装成in(bId1,bId2…..)这种形式的。一定是逗号。因为in语句中使用的是逗号。
<!-- 使用sql语句进行批量删除 -->
<delete id="deleteBook" parameterType="java.util.List">
delete from book where b_id in
<foreach collection="list" item="bId" open="(" close=")" index="index" separator=",">
#{bId}
</foreach>
</delete>
注意点5:关于使用#{}和${value}
1、#{}实现的是向prepareStatement中的预处理语句中设置参数值,sql语句中#{}表示一个占位符即?,使用占位符可以有效的防止sql注入,在进行传参时,不需要关心参数值的类型,mybatis会自动进行java类型和jdbc类型的转换,可以解收简单类型和pojo类型。简单类型时,#{}括号中可以是value或其它名称。
select * from user where name = #{name};
//解析之后为
select * from user where name = ?;
2、${}
,直接将传入的值放入到sql语句中,会引起sql注入,可以解收简单类型和pojo类型,但是当传入的值是简单类型的时候,${}
中大括号里的值必须为value,即${value}
,在进行模糊查询的时候通常使这种方式
select * from user where name = ${zhangsan};
//解析之后为
select * from user where name = zhangsan;
注意点6:关于传递的参数是pojo类型
//传递的是user,在 where id=#{id},#{ }里面直接写的是user中的属性id的名称
class userVo{
private User user;
}
//如果传递的是包装类userVo,这是#{ }里面写的是user.id。
<select id="findUserByUser" parameterType="user" resultType="user">
select * from user where id=#{id} and username like '%${username}%'
</select>
注意点7:动态sql
1、if:这个一定要进行非空的校验
<!-- 传递pojo综合查询用户信息 -->
<select id="findUserList" parameterType="user" resultType="user">
select * from user
where 1=1
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</select>
2、 where:其中我们每一个都要写and,如果判断完以后,只有一个语句执行,where语句可以自动处理第一个and
<select id="findUserList" parameterType="user" resultType="user">
select * from user
<where>
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</where>
</select>
3、 foreach
分为传递单个list和单个数组
单个list:同上面——注意点4
单个数组:index:为数组的下标。
item:为数组每个元素的名称,名称随意定义
open:循环开始
close:循环结束
separator:中间分隔输出
<!-- 传递数组综合查询用户信息 -->
<select id="selectUserByArray" parameterType="Object[]" resultType="user">
select * from user
<where>
<!-- 传递数组 -->
<if test="array!=null">
<foreach collection="array" index="index" item="item" open="and id in("separator=","close=")">
#{item.id}
</foreach>
</if>
</where>
</select>
4、sql片段:如果引用其它mapper.xml的sql片段,则在引用时需要加上namespace,如下:
Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的,如下:
<sql id="query_user_where">
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</sql>
//使用include引入
<select id="findUserList" parameterType="user" resultType="user">
select * from user
<where>
<include refid="query_user_where"/>
</where>
</select>