温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2022-01-23 14:37:23 来源:亿速云 阅读:474 作者:小新 栏目:开发技术

这篇“mybatis mapper.xml中怎么根据数据库类型选择对应SQL语句”文章,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要参考一下,对于“mybatis mapper.xml中怎么根据数据库类型选择对应SQL语句”,小编整理了以下知识点,请大家跟着小编的步伐一步一步的慢慢理解,接下来就让我们进入主题吧。

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语句用法

mybatis 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

  • trim 属性 prefix suffix prefixOverrides suffixOverrides 更灵活地去除多余关键字 替代where和set

  • if+trim 使用if+trim替代if+set进行更新用户表数据,效果一样的 如下:

<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相当于  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>

以上是“mybatis mapper.xml中怎么根据数据库类型选择对应SQL语句”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI