mybatis?mapper.xml中如何根據(jù)數(shù)據(jù)庫類型選擇對(duì)應(yīng)SQL語句
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和setif+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)文章
詳解SpringBoot 添加對(duì)JSP的支持(附常見坑點(diǎn))
這篇文章主要介紹了詳解SpringBoot 添加對(duì)JSP的支持(附常見坑點(diǎn)),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10Spring Boot與RabbitMQ結(jié)合實(shí)現(xiàn)延遲隊(duì)列的示例
本篇文章主要介紹了Spring Boot與RabbitMQ結(jié)合實(shí)現(xiàn)延遲隊(duì)列的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11SpringBoot中的@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)求映射及使用,Spring Boot 中的授權(quán)機(jī)制,包括基于角色的授權(quán)和基于資源的授權(quán),同時(shí),我們也將給出相應(yīng)的代碼示例,幫助讀者更好地理解和應(yīng)用這些授權(quán)機(jī)制,需要的朋友可以參考下2023-07-07Nacos-SpringBoot框架啟動(dòng)不加載bootstrap.yml的解決
這篇文章主要介紹了Nacos-SpringBoot框架啟動(dòng)不加載bootstrap.yml的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Java 線程池ExecutorService詳解及實(shí)例代碼
這篇文章主要介紹了Java 線程池ExecutorService詳解及實(shí)例代碼的相關(guān)資料,線程池減少在創(chuàng)建和銷毀線程上所花的時(shí)間以及系統(tǒng)資源的開銷.如果不使用線程池,有可能造成系統(tǒng)創(chuàng)建大量線程而導(dǎo)致消耗系統(tǒng)內(nèi)存以及”過度切換“2016-11-11SpringMVC+Mybatis二維碼實(shí)現(xiàn)多平臺(tái)付款(附源碼)
本文主要實(shí)現(xiàn)微信支付寶等支付平臺(tái)合多為一的二維碼支付,并且實(shí)現(xiàn)有效時(shí)間內(nèi)支付有效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08