Mybatis学习

Mybatis 查看详情

1.编写接口方法:Mapper接口

Brand selectById(int id);

参数:id

结果:Brand 

2.编写SQL语句:SQL映射文件

3.执行方法

<select id="selectById" parameterType="int" resultType="brand">
  select * from tb_brand where id = #{id};
</select>

 参数占位符:

1.#{}:会将其替换为 ? ,为了防止SQL注入

2.${}:拼sql。会存在SQL注入问题

3.使用时机:

   *参数传递的时候:#{}

  *表名或者列名不固定的情况下:${} 会存在SQL注入问题

*特殊字符处理:

1.转义字符

2.CDATA区:

测试 

 

Mybatis 条件查询

1.编写接口方法:Mapper接口

参数:查询条件

结果:List<Brand>

/**
*条件查询
*参数接收  
  1.散装参数:如果方法中有多个参数,需要使用@Param("SQL参数占位符名称")
  2.对象参数:对象属性名称要和参数占位符名称一致
  3.map集合参数
*/
List<Brand> selectByCondtion(@Param("status")int status,@Param("companyName") String companyName,@Param("brandName") String brandName);

List<Brand> selectByCondition(Brand brand);

List<Brand> selectByCondition(Map map);

2.编写SQL语句:SQL映射文件

<select id="selectByCondition" resultMap="brandResultMap">
   select *
   from tb_brand
   where
      status = #{status}
      and company_name like #{companyName}
      and brand_name like #{brandName}
</select>

3.执行方法,测试

map方法:

Map map = HashMap();
map.put("status" , status);
map.put("companyName" , companyName);
map.put("brandName" , brandName);


List<Brand> brands = brandMapper.selectBycondition(map);
System.out.println(brands);

总结:SQL语句设置多个参数有几种方式

1.散装参数:需要使用@Param("SQL中的参数占位符名称")

2.实体类封装参数

    *只需要保证SQL中的参数名和实体类属性名对应上,即可设置成功

3.map集合

   *只需要保证SQL中的参数名和Map集合的键的名称对应上,即可设置成功

 

Mybatis 动态条件查询

 

<!--
动态条件查询
-->

<select id="selectByConditon" 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>

动态条件查询

*if :条件判断

   *test:逻辑表达式

     *问题:

      *解决方式:恒等式 where 1 = 1;<where></where>标签

 上图中如果用<where>标签的话就不用写otherwise 1 =1 

Mybatis 添加修改功能

1.编写接口方法:Mapper接口

void add(Brand brand);

参数:除了id之外的所有数据

结果:void

2.编写SQL语句:SQL映射文件

<insert id="add">
   insert into tb_brand(brand_name,company_name,ordered,description,status)
   values(#{brandName},#{companyName},#{ordered},#{description},#{status});
</insert>

3.执行方法,测试

MyBatis事务:

*openSession():默认开启事务,进行增删该操作后需要使用sqlSession.commit();手动提交事务

*openSession(true):可以设置为自动提交事务(关闭事务)或者SqlSession.commit;

添加主键返回

修改-修改全部字段 

1.编写接口方法:Mapper接口

参数:所有数据

结果:void

void update(Brand brand);

2.编写SQL语句:SQL映射文件

<update id="update">
  update tb_brand
  set brand_name = #{brandName},
      company_name = #{companyName},
      ordered = #{ordered},
      description = #{description},
      status = #{status}
  where id = #{id};
</update>

3.执行方法,测试

int count = brandMapper.update(brand);
System.out.println(count);

修改-修改动态字段

 上图用<set></set>标签,就可以规避status无值,where前出现逗号的情况

删除功能

1.编写接口方法:Mapper接口

参数:id

结果:void

void deleteById(int id);

2.编写SQL语句:SQL映射文件

<delete id="deleteById">
   delete from tb_brand where id = #{id}
</delete>

3.执行方法,测试

批量删除

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值