Idea+SpringBoot+Mybtis+Mysql+Gradle+Swagger2 - 一-动态查询

项目说明:

1. 开发工具: Idea(2017.1.5),jdk1.8

使用框架,数据库,jar库依赖:SpringBoot+Mybtis, Mysql, Gradle4.0

后台测试使用:Swagger2

2. 本项目模仿的需求: 商品的动态查询

3,代码展示和说明:

(1)扫描dao层需要在项目入口:加上注解扫描@MapperScan("so.sao.dao"),或者在自己的dao里面的接口上面加@Mapper

扫描service层需要在是实现类加@service

      (2)下面写的配置的.xml文件,需要和数据库连接等信息一起在resources/application.properties 加自己的配置

如: mybatis.mapperLocations=classpath:mapper/*.xml

3.1 domain层 : 建立实体对象类:Commodity  and  CommType, 根据需求列名自己定义几个列(字段)。

Page(效验自己的分页参数)

Page实体类如下参考!

/**

 * 当前页数
 */
private int pageNumber;

/**
 * 页面大小
 */
private int pageSize;

public Page() {
}

public Page(int pageNumber, int pageSize) {
    this.pageNumber = pageNumber;
    this.pageSize = pageSize;
}

3.2 web层(Controller)
	CommunityController(商品的控制层):商品的动态查询
 
  
/**
 * Created by XuPengFei on 2017/7/13.

*/ @RestController @RequestMapping(value = "/Commodity") public class CommodityController { @Autowired private CommodityService commodityService; /** http://localhost:8080/Commodity/searchCommdityByCondition/2/2 * Through all parameters(you can give one param or any more param) to search Commdity. * @param firstType * @param secondType * @param secondType * @param commName * @param price01 * @param price02 * @param pageNumber * @param pageSize * @return PageInfo< Commodity> */ @ApiOperation(value = "searchCommBySearchBy", notes = "Search Commidty By Search condition: XUPENGFEI") @ApiImplicitParams({ @ApiImplicitParam(name = "firstType",value = "First Type",required = false,dataType = "String", paramType = "query"), @ApiImplicitParam(name = "secondType",value = "First Type",required = false,dataType = "String",paramType = "query"), @ApiImplicitParam(name = "commName",value = "Commdity name",required = false,dataType = "String",paramType = "query"), @ApiImplicitParam(name = "price01",value = "First price",required = false,dataType = "Double",paramType = "query"), @ApiImplicitParam(name = "price02",value = "Second price",required = false,dataType = "Double",paramType = "query") }) @RequestMapping(value = "/searchCommdityByCondition/{pageNumber}/{pageSize}", method = RequestMethod. GET) public PageInfo searchCommodityByCondition(String firstType, String secondType, String commName, Double price01, Double price02, @PathVariable Integer pageNumber, @PathVariable Integer pageSize) { PageInfo pageInfo = commodityService.searchCommodityByCondition(firstType,secondType,commName,price01,price02,pageNumber,pageSize); return pageInfo; }}
3.2 service层,此处接口省略,直接给出实现类: 
/**
 * Created by 
XuPengFei 
on 2017/7/13. */ @Service public class CommodityServiceImpl implements CommodityService{ @Autowired private CommodityMapper commodityMapper; /** * Through all parameters(you can give one param or any more param) to search Commdity. * @param firstType * @param secondType * @param commName * @param price01 * @param price02 * @param pageNumber * @param pageSize * @return PageInfo< Commodity> */ @Override public PageInfo searchCommodityByCondition(String firstType, String secondType, String commName, Double price01, Double price02, Integer pageNumber, Integer pageSize) { if( null!=firstType){ firstType = firstType.trim(); } if( null!=secondType){ secondType = secondType.trim(); } if( null!=commName){ commName = commName.trim(); } firstType = firstType == "" ? null: firstType; secondType = secondType == "" ? null: secondType; commName = commName == "" ? null : commName; if( null!= price01){ price01 = price01 >= 0 ? price01 : 0.00; } else { price01 = 0.00; } if( null!= price02){ price02 = price02 >= price01 ? price02 : price01; } Page pp = new Page(pageNumber,pageSize); pp = PageUtil. pageParamCheck(pp); PageHelper. startPage(pp.getPageNumber(),pp.getPageSize()); List commodityList = commodityMapper.findPageCommodityBySearch(firstType,secondType,commName,price01, price02); PageInfo pageInfo = new PageInfo(commodityList); return pageInfo; }}
 
  
3.2 dao层(Mapper接口): 
public interface CommodityMapper {

    /**
     * Through all parameters(you can give one param or any more param) to search Commdity.
     * @param firstType
     * @param secondType
     * @param commName
     * @param price01
     * @param price02
     * @return List<Commodity>
     */
    public List findPageCommodityBySearch(@Param("firstType") String firstType, @Param("secondType") String secondType, @Param("commName") String commName,
                                                     @Param("price01") Double price01, @Param("price02") Double price02);
}

3.3 resource 下自己建立mapper包,里面放自己的xml文件
	CommodityMapper.xml如下:
xml version="1.0" encoding="UTF-8" ?>
 mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="so.sao.dao.CommodityMapper">
    <resultMap id="BaseResultMap" type="so.sao.domain.Commodity">
        <id column="ID" property="id"/>
        <result column="COMM_NAME" property="name"/>
        <result column="COMM_DESCRIBE" property="describe"/>
        <result column="FIRSTTYPE" property="firstType"/>
        <result column="SECONDTYPE" property="secondType"/>
        <result column="THIRDTYPE" property="thirdType"/>
        <result column="NUM" property="num"/>
        <result column="NUM69" property="num69"/>
        <result column="BRAND" property="brand"/>
        <result column="COMM_NO" property="no"/>
        <result column="WEIGHT" property="weight"/>
        <result column="DUCTIONDATE" property="ductionDate"/>
        <result column="MINIMG" property="minImg"/>
        <result column="MAXIMG" property="maxImg"/>
        <result column="PRICE" property="price"/>
        <result column="PAYPRICE" property="payPrice"/>
        <result column="ADDESC" property="adDesc"/>
        <result column="COMM_STATUS" property="status"/>
        <result column="CREATEDUSER" property="createdUser"/>
        <result column="CREATEDAT" property="createdAt"/>
        <result column="UPDATEDAT" property="updatedAt"/>
    resultMap>

    
    <select id="findPageCommodityBySearch" resultMap="BaseResultMap">
        SELECT ID,COMM_NAME,COMM_DESCRIBE,FIRSTTYPE,SECONDTYPE,THIRDTYPE,NUM,NUM69,BRAND,
        COMM_NO,WEIGHT,DUCTIONDATE,MINIMG,MAXIMG,PRICE,PAYPRICE,ADDESC,COMM_STATUS,CREATEDUSER,CREATEDAT,UPDATEDAT
        FROM TB_COMMODITY tb01
        <where>
            <if test="firstType != null">
                AND tb01.FIRSTTYPE = #{firstType}
            if>
            <if test="secondType != null">
                AND tb01.SECONDTYPE = #{secondType}
            if>
            <if test="commName != null">
                AND tb01.COMM_NAME LIKE CONCAT ('%',#{commName},'%')
            if>
            <if test="price01 != null and price02!=null">
                AND tb01.PRICE BETWEEN #{price01} AND #{price02}
            if>
        where>
    select>
mapper>

3.4 前面用到的工具类:新建一个Util包	
------------PageUtil类如下:
public class PageUtil{

    public static Page pageParamCheck(Page page)
    {
        int pageNum = page.getPageNumber();
        int pageSize = page.getPageSize();
        //分页参数校验、处理
        pageNum = pageNum > 0 ? pageNum : PageConstant.PAGE_NUM_DEFAULT;
        pageSize = pageSize > 0 ? pageSize : PageConstant.PAGE_SIZE_DEFAULT;
        pageNum = pageNum < 500 ? pageNum : PageConstant.PAGE_NUM_MAX_DEFAULT;
        pageSize = pageSize < 50 ? pageSize : PageConstant.PAGE_SIZE_MAX_DEFAULT;

        page.setPageNumber(pageNum);
        page.setPageSize(pageSize);
        return page;
    }
}
------PageConstant(常量类,名字不好,其实大家可以起名Constant存放所有本项目用到的常量)
/**
 * pagesize默认值10
 */
public static final int PAGE_SIZE_DEFAULT = 10 ;
/**
 * pagenume默认值1
 */
public static final int PAGE_NUM_DEFAULT = 1 ;
/**
 * pagesize最大默认值50
 */
public static final int PAGE_SIZE_MAX_DEFAULT = 50 ;
/**
 * pagenume最大默认值500
 */
public static final int PAGE_NUM_MAX_DEFAULT = 500 ;

 
  


 
 

你可能感兴趣的:(Web项目)