https://www.jianshu.com/p/1819562cbec0DEMO? - 图1

    映射文件

    1. ?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper
    3. PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="ProductMap">
    6. <!--根据姓名中的关键字进行模糊查询-->
    7. <select id="nameLike" resultType="com.dcl.domain.Product" parameterType="com.dcl.dto.LikeDto">
    8. select * from product where pname like "%"#{pname}"%"
    9. </select>
    10. <!--根据姓名的字数 来进行模糊查询-->
    11. <select id="numNameLike" resultType="com.dcl.domain.Product" parameterType="com.dcl.dto.LikeDto">
    12. select * from product where pname like #{pname}
    13. </select>
    14. <!--根据售价范围进行查询-->
    15. <select id="findSellprice" resultType="com.dcl.domain.Product" parameterType="com.dcl.dto.LikeNum">
    16. select * from product where sellprice between #{min} and #{max}
    17. </select>
    18. <!--根据姓名字数和售价范围来进行查询-->
    19. <select id="numAndSellprice" resultType="com.dcl.domain.Product" parameterType="com.dcl.dto.NumAndSellprice">
    20. select * from product where pname like #{pname} and sellprice between #{min} and #{max}
    21. </select>
    22. </mapper>

    ProductDao接口,可以看到需求的参数都是 dto

    1. package com.dcl.dao;
    2. import com.dcl.domain.Product;
    3. import com.dcl.dto.LikeDto;
    4. import com.dcl.dto.LikeNum;
    5. import com.dcl.dto.NumAndSellprice;
    6. import java.util.List;
    7. public interface ProductDao {
    8. /**
    9. * 通过姓氏来进行模糊查询
    10. * @param likeDto
    11. * @return
    12. */
    13. public List<Product> findNameLike(LikeDto likeDto);
    14. /**
    15. * 根据名字的个数来查询
    16. * @param likeNum
    17. * @return
    18. */
    19. public List<Product> findNameThreeLike(LikeDto likeDto);
    20. /**
    21. * 根据价钱的范围来进行查询
    22. * @param likeNum
    23. * @return
    24. */
    25. public List<Product> findSellprice(LikeNum likeNum);
    26. /**
    27. * 根据名字的个数和价钱的范围来进行查询
    28. * @return
    29. */
    30. public List<Product> numAndSellprice(NumAndSellprice numAndSellprice);
    31. }

    实现类

    1. package com.dcl.dao.impl;
    2. import com.dcl.dao.ProductDao;
    3. import com.dcl.domain.Product;
    4. import com.dcl.dto.LikeDto;
    5. import com.dcl.dto.LikeNum;
    6. import com.dcl.dto.NumAndSellprice;
    7. import org.apache.ibatis.session.SqlSession;
    8. import org.apache.ibatis.session.SqlSessionFactory;
    9. import java.util.List;
    10. public class ProductDaoImpl implements ProductDao {
    11. public ProductDaoImpl() {
    12. }
    13. //所有方法公用的 factory对象,应该是应用级别的
    14. private SqlSessionFactory sqlSessionFactory;
    15. //使用构造方法来传值(SQLSessionFactory)
    16. public ProductDaoImpl(SqlSessionFactory sqlSessionFactory) {
    17. this.sqlSessionFactory = sqlSessionFactory;
    18. }
    19. @Override
    20. public List<Product> findNameLike(LikeDto likeDto) {
    21. //每一个方法应该获取一个SqlSession对象,用完应该立即关闭,因为线程不安全
    22. SqlSession sqlSession = sqlSessionFactory.openSession();
    23. //返回的是一个集合
    24. List<Product>list=sqlSession.selectList("ProductMap.nameLike",likeDto);
    25. sqlSession.close();
    26. return list;
    27. }
    28. @Override
    29. public List<Product> findNameThreeLike(LikeDto likeDto) {
    30. //获取SqlSession对象
    31. SqlSession sqlSession=sqlSessionFactory.openSession();
    32. //返回的是一个集合
    33. List<Product>list=sqlSession.selectList("ProductMap.numNameLike",likeDto);
    34. sqlSession.close();
    35. return list;
    36. }
    37. @Override
    38. public List<Product> findSellprice(LikeNum likeNum) {
    39. SqlSession sqlSession=sqlSessionFactory.openSession();
    40. List<Product>list = sqlSession.selectList("ProductMap.findSellprice",likeNum);
    41. sqlSession.close();
    42. return list;
    43. }
    44. @Override
    45. public List<Product> numAndSellprice(NumAndSellprice numAndSellprice) {
    46. SqlSession sqlSession=sqlSessionFactory.openSession();
    47. List<Product>list=sqlSession.selectList("ProductMap.numAndSellprice",numAndSellprice);
    48. return list;
    49. }
    50. }

    Dto对象

    1. public class LikeDto {
    2. private String pname;
    3. }
    4. //get,set,toString(),空构造
    1. public class LikeNum {
    2. private int min;
    3. private int max;
    4. }
    5. //get,set,toString(),空构造,满参构造
    1. public class NumAndSellprice {
    2. private int min;
    3. private int max;
    4. private String pname;
    5. }
    6. //get,set,toString(),空构造,满参构造