实现后台高级查询(基础版)

1 高级查询基础版

操作步骤:
        1.建立高级查询表对象:XxxQuery/XxxQueryObject.(Xxx表示对象名称);

          ProductQueryObject(商品的高级查询对象),封装了商品高级查询表单的数据

        2.在IproductDAO中定义高级查询方法:

          List<Product> query(ProductQueryObject qo);

        3.在ProductDAOImpl中提供高级查询方法的实现

        4.后台测试。

建立高级查询表对象

public class ProductQueryObject {
    private String productName;//商品名称
    private BigDecimal minSalePrice;//最低商品零售价
    private BigDecimal maxSalePrice;//最高商品零售价
    //getter/setter
    public String getProductName() {
        return productName;
    }
    public BigDecimal getMinSalePrice() {
        return minSalePrice;
    }
    public BigDecimal getMaxSalePrice() {
        return maxSalePrice;
    }
    public void setProductName(String productName) {
        this.productName = productName;
    }
    public void setMinSalePrice(BigDecimal minSalePrice) {
        this.minSalePrice = minSalePrice;
    }
    public void setMaxSalePrice(BigDecimal maxSalePrice) {
        this.maxSalePrice = maxSalePrice;
    }
}

在IproductDAO中定义高级查询方法

public interface IProductDAO {
    List list();

    //高级查询的方法
    List query (ProductQueryObject qo);
}

在ProductDAOImpl中提供高级查询方法的实现

public class ProductDAOImpl implements IProductDAO {

    public List query(ProductQueryObject qo) {
        SqlSession session = MyBatisUtil.INSTANCE .openSession();   
        //-------------------------------------------------------
        //拼接SQL
        StringBuilder sql = new StringBuilder(" WHERE 1=1 ");
        Map sqlMap = new HashMap<>();

        //若输入了商品名称
        if(StringUtil.hasLength(qo.getProductName())){//名字占位符
            sql.append(" AND productName LIKE CONCAT('%',#{name},'%')");
            sqlMap.put("name", qo.getProductName());
        }
        //最低零售价
        if(qo.getMinSalePrice()!=null){
            sql.append(" AND salePrice >= #{minSalePrice}");
            sqlMap.put("minSalePrice",qo.getMinSalePrice());
        }
        //最高零售价
        if(qo.getMaxSalePrice()!=null){
            sql.append(" AND salePrice <= #{maxSalePrice}");
            sqlMap.put("maxSalePrice", qo.getMaxSalePrice());
        }
        //-------------------------------------------------------
        try {
            sqlMap.put("querySql", sql.toString());
            return session.selectList("cn.itsource.shopping.mapper.ProductMapper.advanceQuery", sqlMap);
        } finally {
            session.close();
        }
    }
}

后台测试

    @Test
    public void testQuery() {
        ProductQueryObject qo = new ProductQueryObject();
        qo.setProductName("iphone8s");
        qo.setMinSalePrice(new BigDecimal("6000"));
        qo.setMaxSalePrice(new BigDecimal("9000"));
        //------------------------------------
        List list = dao.query(null);
        for (Product p : list) {
            System.out.println(p);`
        }
    }

xml映射文件



 
<mapper namespace="cn.itsource.shopping.mapper.ProductMapper">

    
    <select id="list" resultType="Product">
        SELECT * FROM product
    select>

    
    <select id="advanceQuery" parameterType="map" resultType="Product">
        SELECT * FROM product ${querySql}
    select>
mapper>

MyBatais的XML配置文件



<configuration>
    
    <properties resource="db.properties" />
    
    <typeAliases>
        <typeAlias type="cn.itsource.shopping.domain.Product" alias="Product" />
    typeAliases>
    
    <environments default="development">
        
        <environment id="development">
            
            <transactionManager type="JDBC" />
            
            <dataSource type="POOLED">
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${usename}" />
                <property name="password" value="${password}" />
            dataSource>
        environment>
    environments>
    
    <mappers>
        <mapper resource="cn\itsource\shopping\domain\ProductMapper.xml" />
    mappers>
configuration>

db.properties配置文件

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbcdemo?useSSL=false
usename=root
password=111111

MyBatisUtil工具类

public enum MyBatisUtil {
    INSTANCE;

    private static SqlSessionFactory sessionFactory = null;

    static {
        try {
            sessionFactory = new SqlSessionFactoryBuilder().build(Resources
                    .getResourceAsStream("MyBatis-config.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public SqlSession openSession() {
        return sessionFactory.openSession();
    }
}

StringUtil工具类

public class StringUtil {
    public static boolean hasLength(String str) {
        return str != null && !"".equals(str.trim());
    }
}

商品对象类

public class Product {
    private Long id;
    private String productName;
    private String brand;
    private String supplier;
    private BigDecimal salePrice;
    private BigDecimal costPrice;
    private Double cutoff;
    private Long dir_id;//分类编号
    public Long getId() {
        return id;
    }
    public String getProductName() {
        return productName;
    }
    public void setProductName(String productName) {
        this.productName = productName;
    }
    public String getBrand() {
        return brand;
    }
    public String getSupplier() {
        return supplier;
    }
    public BigDecimal getSalePrice() {
        return salePrice;
    }
    public BigDecimal getCostPrice() {
        return costPrice;
    }
    public Double getCutoff() {
        return cutoff;
    }
    public Long getDir_id() {
        return dir_id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public void setSupplier(String supplier) {
        this.supplier = supplier;
    }
    public void setSalePrice(BigDecimal salePrice) {
        this.salePrice = salePrice;
    }
    public void setCostPrice(BigDecimal costPrice) {
        this.costPrice = costPrice;
    }
    public void setCutoff(Double cutoff) {
        this.cutoff = cutoff;
    }
    public void setDir_id(Long dir_id) {
        this.dir_id = dir_id;
    }
    public String toString() {
        return "Product [id=" + id + ", productName=" + productName + ", brand=" + brand + ", suppliet=" + supplier
                + ", salePrice=" + salePrice + ", costPrice=" + costPrice + ", cutoff=" + cutoff + ", dir_id=" + dir_id
                + "]";
    }
}

你可能感兴趣的:(Jdbc)