编写dao层xml文件

导入mybatis-plus-boot-starter依赖,就需要配置数据库的相关信息,一般会在公共模块下引入,其他模块引入公共模块,但是nacos服务引入在公共模块,有的不需要连接数据库,因此我们需要在不需要连接数据库的地方排除掉这个mybatis的依赖

<dependency>
			<groupId>com.wll.shop</groupId>
			<artifactId>wll-common</artifactId>
			<version>1.0-SNAPSHOT</version>
			<exclusions>
				<exclusion>
					<groupId>com.baomidou</groupId>
					<artifactId>mybatis-plus-boot-starter</artifactId>
				</exclusion>
			</exclusions>

parameterType ,resultMap,resultType

“parameterType”指的是传递给方法或函数的参数的数据类型。它指定在调用该方法或函数时应提供的值的类型。他一般省略不写
"parameterType"的取值可以是任何有效的Java数据类型,例如String、Integer、Boolean等

“resultMap”和“resultType”与对象关系映射(ORM)框架(如MyBatis或Hibernate)有关,这些框架帮助将数据库记录映射到面向对象的代码中。

“resultMap”用于定义数据库查询结果如何映射到对象。它指定了结果集中的列与对象的属性之间的映射关系。

<resultMap id="BaseResultMap" type="com.bjpowernode.crm.workbench.domain.Activity" >
    <id column="id" property="id" jdbcType="CHAR" />
    <result column="owner" property="owner" jdbcType="CHAR" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="start_date" property="startDate" jdbcType="CHAR" />
    <result column="end_date" property="endDate" jdbcType="CHAR" />
    <result column="cost" property="cost" jdbcType="VARCHAR" />
    <result column="description" property="description" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="CHAR" />
    <result column="create_by" property="createBy" jdbcType="VARCHAR" />
    <result column="edit_time" property="editTime" jdbcType="CHAR" />
    <result column="edit_by" property="editBy" jdbcType="VARCHAR" />
    <collection id="attrs" ofType="com.wll.shop.skuEsModule">
	    <result column="attr_id" property="atrrId"></result>
	    <result column="attr_name" property="attrName"></result>
	    <result column="attr_value" property="attrValue"></result>
    </collection>
  </resultMap>

另一方面,“resultType”指定应使用的Java数据类型来保存数据库查询结果。它通常用于结果是单个值或简单数据类型(例如Integer、String)的情况。

当数据库中没有匹配结果,查询的数据为空的时候,返回的类型是什么?

答:

第一种:resultType为基本类型,如string

如果select的结果为空,则dao接口返回结果为null

第二种,resultType为基本类型,如int

后台报异常: org.apache.ibatis.binding.BindingException: Mapper method
'com.fkit.dao.xxDao.getUserById attempted to return null from a method
with a primitive return type (int).
解释:查询结果为null,试图返回null但是方法定义的返回值是int,null转为int时报错
解决办法:修改select的返回值为String

第三种 resultType为类为map ,如map、hashmap

dao层接口返回值为null

第四种 resultType 为list ,如list

dao层接口返回值为[],即空集合。

注意:此时判断查询是否为空就不能用null做判断

第五种 resultType 为类 ,如com.fkit.pojo.User

dao层接口返回值null

“resultMap”和“resultType”都用于处理关系数据库和面向对象代码之间的映射,但与“resultType”相比,“resultMap”提供了更大的灵活性和对映射过程的控制。

动态sql标签

if

他常用于判断传进来的参数的值是否为空,如果不为空加入sql语句中
mapper接口:

List<UserInfo> queryByUserInfo(UserInfo userInfo);

映射文件中对应的动态sql:

<select id="queryByUserInfo" parameterType="UserInfo" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from user_info where 1=1
    <if test="name != null and name != ''">
        and name=#{name}
    </if>
    <if test="gender != null and gender != ''">
        and gender=#{gender}
    </if>
</select>

2、where标签

where标签的作用:让where子句更加动态智能。
● 所有条件都为空时,where标签保证不会生成where子句。
● 自动去除某些条件前面多余的and或or。
把上面的代码where1=1修改为下面的代码,当存在用户名时根据用户名查询,存在性别根据性别查询,两个都存在是根据两个条件插叙,两个都不存在时查询全部。

<select id="queryByUserInfo" parameterType="UserInfo" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from user_info
    <where>
        <if test="name != null and name != ''">
            and name=#{name}
        </if>
        <if test="gender != null and gender != ''">
            and gender=#{gender}
        </if>
    </where>
</select>

trim标签

trim标签的属性:
● prefix:在trim标签中的语句前添加内容
● suffix:在trim标签中的语句后添加内容
● prefixOverrides:前缀覆盖掉(去掉)
● suffixOverrides:后缀覆盖掉(去掉)

List<Car> selectByMultiConditionWithTrim(@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("carType") String carType);
<select id="selectByMultiConditionWithTrim" resultType="car">
  select * from t_car
  <trim prefix="where" suffixOverrides="and|or">
    <if test="brand != null and brand != ''">
      brand like #{brand}"%" and
    </if>
    <if test="guidePrice != null and guidePrice != ''">
      guide_price >= #{guidePrice} and
    </if>
    <if test="carType != null and carType != ''">
      car_type = #{carType}
    </if>
  </trim>
</select>

foreach

他的属性:

collection:集合或数组,如传进来参数的名称
item:集合或数组中的元素
separator:分隔符 ,如’,’
open:foreach标签中所有内容的开始
close:foreach标签中所有内容的结束

循环数组或集合,动态生成sql,比如这样的SQL:
批量删除:

delete from t_car where id in(1,2,3);
delete from t_car where id = 1 or id = 2 or id = 3;

批量添加:

insert into t_car values
  (null,'1001','凯美瑞',35.0,'2010-10-11','燃油车'),
  (null,'1002','比亚迪唐',31.0,'2020-11-11','新能源'),
  (null,'1003','比亚迪宋',32.0,'2020-10-11','新能源')

用in来删除

int deleteBatchByForeach(@Param("ids") Long[] ids);
<delete id="deleteBatchByForeach">
  delete from t_car where id in
  <foreach collection="ids" item="id" separator="," open="(" close=")">
    #{id}
  </foreach>
</delete>

用or来删除

int deleteBatchByForeach2(@Param("ids") Long[] ids);
<delete id="deleteBatchByForeach2">
  delete from t_car where
  <foreach collection="ids" item="id" separator="or">
    id = #{id}
  </foreach>
</delete>

批量添加

int insertBatchByForeach(@Param("cars") List<Car> cars);
<insert id="insertBatchByForeach">
  insert into t_car values 
  <foreach collection="cars" item="car" separator=",">
    (null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})
  </foreach>
</insert>

sql标签与include标签

sql标签用来声明sql片段
include标签用来将声明的sql片段包含到某个sql语句当中
作用:代码复用。易维护。

<sql id="carCols">id,car_num carNum,brand,guide_price guidePrice,produce_time produceTime,car_type carType</sql>

<select id="selectAllRetMap" resultType="map">
  select <include refid="carCols"/> from t_car
</select>

<select id="selectAllRetListMap" resultType="map">
  select <include refid="carCols"/> carType from t_car
</select>

<select id="selectByIdRetMap" resultType="map">
  select <include refid="carCols"/> from t_car where id = #{id}
</select>

查询

1、List<Activity> selectActivityForDetailByIds(String[] ids);

传入一个String数组,通过这个数组来查询活动,返回一个Activity活动集合

  <select id="selectActivityForDetailById" parameterType="string" resultMap="BaseResultMap">
      select a.id,u1.name as owner,a.name,a.start_date,a.end_date,a.cost,a.description,a.create_time,u2.name as create_by,
          a.edit_time,u3.name as edit_by
      from tbl_activity a
      join tbl_user u1 on a.owner=u1.id
      join tbl_user u2 on a.create_by=u2.id
      left join tbl_user u3 on a.edit_by=u3.id
      where a.id=#{id}
    </select>

注意事项:

1、parameterType为string类型是因为数组中的每一个元素都是string类型的,resultMap对应写好的数据库字段和实体类属性的映射id
2、多表联查,join on 后面的on字段是主表属性在前
3、传进来的参数用#{}括起来,括号内的值和传进来的dao层方法的参数名相同。可以使用@Param()来定义

2、List<Activity> selectActivityForDetailByNameClueId(Map<String,Object> map);

 <select id="selectActivityForDetailByNameClueId" parameterType="map" resultMap="BaseResultMap">
    select a.id,a.name,a.start_date,a.end_date,u.name as owner
    from tbl_activity a
    join tbl_user u on a.owner=u.id
    where a.name like '%' #{activityName} '%' and a.id not in (
         select activity_id
         from tbl_clue_activity_relation
         where clue_id=#{clueId}
    )
  </select>

注意事项

1、parameterType是传进来的参数类型是map,因此是map 2、这里涉及到了模糊查询,模糊查询这里使用的是 like '%' #{参数} '%'

SQL中IF函数的使用

语法:IF(expr1,expr2,expr3)

expr1 的值为 TRUE,则返回值为 expr2
expr1 的值为FALSE,则返回值为 expr3

其中,expr1是判断条件,expr2和expr3是符合expr1的自定义的返回结果。

select
if(il.status_id = 'INV_STTS_AVAILABLE','全新','二手') as status_id
from inventory_location as il;

求有效期

在查询的时候的select后面使用
有效期是当前时间到终止时间(大于0则显示,否则<0是默认为0)

IF(TIMESTAMPDIFF(DAY,NOW(),t.end_date) >0 ,TIMESTAMPDIFF(DAY,NOW(),t.end_date),0) as effictiveDay

知识点讲解----------------------时间差函数:TIMESTAMPDIFF() -----------------------------------------

TIMESTAMPDIFF() 是 MySQL 中用来计算两个日期或时间之间的差值的函数。该函数返回两个日期/时间之间的差值,可以指定差值的单位(秒、分钟、小时、天等)。TIMESTAMPDIFF() 函数的语法如下:

TIMESTAMPDIFF(unit, start_date, end_date)

参数说明:

unit:表示差值的单位,可以是以下值之一:MICROSECOND(微秒)、SECOND(秒)、MINUTE(分)、HOUR(小时)、DAY(天)、WEEK(周)、MONTH(月)、QUARTER(季度)或 YEAR(年)。
start_date:表示时间段的起始时间。
end_date:表示时间段的结束时间。

插入

删除

新增

1、int insertActivityByList(List activityList);

 <insert id="insertActivityByList" parameterType="com.bjpowernode.crm.workbench.domain.Activity">
    insert into tbl_activity(id, owner, name, start_date, end_date, cost, description, create_time, create_by)
    values
    <foreach collection="list" item="obj" separator=",">
      (#{obj.id},#{obj.owner},#{obj.name},#{obj.startDate},#{obj.endDate},#{obj.cost},#{obj.description},#{obj.createTime},#{obj.createBy})
    </foreach>
  </insert>

注意事项:

1、因为插入的元素每一个都是Activity类型的,因此parameterType是Activity类型的 因为这个是inset into
表名(列名1,列名2,…)
values(?,?,…)(?,?,…),批量插入,因此这里使用了foreach标签,遍历整个集合,在values后面给占位符赋值。

Sql中的关键字

GROUP_CONCAT

分析当前spu有多少个sku,所有sku涉及到的属性组合?
1个spu有多个sku,sku有多个属性组合,应该是一个属性id,一个属性的名称,对应有多少属性。
比如说品牌是attr,他的名字是品牌,他的值可以是小米,华为,苹果等。
实现一对多的展现结果,使用GROUP_CONCAT关键字。

select ssav.`attr_id` attr_id,
ssav.`attr_name` attr_name,
GROUP_CONCAT(DISTINCT ssav.`attr_value`) attr_values
from `pms_sku_info` info
left join `pms_sku_sale_attr_value` ssav on ssav.`sku_id` = info.`sku_id`
where info.`spu_id` = #{spu_id}
group by ssav.`attr_id`,ssav.`attr_name`

在这里插入图片描述

@Mapper
List<SkuItemSaleAttrVo> getSaleAttrsBySpuId(@Param("spuId") Long spuId);

Case When
现在公司出台了一个奇葩的规定

对当前工资为 1 万以上的员工,降薪 10%。
对当前工资低于 1 万的员工,加薪 20%。

需要使用case when

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值