mybatis?mapper.xml中如何根據(jù)數(shù)據(jù)庫(kù)類型選擇對(duì)應(yīng)SQL語(yǔ)句
mapper.xml根據(jù)數(shù)據(jù)庫(kù)類型選擇對(duì)應(yīng)SQL語(yǔ)句
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)紅的語(yǔ)句一定要有,其它按照自己原有的配置走。
<!-- 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語(yǔ)句,若不寫databaseId ,且同時(shí)存在相同ID的SQL語(yǔ)句,則只要是非mysql數(shù)據(jù)庫(kù)的數(shù)據(jù)源,都會(huì)調(diào)用該條SQL語(yǔ)句。
mapper.xml動(dòng)態(tài)SQL語(yǔ)句用法

用于實(shí)現(xiàn)動(dòng)態(tài)SQL的元素主要有
if
用于判斷 示例
<update id="upda" parameterType="User">
update smbms_user
<set>
<!--修改時(shí)可以判斷userCode是否是空的如果不為空就把數(shù)據(jù)庫(kù)中這一列的值更改掉
如果為空就不修改這一列數(shù)據(jù)庫(kù)這一列的值也不會(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語(yǔ)句suffix表示后綴,插入到最后,與perfix正好相反suffixOverrides="xxx"表示如果最后生成的sql語(yǔ)句多一個(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語(yǔ)句的‘,'的最后一列會(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語(yǔ)句 當(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)文章
IDEA創(chuàng)建Java項(xiàng)目文件并運(yùn)行教程解析
這篇文章主要介紹了IDEA創(chuàng)建Java項(xiàng)目文件并運(yùn)行教程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
SpringBoot+Redis執(zhí)行l(wèi)ua腳本的方法步驟
這篇文章主要介紹了SpringBoot+Redis執(zhí)行l(wèi)ua腳本的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Java特性?Lambda?表達(dá)式和函數(shù)式接口
這篇文章主要介紹了Java特性?Lambda?表達(dá)式和函數(shù)式接口,Lambda表達(dá)式基于函數(shù)式編程思想,也可以稱為閉包,是Java?8引入的重要新特性,?Lambda允許把函數(shù)作為一個(gè)方法的參數(shù)2022-06-06
Java?NIO?Buffer實(shí)現(xiàn)原理詳解
淺談MyBatis循環(huán)Map(高級(jí)用法)
Spring的@Value如何從Nacos配置中心獲取值并自動(dòng)刷新

