Mybatis相关知识点
一、起别名相关问题
因为实体类和表中字段起名的方式不同导致。
解决方法:
1.在写sql语句的时候给字段起别名。
(将需要用的sql片段抽取到sql标签中。)
<sql id="brand_column">
id,brand_name as brandName,company_name as companyName
</sql>
引用sql标签
<select id="selectAll" resultType="brand">
select
<include refid="brand_column" />
from tb_brand;
</select>
2.用resultMap解决
在映射文件中实用resultMap定义字段
<resultMap id="brandResultMap" type="brand">
<!--
id:完成主键字段的映射 column:表的列名 property:实体类的属性名
result:完成一般字段的映射 column:表的列名 property:实体类的属性名 -->
<result column="brand_name" property="brandName"/>
<result column="company_name" property="companyName"/> </resultMap>
<select id="selectAll" resultMap="brandResultMap">
select *
from tb_brand;
</select>
二、#{}与${}
1 #{}
#{} :执行SQL时,会将 #{} 占位符替换为?,将来自动设置参数值。#{} 底层使用的是PreparedStatement
在日志中:Preparing:后是对应的sql语句。
Parameters:后是传入的参数。
2 ${}
${} :拼接SQL。底层使用的是 Statement ,直接传入参数,会存在SQL注入问题。
sql注入:SQL 注入就是指 web应用程序对用户输入的数据合法性没有过滤或者是判断,前端传入的参数是攻击者可以控制,并且参数带入数据库的查询,攻击者可以通过构造恶意的 sql语句来实现对数据库的任意操作。
三、编写接口方法及其对应的执行方法
1.使用@Param(“参数名称”)标记每一个参数
List<Brand> selectByCondition(@Param("status") int status
,@Param("companyName") String companyName,
@Param("brandName") String brandName)
对应执行方法时:
List<Brand> brands = brandMapper.selectByCondition(status,companyName,brandName);
2.将多个参数封装成一个实体对象,将该对象作为接口的方法参数
List<Brand> selectByCondition(Brand brand);
对应的执行方法:
Brand brand = new Brand();
brand.setStatus(status);
brand.setCompanyName(companyName);
brand.setBrandName(brandName);
List<Brand> brands = brandMapper.selectByCondition(brand);
3.将多个参数封装到Map集合里,将map集合作为接口的方法参数
List<Brand> selectByCondition(Map map);
对应执行方法:
Map map = new HashMap();
map.put("status",status);
map.put("companyName", companyName);
map.put("brandName" , brandName);
List<Brand> brands = brandMapper.selectByCondition(map);
四、动态sql
当用户输入条件时,不会所有有条件都写,这时候就要动态sql
if(test):条件判断
test属性:逻辑表达式
<select id="selectByCondition" resultMap="brandResultMap">
select *
from tb_brand
where
<if test="status != null">
status = #{status}
</if>
<if test="companyName != null and companyName != '' ">
and company_name like #{companyName}
</if>
<if test="brandName != null and brandName != '' ">
and brand_name like #{brandName}
</if>
</select>
如果给的参数是后两个,sql语句where后面就会直接and,于是用where标签解决
where标签作用:
1.替换where关键字
2.动态去掉第一个and
3.如果所有参数没有值则不加where
<select id="selectByCondition" resultMap="brandResultMap">
select *
from tb_brand
<where>
<if test="status != null"> and status = #{status} </if>
<if test="companyName != null and companyName != '' "> and company_name like #{companyName} </if>
<if test="brandName != null and brandName != '' "> and brand_name like #{brandName} </if>
</where>
</select>
choose (when,otherwise)
如果只传递一个参数的情况(例如下拉框选择某个种类)用到choose标签
<select id="selectByConditionSingle" resultMap="brandResultMap"> select *
from tb_brand
<where>
<choose>
<!--相当于switch-->
<when test="status != null">
status = #{status}
</when>
<when test="companyName != null and companyName != '' ">
company_name like #{companyName}
</when>
<when test="brandName != null and brandName != ''">
brand_name like #{brandName}
</when>
</choose>
</where>
</select>
foreach
用来迭代任何可迭代的对象(如数组,集合)。
例如进行批量删除时
<delete id="deleteByIds">
delete from tb_brand
where id
in
<foreach collection="array" item="id" separator="," open="(" close=")">
#{id}
</foreach>
;
</delete>
得到的sql语句是:
delete from tb_brand where id in (1,2,3);
五、insert标签
useGeneratedKeys:是够获取自动增长的主键值。true表示获取
keyProperty :指定将获取到的主键值封装到哪儿个属性里
六、update标签
<update id="update">
update tb_brand
<set>
<if test="brandName != null and brandName != ''">
brand_name = #{brandName},
</if>
<if test="companyName != null and companyName != ''">
company_name = #{companyName},
</if>
<if test="ordered != null">
ordered = #{ordered},
</if>
<if test="description != null and description != ''">
description = #{description},
</if>
<if test="status != null">
status = #{status}
</if>
</set>
where id = #{id};
</update>