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語句用法
用于實現(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和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
表示有一個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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解SpringBoot 添加對JSP的支持(附常見坑點(diǎn))
這篇文章主要介紹了詳解SpringBoot 添加對JSP的支持(附常見坑點(diǎn)),非常具有實用價值,需要的朋友可以參考下2017-10-10Spring Boot與RabbitMQ結(jié)合實現(xiàn)延遲隊列的示例
本篇文章主要介紹了Spring Boot與RabbitMQ結(jié)合實現(xiàn)延遲隊列的示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11SpringBoot中的@ResponseStatus注解處理異常狀態(tài)碼
這篇文章主要介紹了SpringBoot中的@ResponseStatus注解處理異常狀態(tài)碼,在?SpringBoot?應(yīng)用程序中,異常處理是一個非常重要的話題。當(dāng)應(yīng)用程序出現(xiàn)異常時,我們需要對異常進(jìn)行處理,以保證應(yīng)用程序的穩(wěn)定性和可靠性,需要的朋友可以參考下2023-08-08Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決
這篇文章主要介紹了Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11Java 線程池ExecutorService詳解及實例代碼
這篇文章主要介紹了Java 線程池ExecutorService詳解及實例代碼的相關(guān)資料,線程池減少在創(chuàng)建和銷毀線程上所花的時間以及系統(tǒng)資源的開銷.如果不使用線程池,有可能造成系統(tǒng)創(chuàng)建大量線程而導(dǎo)致消耗系統(tǒng)內(nèi)存以及”過度切換“2016-11-11SpringMVC+Mybatis二維碼實現(xiàn)多平臺付款(附源碼)
本文主要實現(xiàn)微信支付寶等支付平臺合多為一的二維碼支付,并且實現(xiàn)有效時間內(nèi)支付有效,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08