国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

編寫dao層xml文件

這篇具有很好參考價(jià)值的文章主要介紹了編寫dao層xml文件。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

導(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`

編寫dao層xml文件

@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%。

需要使用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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • MyBatis的XML映射文件

    MyBatis的XML映射文件

    目錄 2.1 XML配置文件規(guī)范 在Mybatis中使用XML映射文件方式開發(fā),需要符合一定的規(guī)范 - XML映射文件定義規(guī)范: 2.2 XML配置文件實(shí)現(xiàn) 第1步:創(chuàng)建XML映射文件 第2步:編寫XML映射文件 2.3 MybatisX的使用 總結(jié): Mybatis的開發(fā)有兩種方式: 注解 XML配置文件 通過XML配置文件的形式來配置

    2024年02月13日
    瀏覽(19)
  • 文件導(dǎo)入以及在線編輯-xml案例

    文件導(dǎo)入以及在線編輯-xml案例

    ? ? 在線編輯器 參考?http://t.csdn.cn/uSgnf ? 在components下創(chuàng)建一個(gè)文件 叫 editor 然后 在下面創(chuàng)建一個(gè)js文件 叫 data_format_utils data_format_utils.js 參考代碼如下 用來格式化 vkBeautify - javascript plugin 然后在 editor目錄下創(chuàng)建一個(gè)組件 我這里叫 index.vue 參考代碼如下 然后在main.js全局引入

    2024年02月13日
    瀏覽(19)
  • mybatis(mybatis-plus)映射文件(XML文件)中特殊字符轉(zhuǎn)義

    XML 文件在解析時(shí)會(huì)將五種特殊字符進(jìn)行轉(zhuǎn)義,當(dāng)不希望語法被轉(zhuǎn)義時(shí),就需要進(jìn)行特別處理,參考HTML字符轉(zhuǎn)義規(guī)范Myabtis 中五個(gè)特殊字符 原始符號(hào) 符號(hào)含義 大于 小于 and \\\" 英文雙引號(hào) ’ 英文單引號(hào) 原始符號(hào) 轉(zhuǎn)義字符 gt lt amp \\\" quot ’ apos

    2024年02月10日
    瀏覽(35)
  • MyBatis的使用(XML映射文件)

    MyBatis基于注解開發(fā)簡(jiǎn)單便捷,但是弊端是失去SQL語句的靈活性,不能根據(jù)實(shí)際情況產(chǎn)生不同的SQL語句 MyBatis除了支持注解開發(fā)以外,還支持一種開發(fā)方式:XML映射文件,將SQL語句寫到XML映射文件中,基于更多種的選擇可以讓SQL變得更加靈活 1.開發(fā)方式 1.和基于注解開發(fā)方式一

    2024年02月08日
    瀏覽(26)
  • MyBatis-XML映射文件

    MyBatis-XML映射文件

    規(guī)范 XML映射文件的名稱與Mapper接口名稱一致(EmpMapper對(duì)應(yīng)EmpMpper.xml),并且將XML映射文件和Mapper接口放置在相同包下(同包同名) ??? 在maven項(xiàng)目結(jié)構(gòu)中所有的配置文件都在resources目錄之下,因此要在該目錄下創(chuàng)建Mapper接口相同的文件目錄,注意在創(chuàng)建目錄時(shí)要使用 / 來

    2024年02月13日
    瀏覽(35)
  • Mybatis—XML配置文件、動(dòng)態(tài)SQL

    Mybatis—XML配置文件、動(dòng)態(tài)SQL

    學(xué)習(xí)完Mybatis的基本操作之后,繼續(xù)學(xué)習(xí)Mybatis—XML配置文件、動(dòng)態(tài)SQL。 Mybatis的開發(fā)有兩種方式: 注解 XML 之前學(xué)習(xí)的基本操作都是基于注解開發(fā)。使用Mybatis的注解方式,主要是來完成一些簡(jiǎn)單的增刪改查功能。如果需要實(shí)現(xiàn)復(fù)雜的SQL功能,建議使用XML來配置映射語句,也就

    2024年02月06日
    瀏覽(22)
  • Mybatis|mapper配置文件xml位置

    Mybatis|mapper配置文件xml位置

    在核心配置文件mybatis-config.xml中設(shè)置映射文件位置 application.yml文件中添加配置: mybatis案例中和springboot中都是一樣的,只要目錄名和包名相同 需要在pom.xml中添加如下內(nèi)容 越努力,越幸運(yùn)! codefishyyf與你一起努力!

    2024年02月06日
    瀏覽(25)
  • MyBatis運(yùn)行找不到xml資源文件

    MyBatis運(yùn)行找不到xml資源文件

    程序運(yùn)行后,沒有將 src/main/java 目錄下的資源文件(xml、properties等等)導(dǎo)出到 target工作目錄下,所以程序找不到 java目錄: 運(yùn)行后的target目錄: 可以看到并沒有 MonsterMapper.xml文件 Maven項(xiàng)目在 pom.xml 文件中配置 將Maven項(xiàng)目clean一下, 運(yùn)行后 target目錄就會(huì)生成xml文件 如果還是不行

    2023年04月23日
    瀏覽(26)
  • IDEA創(chuàng)建Mybatis格式XML文件

    IDEA創(chuàng)建Mybatis格式XML文件

    設(shè)置位置:File | Settings | Editor | File and Code Templates 選擇Files,點(diǎn)擊+號(hào) Name中輸入xml模板名(名稱自行決定),后綴名extension輸入xml(固定) 內(nèi)容處輸入Mybatis的xml文件模板內(nèi)容,勾選Enable Live Template,點(diǎn)擊應(yīng)用 完成后,在需要xml文件處新建,選擇設(shè)置好的xml名稱,按需補(bǔ)充內(nèi)容

    2024年02月12日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包