Mybatis2(黑马笔记)

一、起别名相关问题

因为实体类和表中字段起名的方式不同导致。
解决方法:
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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜狗ei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值