mybatis mapper.xml中如何根据数据库类型选择对应SQL语句
更新时间:2022年1月20日 13:15 点击:357 作者: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
trim
属性 prefix suffix prefixOverrides suffixOverrides 更灵活地去除多余关键字 替代where和setif+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>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。
原文出处:https://blog.csdn.net/qq_30210697/article/details/79917271
相关文章
- 操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了...2016-11-25
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 这篇文章主要介绍了Mybatis Plus select 实现只查询部分字段的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-01
- 这篇文章主要介绍了MySQL性能监控软件Nagios的安装及配置教程,这里以CentOS操作系统为环境进行演示,需要的朋友可以参考下...2015-12-14
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 这篇文章主要介绍了PostgreSQL判断字符串是否包含目标字符串的多种方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-23
- 这篇文章主要介绍了解决Mybatis 大数据量的批量insert问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-09
- 这篇文章主要介绍了Intellij IDEA连接Navicat数据库的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借价值,需要的朋友可以参考下...2021-03-25
- 这篇文章主要介绍了mybatis的Configuration详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-04
- 在开发过程中,我们经常会将日期时间的毫秒数存放到数据库,但是它对应的时间看起来就十分不方便,我们可以使用一些函数将毫秒转换成date格式。 一、 在MySQL中,有内置的函数from_unixtime()来做相应的转换,使用如下: 复制...2014-05-31
- 这篇文章主要介绍了PostgreSQL TIMESTAMP类型 时间戳操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-26
- 新版 Mysql 中加入了对 JSON Document 的支持,可以创建 JSON 类型的字段,并有一套函数支持对JSON的查询、修改等操作,下面就实际体验一下...2016-08-23
- C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
mybatis 返回Integer,Double,String等类型的数据操作
这篇文章主要介绍了mybatis 返回Integer,Double,String等类型的数据操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-25- 这篇文章主要介绍了postgresql 实现多表关联删除操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-02
- 通过内网连另外一台机器的mysql服务, 确发现速度N慢! 等了大约几十秒才等到提示输入密码。 但是ping mysql所在服务器却很快! 想到很久之前有过类似的经验, telnet等一些服务在连接请求的时候,会做一些反向域名解析(如果...2015-10-21
- 这篇文章主要介绍了Postgresl 如何选择正确的关闭模式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-18
- 某些时候,例如为了搭建一个测试环境,或者克隆一个网站,需要复制一个已存在的mysql数据库。使用以下方法,可以非常简单地实现。假设已经存在的数据库名字叫db1,想要复制一份,命名为newdb。步骤如下:1. 首先创建新的数据库newd...2015-10-21
- mysqldump命令的用法1、导出所有库系统命令行mysqldump -uusername -ppassword --all-databases > all.sql 2、导入所有库mysql命令行mysql>source all.sql; 3、导出某些库系统命令行mysqldump -uusername -ppassword...2015-10-21
深入研究mysql中的varchar和limit(容易被忽略的知识)
为什么标题要起这个名字呢?commen sence指的是那些大家都应该知道的事情,但往往大家又会会略这些东西,或者对这些东西一知半解,今天我总结下自己在mysql中遇到的一些commen sense类型的问题。 ...2015-03-15