您现在的位置是:首页 > 个人日记个人日记

mybatis-05-动态SQL

2019-02-02 17:27:37【个人日记】396人已围观

简介主要是mybatis提供的一些标签,可以处理一些复杂SQL,使用起来方便

动态SQL

where...if...

  1. <select id="findList" parameterType="java.util.Map" resultType="com.huangxin.model.ProductCategory">
  2. SELECT * FROM product
  3. <!--WHERE标签可以去掉查询条件中的AND关键词-->
  4. <!--如果只有一个条件则去掉AND,否则只去掉第一个-->
  5. <where>
  6. <if test="productName!=null and productName!=''">
  7. AND productName LIKE '%${productName}%'
  8. </if>
  9. <if test="brand!=null and brand!=''">
  10. AND brand = #{brand}
  11. </if>
  12. <if test="supplier!=null and supplier!=''">
  13. AND supplier = #{supplier}
  14. </if>
  15. <if test="endTime!=null and startTime!=null">
  16. AND BETWEEN endTime = #{endTime} AND startTime = #{startTime}
  17. </if>
  18. </where>
  19. </select>

接口代码

  1. /**
  2. * 动态多条件查询
  3. *
  4. * @param param
  5. * @return
  6. */
  7. List<ProductCategory> findList(Map<String, Object> param);

可以动态改变SQL语句

  1. @Test
  2. public void findListl() {
  3. Map<String,Object> param = new HashMap<String, Object>();
  4. // param.put("productName","耳机");
  5. param.put("brand","未知");
  6. SqlSession sqlSession = factory.openSession();
  7. //通过mybatis的动态代理机制,生成了一个ProductCategory接口实现类
  8. ProductCategoryMapper mapper = sqlSession.getMapper(ProductCategoryMapper.class);
  9. System.out.println(mapper.findList(param));
  10. }

控制台输出

  1. 2018-12-29 12:04:13 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  2. 2018-12-29 12:04:13 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  3. 2018-12-29 12:04:13 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  4. 2018-12-29 12:04:13 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  5. 2018-12-29 12:04:13 [DEBUG] Opening JDBC Connection
  6. 2018-12-29 12:04:13 [DEBUG] Created connection 1824837049.
  7. 2018-12-29 12:04:13 [DEBUG] Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6cc4cdb9]
  8. 2018-12-29 12:04:13 [DEBUG] > Preparing: SELECT * FROM product WHERE brand = ?
  9. 2018-12-29 12:04:13 [DEBUG] > Parameters: 未知(String)
  10. 2018-12-29 12:04:13 [DEBUG] < Total: 7

以上SQL中有的数据才会拼接,没有则查询全部

查询数据形参为Map类型

SQL片段

解决代码重复,提高复用性

  1. <sql id="repeat"><!--ID则是引入时的唯一标志-->
  2. <if test="productName!=null and productName!=''">
  3. AND productName LIKE '%${productName}%'
  4. </if>
  5. <if test="brand!=null and brand!=''">
  6. AND brand = #{brand}
  7. </if>
  8. <if test="supplier!=null and supplier!=''">
  9. AND supplier = #{supplier}
  10. </if>
  11. </sql>

下面为引入之后的代码

  1. <!--通过条件查询条数-->
  2. <select id="findCount" parameterType="java.util.Map" resultType="java.lang.Long">
  3. SELECT COUNT (*) FROM product
  4. <where>
  5. <!--refid中填入SQL片段的ID-->
  6. <include refid="repeat"></include>
  7. </where>
  8. </select>
  9. <!--动态查询,使用where...if...-->
  10. <select id="findList" parameterType="java.util.Map" resultType="com.huangxin.model.ProductCategory">
  11. SELECT * FROM product
  12. <!--WHERE 标签可以去掉查询条件中的and关键词-->
  13. <where>
  14. <include refid="repeat"></include>
  15. </where>
  16. </select>

不能引入where

foreach遍历传入sql

1) collection表示传入集合名称
2) item表示每次遍历生成的对象
3) open表示开始遍历时的拼接
4) separator表示遍历用什么分割
5) close表示结束时的拼接

  1. <select id="findByIds" resultType="com.huangxin.model.ProductCategory" parameterType="java.util.List">
  2. <foreach
  3. <!--使用collection前记得命名-->
  4. collection="ids"
  5. item="id"
  6. open="IN ("
  7. separator=","
  8. close=")">
  9. <!--这里给括号中赋值,id则是item起的名字(列名)-->
  10. id=#{id}
  11. </foreach>
  12. </select>

接口中添加方法

  1. public interface ProductCategoryMapper {
  2. /**
  3. * 通过多个id查询数据
  4. *
  5. * @param list
  6. * @return
  7. */
  8. //一定要命名
  9. List<ProductCategory> findByIds(@Param("ids") List<Long> list);

@Param("起得名字"),一定要给List命名,这样才可以在 xml中的foreach中使用collection="List的名字"

Tags: JavaWeb  

评论区

    2024-04-27 06:21:10

    站长

    没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信

    2019-07-04 22:30:03

    Devil·K

    000000000000---------------------000000000000000000000000


文章评论



给自个选个头像吧!






站点信息

  • 建站时间:   2019-01-31
  • 网站程序:   Tomcat+nginx
  • 文章统计:   44篇文章
  • 标签管理:   标签云
  • 微信公众号:  扫描二维码,联系我