mybatis mapper.xml中怎么根据数据库类型选择对应SQL语句
导读:本文共4564.5字符,通常情况下阅读需要15分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: mapper.xml根据数据库类型选择对应SQL语句1、spring-database.xml文件中配置<beanid="vendorProperties"class="org.springframework.beans.factory.config.PropertiesFactoryBean"><pr... ...
目录
(为您整理了一些要点),点击可以直达。mapper.xml根据数据库类型选择对应SQL语句
1、spring-database.xml文件中配置
<beanid="vendorProperties"class="org.springframework.beans.factory.config.PropertiesFactoryBean"><propertyname="properties"><props><propkey="DB2">db2</prop><propkey="Oracle">oracle</prop><propkey="MySQL">mysql</prop></props></property></bean><beanid="databaseIdProvider"class="org.apache.ibatis.mapping.VendorDatabaseIdProvider"><propertyname="properties"ref="vendorProperties"/></bean>
对于sessionFactory的配置,主要是标红的语句一定要有,其它按照自己原有的配置走。
<!--sessionFactory将spring和mybatis整合--><beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><propertyname="databaseIdProvider"ref="databaseIdProvider"/><propertyname="configLocation"value="classpath:mybatis-config.xml"/><propertyname="mapperLocations"value="classpath*:/com/sunyard/cop/IF/mybatis/mapping/*.xml"/><propertyname="plugins"><array><beanclass="com.github.pagehelper.PageInterceptor"><propertyname="properties"><!--使用下面的方式配置参数,一行配置一个,后面会有所有的参数介绍--><value> helperDialect=oracle reasonable=true supportMethodsArguments=true params=count=countSql autoRuntimeDialect=true </value></property></bean></array></property></bean>
2、mapper.xml文件中配置
<selectid="selectByUserNo"databaseId="mysql"parameterType="java.lang.String"resultMap="UserResultMap">select*fromSM_USERS_TB</select><selectid="selectByUserNo"parameterType="java.lang.String"resultMap="UserResultMap">select*fromSM_USERS_TB</select>
若写上databaseId = "mysql",则在数据源为mysql类型时,自动执行该SQL语句,若不写databaseId ,且同时存在相同ID的SQL语句,则只要是非mysql数据库的数据源,都会调用该条SQL语句。
mapper.xml动态SQL语句用法
用于实现动态SQL的元素主要有
if
用于判断 示例
<updateid="upda"parameterType="User"> updatesmbms_user <set><!--修改时可以判断userCode是否是空的如果不为空就把数据库中这一列的值更改掉如果为空就不修改这一列数据库这一列的值也不会为Null--> <iftest="userCode!=nullanduserCode!=''"> userCode=#{userCode}, </if> <iftest="userName!=nullanduserName!=''"> userName=#{userName}, </if> <iftest="userPassword!=nullanduserPassword!=''"> userPassword=#{userPassword}, </if> <iftest="gender!=nullandgender!=''"> gender=#{gender}, </if> <iftest="phone!=nullandphone!=''"> phone=#{phone}, </if> <iftest="address!=nullandaddress!=''"> address=#{address}, </if> <iftest="userRole!=nullanduserRole!=''"> userRole=#{userRole}, </if> <iftest="createdBy!=nullandcreatedBy!=''"> createdBy=#{createdBy}, </if> </set> whereid=#{id} </update>
trim
trim
属性 prefix suffix prefixOverrides suffixOverrides 更灵活地去除多余关键字 替代where和setif+trim
使用if+trim替代if+set进行更新用户表数据,效果一样的如下:
<updateid="modify"parameterType="User">updatesmbms_user<trimprefix="set"suffixOverrides=","suffix="whereid=#{id}"> <iftest="userCode!=null">userCode=#{userCode},</if> <iftest="userName!=null">userCode=#{userName},</if> <iftest="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
SELECTu.*,r.roleName,r.roleCodeFROMsmbms_useruINNERJOINsmbms_rolerONu.userrole=r.id<where><!--where相当于select*frompetwhereid=1中的where一样--> <iftest="usercode!=nullandusercode!=''"> ANDuserCodeLIKECONCAT('%',#{usercode},'%') </if> <iftest="userrole!=0"> ANDuserRole=#{userrole} </if> <iftest="gender!=nullandgender!=0"> ANDgender=#{gender} </if> </where>
set
<updateid="upda"parameterType="User"> updatesmbms_user <set><!--与修改时搭配使用我们修改set的sql语句的‘,'的最后一列会被自动省略--> <iftest="userCode!=nullanduserCode!=''"> userCode=#{userCode}, </if> <iftest="userName!=nullanduserName!=''"> userName=#{userName}, </if> <iftest="userPassword!=nullanduserPassword!=''"> userPassword=#{userPassword}, </if> <iftest="gender!=nullandgender!=''"> gender=#{gender}, </if> <iftest="phone!=nullandphone!=''"> phone=#{phone}, </if> <iftest="address!=nullandaddress!=''"> address=#{address}, </if> <iftest="userRole!=nullanduserRole!=''"> userRole=#{userRole}, </if> <iftest="createdBy!=nullandcreatedBy!=''"> createdBy=#{createdBy}, </if> </set> whereid=#{id} </update>
choose(when、otherwise)
相当于Java中switch语句 当when有条件满足的时候,就跳出choose
<choose> <whentest="条件1">…</when> <whentest="条件2">…</when> <whentest="条件3">…</when> … <otherwise>…</otherwise></choose>
foreach
迭代一个集合,通常用于in条件 属性 item index collection:必须指定 list array map-key open separator close
<selectid="getUserName"resultType="User"parameterType="java.util.List"> select*fromsmbms_userwhereuserCodein <foreachcollection="list"open="("close=")"item="userCode"separator=","> #{userCode} </foreach> </select>
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
mybatis mapper.xml中怎么根据数据库类型选择对应SQL语句的详细内容,希望对您有所帮助,信息来源于网络。