https://www.jianshu.com/p/1819562cbec0
映射文件
?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="ProductMap">
<!--根据姓名中的关键字进行模糊查询-->
<select id="nameLike" resultType="com.dcl.domain.Product" parameterType="com.dcl.dto.LikeDto">
select * from product where pname like "%"#{pname}"%"
</select>
<!--根据姓名的字数 来进行模糊查询-->
<select id="numNameLike" resultType="com.dcl.domain.Product" parameterType="com.dcl.dto.LikeDto">
select * from product where pname like #{pname}
</select>
<!--根据售价范围进行查询-->
<select id="findSellprice" resultType="com.dcl.domain.Product" parameterType="com.dcl.dto.LikeNum">
select * from product where sellprice between #{min} and #{max}
</select>
<!--根据姓名字数和售价范围来进行查询-->
<select id="numAndSellprice" resultType="com.dcl.domain.Product" parameterType="com.dcl.dto.NumAndSellprice">
select * from product where pname like #{pname} and sellprice between #{min} and #{max}
</select>
</mapper>
ProductDao接口,可以看到需求的参数都是 dto
package com.dcl.dao;
import com.dcl.domain.Product;
import com.dcl.dto.LikeDto;
import com.dcl.dto.LikeNum;
import com.dcl.dto.NumAndSellprice;
import java.util.List;
public interface ProductDao {
/**
* 通过姓氏来进行模糊查询
* @param likeDto
* @return
*/
public List<Product> findNameLike(LikeDto likeDto);
/**
* 根据名字的个数来查询
* @param likeNum
* @return
*/
public List<Product> findNameThreeLike(LikeDto likeDto);
/**
* 根据价钱的范围来进行查询
* @param likeNum
* @return
*/
public List<Product> findSellprice(LikeNum likeNum);
/**
* 根据名字的个数和价钱的范围来进行查询
* @return
*/
public List<Product> numAndSellprice(NumAndSellprice numAndSellprice);
}
实现类
package com.dcl.dao.impl;
import com.dcl.dao.ProductDao;
import com.dcl.domain.Product;
import com.dcl.dto.LikeDto;
import com.dcl.dto.LikeNum;
import com.dcl.dto.NumAndSellprice;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class ProductDaoImpl implements ProductDao {
public ProductDaoImpl() {
}
//所有方法公用的 factory对象,应该是应用级别的
private SqlSessionFactory sqlSessionFactory;
//使用构造方法来传值(SQLSessionFactory)
public ProductDaoImpl(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
@Override
public List<Product> findNameLike(LikeDto likeDto) {
//每一个方法应该获取一个SqlSession对象,用完应该立即关闭,因为线程不安全
SqlSession sqlSession = sqlSessionFactory.openSession();
//返回的是一个集合
List<Product>list=sqlSession.selectList("ProductMap.nameLike",likeDto);
sqlSession.close();
return list;
}
@Override
public List<Product> findNameThreeLike(LikeDto likeDto) {
//获取SqlSession对象
SqlSession sqlSession=sqlSessionFactory.openSession();
//返回的是一个集合
List<Product>list=sqlSession.selectList("ProductMap.numNameLike",likeDto);
sqlSession.close();
return list;
}
@Override
public List<Product> findSellprice(LikeNum likeNum) {
SqlSession sqlSession=sqlSessionFactory.openSession();
List<Product>list = sqlSession.selectList("ProductMap.findSellprice",likeNum);
sqlSession.close();
return list;
}
@Override
public List<Product> numAndSellprice(NumAndSellprice numAndSellprice) {
SqlSession sqlSession=sqlSessionFactory.openSession();
List<Product>list=sqlSession.selectList("ProductMap.numAndSellprice",numAndSellprice);
return list;
}
}
Dto对象
public class LikeDto {
private String pname;
}
//get,set,toString(),空构造
public class LikeNum {
private int min;
private int max;
}
//get,set,toString(),空构造,满参构造
public class NumAndSellprice {
private int min;
private int max;
private String pname;
}
//get,set,toString(),空构造,满参构造