導(dǎo)入mybatis-plus-boot-starter依賴,就需要配置數(shù)據(jù)庫的相關(guān)信息,一般會(huì)在公共模塊下引入,其他模塊引入公共模塊,但是nacos服務(wù)引入在公共模塊,有的不需要連接數(shù)據(jù)庫,因此我們需要在不需要連接數(shù)據(jù)庫的地方排除掉這個(gè)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”指的是傳遞給方法或函數(shù)的參數(shù)的數(shù)據(jù)類型。它指定在調(diào)用該方法或函數(shù)時(shí)應(yīng)提供的值的類型。他一般省略不寫。
"parameterType"的取值可以是任何有效的Java數(shù)據(jù)類型,例如String、Integer、Boolean等
“resultMap”和“resultType”與對(duì)象關(guān)系映射(ORM)框架(如MyBatis或Hibernate)有關(guān),這些框架幫助將數(shù)據(jù)庫記錄映射到面向?qū)ο蟮拇a中。
“resultMap”用于定義數(shù)據(jù)庫查詢結(jié)果如何映射到對(duì)象。它指定了結(jié)果集中的列與對(duì)象的屬性之間的映射關(guān)系。
<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”指定應(yīng)使用的Java數(shù)據(jù)類型來保存數(shù)據(jù)庫查詢結(jié)果。它通常用于結(jié)果是單個(gè)值或簡(jiǎn)單數(shù)據(jù)類型(例如Integer、String)的情況。
當(dāng)數(shù)據(jù)庫中沒有匹配結(jié)果,查詢的數(shù)據(jù)為空的時(shí)候,返回的類型是什么?
答:
第一種:resultType為基本類型,如string
如果select的結(jié)果為空,則dao接口返回結(jié)果為null
第二種,resultType為基本類型,如int
后臺(tái)報(bào)異常: 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).
解釋:查詢結(jié)果為null,試圖返回null但是方法定義的返回值是int,null轉(zhuǎn)為int時(shí)報(bào)錯(cuò)
解決辦法:修改select的返回值為String第三種 resultType為類為map ,如map、hashmap
dao層接口返回值為null
第四種 resultType 為list ,如list
dao層接口返回值為[],即空集合。
注意:此時(shí)判斷查詢是否為空就不能用null做判斷
第五種 resultType 為類 ,如com.fkit.pojo.User
dao層接口返回值null
“resultMap”和“resultType”都用于處理關(guān)系數(shù)據(jù)庫和面向?qū)ο蟠a之間的映射,但與“resultType”相比,“resultMap”提供了更大的靈活性和對(duì)映射過程的控制。
動(dòng)態(tài)sql標(biāo)簽
if
他常用于判斷傳進(jìn)來的參數(shù)的值是否為空,如果不為空加入sql語句中
mapper接口:
List<UserInfo> queryByUserInfo(UserInfo userInfo);
映射文件中對(duì)應(yīng)的動(dòng)態(tài)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標(biāo)簽
where標(biāo)簽的作用:讓where子句更加動(dòng)態(tài)智能。
● 所有條件都為空時(shí),where標(biāo)簽保證不會(huì)生成where子句。
● 自動(dòng)去除某些條件前面多余的and或or。
把上面的代碼where1=1修改為下面的代碼,當(dāng)存在用戶名時(shí)根據(jù)用戶名查詢,存在性別根據(jù)性別查詢,兩個(gè)都存在是根據(jù)兩個(gè)條件插敘,兩個(gè)都不存在時(shí)查詢?nèi)俊?/p>
<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標(biāo)簽
trim標(biāo)簽的屬性:
● prefix:在trim標(biāo)簽中的語句前添加內(nèi)容
● suffix:在trim標(biāo)簽中的語句后添加內(nèi)容
● 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:集合或數(shù)組,如傳進(jìn)來參數(shù)的名稱
item:集合或數(shù)組中的元素
separator:分隔符 ,如’,’
open:foreach標(biāo)簽中所有內(nèi)容的開始
close:foreach標(biāo)簽中所有內(nèi)容的結(jié)束
循環(huán)數(shù)組或集合,動(dòng)態(tài)生成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標(biāo)簽與include標(biāo)簽
sql標(biāo)簽用來聲明sql片段
include標(biāo)簽用來將聲明的sql片段包含到某個(gè)sql語句當(dāng)中
作用:代碼復(fù)用。易維護(hù)。
<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);
傳入一個(gè)String數(shù)組,通過這個(gè)數(shù)組來查詢活動(dòng),返回一個(gè)Activity活動(dòng)集合
<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>
注意事項(xiàng):
1、parameterType為string類型是因?yàn)閿?shù)組中的每一個(gè)元素都是string類型的,resultMap對(duì)應(yīng)寫好的數(shù)據(jù)庫字段和實(shí)體類屬性的映射id
2、多表聯(lián)查,join on 后面的on字段是主表屬性在前
3、傳進(jìn)來的參數(shù)用#{}括起來,括號(hào)內(nèi)的值和傳進(jìn)來的dao層方法的參數(shù)名相同。可以使用@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>
注意事項(xiàng)
1、parameterType是傳進(jìn)來的參數(shù)類型是map,因此是map 2、這里涉及到了模糊查詢,模糊查詢這里使用的是
like '%' #{參數(shù)} '%'
SQL中IF函數(shù)的使用
語法:IF(expr1,expr2,expr3)
expr1 的值為 TRUE,則返回值為 expr2
expr1 的值為FALSE,則返回值為 expr3
其中,expr1是判斷條件,expr2和expr3是符合expr1的自定義的返回結(jié)果。
select
if(il.status_id = 'INV_STTS_AVAILABLE','全新','二手') as status_id
from inventory_location as il;
求有效期
在查詢的時(shí)候的select后面使用
有效期是當(dāng)前時(shí)間到終止時(shí)間(大于0則顯示,否則<0是默認(rèn)為0)
IF(TIMESTAMPDIFF(DAY,NOW(),t.end_date) >0 ,TIMESTAMPDIFF(DAY,NOW(),t.end_date),0) as effictiveDay
知識(shí)點(diǎn)講解----------------------時(shí)間差函數(shù):TIMESTAMPDIFF() -----------------------------------------
TIMESTAMPDIFF() 是 MySQL 中用來計(jì)算兩個(gè)日期或時(shí)間之間的差值的函數(shù)。該函數(shù)返回兩個(gè)日期/時(shí)間之間的差值,可以指定差值的單位(秒、分鐘、小時(shí)、天等)。TIMESTAMPDIFF() 函數(shù)的語法如下:
TIMESTAMPDIFF(unit, start_date, end_date)
參數(shù)說明:
unit:表示差值的單位,可以是以下值之一:MICROSECOND(微秒)、SECOND(秒)、MINUTE(分)、HOUR(小時(shí))、DAY(天)、WEEK(周)、MONTH(月)、QUARTER(季度)或 YEAR(年)。
start_date:表示時(shí)間段的起始時(shí)間。
end_date:表示時(shí)間段的結(jié)束時(shí)間。
插入
刪除
新增
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>
注意事項(xiàng):
1、因?yàn)椴迦氲脑孛恳粋€(gè)都是Activity類型的,因此parameterType是Activity類型的 因?yàn)檫@個(gè)是inset into
表名(列名1,列名2,…)
values(?,?,…)(?,?,…),批量插入,因此這里使用了foreach標(biāo)簽,遍歷整個(gè)集合,在values后面給占位符賦值。
Sql中的關(guān)鍵字
GROUP_CONCAT
分析當(dāng)前spu有多少個(gè)sku,所有sku涉及到的屬性組合?
1個(gè)spu有多個(gè)sku,sku有多個(gè)屬性組合,應(yīng)該是一個(gè)屬性id,一個(gè)屬性的名稱,對(duì)應(yīng)有多少屬性。
比如說品牌是attr,他的名字是品牌,他的值可以是小米,華為,蘋果等。
實(shí)現(xiàn)一對(duì)多的展現(xiàn)結(jié)果,使用GROUP_CONCAT
關(guān)鍵字。
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
現(xiàn)在公司出臺(tái)了一個(gè)奇葩的規(guī)定
對(duì)當(dāng)前工資為 1 萬以上的員工,降薪 10%。
對(duì)當(dāng)前工資低于 1 萬的員工,加薪 20%。文章來源:http://www.zghlxwxcb.cn/news/detail-498243.html
需要使用case when文章來源地址http://www.zghlxwxcb.cn/news/detail-498243.html
到了這里,關(guān)于編寫dao層xml文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!