首页 > 编程技术 > java

mybatis mapper.xml中如何根据数据库类型选择对应SQL语句

发布时间:2022-1-20 13:15 作者:qq_30210697

mapper.xml根据数据库类型选择对应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的配置,主要是标红的语句一定要有,其它按照自己原有的配置走。

<!-- 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">
           <!--使用下面的方式配置参数,一行配置一个,后面会有所有的参数介绍 -->
           <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",则在数据源为mysql类型时,自动执行该SQL语句,若不写databaseId ,且同时存在相同ID的SQL语句,则只要是非mysql数据库的数据源,都会调用该条SQL语句。

mapper.xml动态SQL语句用法

用于实现动态SQL的元素主要有

if

用于判断  示例

<update id="upda" parameterType="User">
		update smbms_user
		<set>
                <!--修改时可以判断userCode是否是空的如果不为空就把数据库中这一列的值更改掉
                如果为空就不修改这一列数据库这一列的值也不会为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

<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>
 

其中:

这里如果任意一个<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相当于  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)

相当于Java中switch语句 当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>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。

原文出处:https://blog.csdn.net/qq_30210697/article/details/79917271

标签:[!--infotagslink--]

您可能感兴趣的文章: