最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

mapper.xml根據(jù)數(shù)據(jù)庫類型選擇對應(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>

對于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ù),一行配置一個,后面會有所有的參數(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類型時,自動執(zhí)行該SQL語句,若不寫databaseId ,且同時存在相同ID的SQL語句,則只要是非mysql數(shù)據(jù)庫的數(shù)據(jù)源,都會調(diào)用該條SQL語句。

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

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

if

用于判斷  示例

<update id="upda" parameterType="User">
		update smbms_user
		<set>
                <!--修改時可以判斷userCode是否是空的如果不為空就把數(shù)據(jù)庫中這一列的值更改掉
                如果為空就不修改這一列數(shù)據(jù)庫這一列的值也不會為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表示有一個if成立則插入where語句
  • suffix表示后綴,插入到最后,與perfix正好相反
  • suffixOverrides="xxx"表示如果最后生成的sql語句多一個xxx,則自動去掉
  • prefixOverrides的意思是去掉前綴,和suffixOverrides的使用正好相反

這里如果任意一個<if>條件為true,<trim>元素會插入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><!-- 與修改時搭配使用我們修改set的sql語句的‘,'的最后一列會被自動省略 -->
			<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有條件滿足的時候,就跳出choose

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

foreach

迭代一個集合,通常用于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>

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

相關(guān)文章

  • Java?NIO?Buffer實(shí)現(xiàn)原理詳解

    Java?NIO?Buffer實(shí)現(xiàn)原理詳解

    本篇文章主要對NIO核心三件套:緩沖區(qū)(Buffer)、選擇器?(Selector)和通道(Channel),其中之一的緩沖區(qū)Buffer實(shí)現(xiàn)原理的學(xué)習(xí)總結(jié)。感興趣的小伙伴可以了解一下
    2021-11-11
  • 淺談MyBatis循環(huán)Map(高級用法)

    淺談MyBatis循環(huán)Map(高級用法)

    這篇文章主要介紹了淺談MyBatis循環(huán)Map(高級用法),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java中Properties類的操作實(shí)例詳解

    Java中Properties類的操作實(shí)例詳解

    這篇文章主要介紹了Java中Properties類的操作實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 聊聊drools?session的不同

    聊聊drools?session的不同

    在drools中存在2種session,一種是有狀態(tài)的Session (Stateful Session),另外一種一種是無狀態(tài)的Session (Stateless Session,本文通過實(shí)例代碼給大家介紹drools?session的不同,感興趣的朋友一起看看吧
    2022-05-05
  • Spring的@Value如何從Nacos配置中心獲取值并自動刷新

    Spring的@Value如何從Nacos配置中心獲取值并自動刷新

    這篇文章主要介紹了Spring的@Value如何從Nacos配置中心獲取值并自動刷新,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 最新評論

    怀化市| 山阴县| 建德市| 新竹市| 荔波县| 和平县| 襄汾县| 张家界市| 云龙县| 松溪县| 福贡县| 望江县| 花莲县| 建宁县| 淮南市| 扶绥县| 陕西省| 通化县| 津市市| 蓝田县| 宜兰市| 滨海县| 呈贡县| 鄂伦春自治旗| 美姑县| 巴林右旗| 大新县| 灌云县| 奈曼旗| 呈贡县| 瑞安市| 永登县| 沂源县| 湾仔区| 扶余县| 石嘴山市| 临桂县| 彭水| 鄢陵县| 游戏| 阜城县|