mybatis使用经验之查询

[size=small][size=xx-large][size=medium][size=xx-small]今天给大家分享一下mybatis查询的使用方法,希望我的分享能够帮到有需要的人


mybatis查询主要有三种方式实现:

方式一:<select id="selectResouceInfoByNotNullAttributes" resultMap="ExpandResultMap" parameterType="bean类名">    select * from table_name where 1=1      <if test="resourceId != null">        and resource_id = #{resourceId,jdbcType=INTEGER}      </if>      <if test="appid != null">       and  appid = #{appid,jdbcType=TINYINT}      </if>      <if test="resourceUrl != null">       and  resource_url = #{resourceUrl,jdbcType=VARCHAR}      </if>      <if test="resourceDesc != null">       and  resource_desc = #{resourceDesc,jdbcType=VARCHAR}      </if>  </select>

这种方式的要点在where后面的1=1,加上这个能够避免第一个if条件后面是否需要加and的选择困境。
方式二:<select id="selectResouceInfoByNotNullAttributes" resultMap="ExpandResultMap" parameterType="bean类名">    select * from 表名    <where>      <if test="resourceId != null">        and resource_id = #{resourceId,jdbcType=INTEGER}      </if>      <if test="appid != null">       and  appid = #{appid,jdbcType=TINYINT}      </if>      <if test="resourceUrl != null">       and  resource_url = #{resourceUrl,jdbcType=VARCHAR}      </if>      <if test="resourceDesc != null">       and  resource_desc = #{resourceDesc,jdbcType=VARCHAR}      </if>    </where>  </select>

这种方式比第一种多了一个where标签,而且不需要在where后面显示的加一个1=1的字段。
方式三:<select id="selectResouceInfoByNotNullAttributes" resultMap="ExpandResultMap" parameterType="bean类名">    select * from 表名    <trim prefix = "where" prefixOverrides="and|or">      <if test="resourceId != null">        and resource_id = #{resourceId,jdbcType=INTEGER}      </if>      <if test="appid != null">       and  appid = #{appid,jdbcType=TINYINT}      </if>      <if test="resourceUrl != null">       and  resource_url = #{resourceUrl,jdbcType=VARCHAR}      </if>      <if test="resourceDesc != null">       and  resource_desc = #{resourceDesc,jdbcType=VARCHAR}      </if>    </trim>  </select>

第三种方式是最值得提倡的方式,其中的trim标签中标记该标签是以where为前缀的,即where条件的字句。后面的prefixOverrides="and|or"是说如果where标签中包含的内容开头是and或者or,那么久忽略and或者or。还有另外一个:suffixOverrides="and|or",表示where字句中是and或者or结尾则忽略and或者or的意思。

1)id必须与mapper.java里的函数名称一致2)注意sql语句的原格式,去掉if等标签后也要是一个正确的语句3)trim语句可以包含前缀和后缀,前缀用prefix表示,后缀用suffix表示,以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides;正因为trim有这样的功能,所以我们也可以非常简单的利用trim来代替where元素的功能

[size=large]camel骆驼男士凉鞋 真皮潮流魔术贴沙滩鞋男鞋 夏季新款正品凉鞋 只要56元!


http://redirect.simba.taobao.com/rd?w=unionnojs&f=http%3A%2F%2Fai.taobao.com%2Fauction%2Fedetail.htm%3Fe%3DYNbrUj%252FZdJwjmraEDZVrLkKA%252ByOYgzU6TbuPAry6zvGLltG5xFicOdXrTUTgh9sMDPIwxrc30rhF03SVjj78hGqYCHH8uv2oZb7Xhy%252F%252BGHmWC8e6JwspUeIZWR1bMnHu%26unid%3D96391090%26ptype%3D100010%26from%3Dbasic&k=5ccfdb950740ca16&c=un&b=alimm_0&p=mm_96391090_7268811_24064425

原博客地址:
http://jingyan.baidu.com/article/af9f5a2dd8143b43140a4520.html
关于怎样通过sshpass来动态获取日志文件请看下面的文章。
http://jingyan.baidu.com/article/cd4c2979ca556c756e6e60aa.html
java中怎样使用linux的md5sun生成md5文件
http://jingyan.baidu.com/article/cd4c2979ca556c756e6e60aa.html
[/size][/size][/size][/size][/size]
MySql数据库中null值与其他值的比较
http://jingyan.baidu.com/article/9113f81b2adc882b3214c7cb.html
Java编程经验分享之Map使用
http://jingyan.baidu.com/article/215817f7d9d6b31eda1423d8.html
MySql使用经验——通过in查询怎样排序问题
http://jingyan.baidu.com/article/67662997325ccf54d51b84d5.html
Mybatis使用经验之xml注意事项
http://jingyan.baidu.com/article/fec7a1e51d7b451190b4e729.html
Mybatis使用经验分享之批量操作
http://jingyan.baidu.com/article/11c17a2c7f376af446e39d21.html
MyBatis使用经验分享之查询
http://jingyan.baidu.com/article/af9f5a2dd8143b43140a4520.html
linux怎样搭建tomcat服务器
http://jingyan.baidu.com/article/3052f5a1d93c1497f31f860d.html
怎样用linux脚本查询数据中的数据
http://jingyan.baidu.com/article/2c8c281dfb0add0008252a04.html
linux编程实践经验sshpass、md5sum、ssh
http://jingyan.baidu.com/article/cd4c2979ca556c756e6e60aa.html

猜你喜欢

转载自yangyingda2008.iteye.com/blog/1981805