亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

mybatis?mapper.xml中如何根據(jù)數(shù)據(jù)庫類型選擇對(duì)應(yīng)SQL語句

 更新時(shí)間:2022年01月20日 12:11:34   作者:qq_30210697  
這篇文章主要介紹了mybatis?mapper.xml中如何根據(jù)數(shù)據(jù)庫類型選擇對(duì)應(yīng)SQL語句,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

mapper.xml根據(jù)數(shù)據(jù)庫類型選擇對(duì)應(yīng)SQL語句

1、spring-database.xml文件中配置

? <bean id="vendorProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
? ? <property name="properties">
? ? ? <props>
? ? ? ? <prop key="DB2">db2</prop>
? ? ? ? <prop key="Oracle">oracle</prop>
? ? ? ? <prop key="MySQL">mysql</prop>
? ? ? </props>
? ? </property>
? ?</bean>
? ?<bean id="databaseIdProvider" class="org.apache.ibatis.mapping.VendorDatabaseIdProvider">
? ? <property name="properties" ref="vendorProperties"/>
? </bean>

對(duì)于sessionFactory的配置,主要是標(biāo)紅的語句一定要有,其它按照自己原有的配置走。

<!-- sessionFactory 將spring和mybatis整合 -->
?<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
?<property name="dataSource" ref="dataSource" />
?<property name="databaseIdProvider" ref="databaseIdProvider" />
?<property name="configLocation" value="classpath:mybatis-config.xml" />
?<property name="mapperLocations"
?value="classpath*:/com/sunyard/cop/IF/mybatis/mapping/*.xml" />?
?<property name="plugins">
? ? ?<array>
? ? ? ?<bean class="com.github.pagehelper.PageInterceptor">
? ? ? ? ?<property name="properties">
? ? ? ? ? ?<!--使用下面的方式配置參數(shù),一行配置一個(gè),后面會(huì)有所有的參數(shù)介紹 -->
? ? ? ? ? ?<value>
?        helperDialect=oracle
?        reasonable=true
?        supportMethodsArguments=true
?        params=count=countSql
?        autoRuntimeDialect=true?
?      </value>
? ? ? ? ?</property>
? ? ? ?</bean>
? ? ?</array>
? ? ? ? ?</property>
?</bean>

2、mapper.xml文件中配置

? ? <select id="selectByUserNo" databaseId="mysql" ? parameterType="java.lang.String" resultMap="UserResultMap">
?select * from SM_USERS_TB
? ? </select>
? ? <select id="selectByUserNo" ?parameterType="java.lang.String" resultMap="UserResultMap">
?select * from SM_USERS_TB
? ? </select>

若寫上databaseId = "mysql",則在數(shù)據(jù)源為mysql類型時(shí),自動(dòng)執(zhí)行該SQL語句,若不寫databaseId ,且同時(shí)存在相同ID的SQL語句,則只要是非mysql數(shù)據(jù)庫的數(shù)據(jù)源,都會(huì)調(diào)用該條SQL語句。

mapper.xml動(dòng)態(tài)SQL語句用法

用于實(shí)現(xiàn)動(dòng)態(tài)SQL的元素主要有

if

用于判斷  示例

<update id="upda" parameterType="User">
		update smbms_user
		<set>
                <!--修改時(shí)可以判斷userCode是否是空的如果不為空就把數(shù)據(jù)庫中這一列的值更改掉
                如果為空就不修改這一列數(shù)據(jù)庫這一列的值也不會(huì)為Null-->
			<if test="userCode!=null and userCode!=''">
				userCode=#{userCode},
			</if>
			<if test="userName!=null and userName!=''">
				userName=#{userName},
			</if>
			<if test="userPassword!=null and userPassword!=''">
				userPassword=#{userPassword},
			</if>
			<if test="gender!=null and gender!=''">
				gender=#{gender},
			</if>
			<if test="phone!=null and phone!=''">
				phone=#{phone},
			</if>
			<if test="address!=null and address!=''">
				address=#{address},
			</if>
			<if test="userRole!=null and userRole!=''">
				userRole=#{userRole},
			</if>
			<if test="createdBy!=null and createdBy!=''">
				createdBy=#{createdBy},
			</if>
		</set>
		where id=#{id}
	</update>

trim

  • trim 屬性 prefix suffix prefixOverrides suffixOverrides 更靈活地去除多余關(guān)鍵字 替代where和set
  • if+trim 使用if+trim替代if+set進(jìn)行更新用戶表數(shù)據(jù),效果一樣的 如下:
<update id ="modify" parameterType="User">
update smbms_user
<trim prefix="set" suffixOverrides="," suffix="where id = #{id}">	
	<if test="userCode != null">userCode = #{userCode},</if>
	<if test="userName!= null">userCode = #{userName },</if>
	<if test="userPassword!= null">userPassword=#{userPassword },</if>
</trim>
</update>
 

其中:

  • prefix表示有一個(gè)if成立則插入where語句
  • suffix表示后綴,插入到最后,與perfix正好相反
  • suffixOverrides="xxx"表示如果最后生成的sql語句多一個(gè)xxx,則自動(dòng)去掉
  • prefixOverrides的意思是去掉前綴,和suffixOverrides的使用正好相反

這里如果任意一個(gè)<if>條件為true,<trim>元素會(huì)插入WHERE,并且移除緊跟where后面的(and或or) 

where

    
SELECT u.*,r.roleName,r.roleCode FROM smbms_user u INNER JOIN smbms_role r ON u.userrole=r.id 
    <where>
            <!-- where相當(dāng)于  select * from pet where id=1 中的where一樣  -->
			<if test="usercode !=null and usercode !=''">
				AND userCode LIKE CONCAT('%',#{usercode},'%')
			</if>
			<if test="userrole!=0">
				AND userRole=#{userrole}
			</if>
			<if test="gender!=null and gender!=0">
				AND gender=#{gender} 
			</if>
	</where>

set

<update id="upda" parameterType="User">
		update smbms_user
		<set><!-- 與修改時(shí)搭配使用我們修改set的sql語句的‘,'的最后一列會(huì)被自動(dòng)省略 -->
			<if test="userCode!=null and userCode!=''">
				userCode=#{userCode},
			</if>
			<if test="userName!=null and userName!=''">
				userName=#{userName},
			</if>
			<if test="userPassword!=null and userPassword!=''">
				userPassword=#{userPassword},
			</if>
			<if test="gender!=null and gender!=''">
				gender=#{gender},
			</if>
			<if test="phone!=null and phone!=''">
				phone=#{phone},
			</if>
			<if test="address!=null and address!=''">
				address=#{address},
			</if>
			<if test="userRole!=null and userRole!=''">
				userRole=#{userRole},
			</if>
			<if test="createdBy!=null and createdBy!=''">
				createdBy=#{createdBy},
			</if>
		</set>
		where id=#{id}
	</update>

choose(when、otherwise)

相當(dāng)于Java中switch語句 當(dāng)when有條件滿足的時(shí)候,就跳出choose

<choose>
	<when test ="條件1"> …</when>
	<when test ="條件2"> …</when>
	<when test ="條件3"> …</when>
	…
	<otherwise>…</otherwise>
</choose>	

foreach

迭代一個(gè)集合,通常用于in條件 屬性 item index collection:必須指定 list array map-key open separator close

<select id="getUserName" resultType="User" parameterType="java.util.List">
		select * from smbms_user where userCode in
		<foreach collection="list" open="(" close=")" item="userCode" separator=",">
			#{userCode}
		</foreach>
	</select>

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java自然排序Comparable使用方法解析

    Java自然排序Comparable使用方法解析

    這篇文章主要介紹了Java自然排序Comparable使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 詳解SpringBoot 添加對(duì)JSP的支持(附常見坑點(diǎn))

    詳解SpringBoot 添加對(duì)JSP的支持(附常見坑點(diǎn))

    這篇文章主要介紹了詳解SpringBoot 添加對(duì)JSP的支持(附常見坑點(diǎn)),非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-10-10
  • Spring Boot與RabbitMQ結(jié)合實(shí)現(xiàn)延遲隊(duì)列的示例

    Spring Boot與RabbitMQ結(jié)合實(shí)現(xiàn)延遲隊(duì)列的示例

    本篇文章主要介紹了Spring Boot與RabbitMQ結(jié)合實(shí)現(xiàn)延遲隊(duì)列的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • SpringBoot中的@ResponseStatus注解處理異常狀態(tài)碼

    SpringBoot中的@ResponseStatus注解處理異常狀態(tài)碼

    這篇文章主要介紹了SpringBoot中的@ResponseStatus注解處理異常狀態(tài)碼,在?SpringBoot?應(yīng)用程序中,異常處理是一個(gè)非常重要的話題。當(dāng)應(yīng)用程序出現(xiàn)異常時(shí),我們需要對(duì)異常進(jìn)行處理,以保證應(yīng)用程序的穩(wěn)定性和可靠性,需要的朋友可以參考下
    2023-08-08
  • 關(guān)于SpringBoot中的請(qǐng)求映射及使用

    關(guān)于SpringBoot中的請(qǐng)求映射及使用

    這篇文章主要介紹了關(guān)于SpringBoot中的請(qǐng)求映射及使用,Spring Boot 中的授權(quán)機(jī)制,包括基于角色的授權(quán)和基于資源的授權(quán),同時(shí),我們也將給出相應(yīng)的代碼示例,幫助讀者更好地理解和應(yīng)用這些授權(quán)機(jī)制,需要的朋友可以參考下
    2023-07-07
  • Nacos-SpringBoot框架啟動(dòng)不加載bootstrap.yml的解決

    Nacos-SpringBoot框架啟動(dòng)不加載bootstrap.yml的解決

    這篇文章主要介紹了Nacos-SpringBoot框架啟動(dòng)不加載bootstrap.yml的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 微信小程序 開發(fā)中遇到問題總結(jié)

    微信小程序 開發(fā)中遇到問題總結(jié)

    這篇文章主要介紹了微信小程序 開發(fā)中遇到問題總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Java 線程池ExecutorService詳解及實(shí)例代碼

    Java 線程池ExecutorService詳解及實(shí)例代碼

    這篇文章主要介紹了Java 線程池ExecutorService詳解及實(shí)例代碼的相關(guān)資料,線程池減少在創(chuàng)建和銷毀線程上所花的時(shí)間以及系統(tǒng)資源的開銷.如果不使用線程池,有可能造成系統(tǒng)創(chuàng)建大量線程而導(dǎo)致消耗系統(tǒng)內(nèi)存以及”過度切換“
    2016-11-11
  • MyBatis之一級(jí)緩存和二級(jí)緩存問題

    MyBatis之一級(jí)緩存和二級(jí)緩存問題

    這篇文章主要介紹了MyBatis之一級(jí)緩存和二級(jí)緩存問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • SpringMVC+Mybatis二維碼實(shí)現(xiàn)多平臺(tái)付款(附源碼)

    SpringMVC+Mybatis二維碼實(shí)現(xiàn)多平臺(tái)付款(附源碼)

    本文主要實(shí)現(xiàn)微信支付寶等支付平臺(tái)合多為一的二維碼支付,并且實(shí)現(xiàn)有效時(shí)間內(nèi)支付有效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08

最新評(píng)論