畅购第一天--环境搭建

  1. 创建一个Maven项目作为父工程 changgou_parent

  2. 在父工程下面创建子工程,分别为:
    创建changgou_gateway、changgou_service、changgou_service_api、changgou_web工 程,工程全部为pom工程,并将所有工程的src文件删除。
    项目结构:在这里插入图片描述

  3. 父工程的依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
</parent><properties>
    <skipTests>true</skipTests>
</properties><!--依赖包-->
<dependencies>
    <!--测试包-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies><dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

4.Eureka微服务搭建
(1) pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>

(2) appliation.yml

server:
  port: 6868
eureka:
  client:
    register-with-eureka: false #是否将自己注册到eureka中
    fetch-registry: false #是否从eureka中获取信息
    service-url:
      defaultZone: http://127.0.0.1:${server.port}/eureka/

5.全局公共模块

pom.xml

<dependencies>
    <!--web起步依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- redis 使用-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.51</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
</dependencies>

6.数据访问公共模块搭建
pom.xml

<dependencies>
    <dependency>
        <groupId>com.changgou</groupId>
        <artifactId>changgou_common</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <!--通用mapper起步依赖-->
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>2.0.4</version>
    </dependency>
    <!--MySQL数据库驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!--mybatis分页插件-->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.3</version>
    </dependency>
</dependencies>

7.商品微服务搭建

(1).商品微服务API工程搭建

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

(2).微服务工程搭建

<dependencies>
    <dependency>
        <groupId>com.changgou</groupId>
        <artifactId>changgou_common_db</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.changgou</groupId>
        <artifactId>changgou_service_goods_api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

application.yml

server:
  port: 9011
spring:
  application:
    name: goods
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.200.128:3306/changgou_goods?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:6868/eureka
  instance:
    prefer-ip-address: true
feign:
  hystrix:
    enabled: true
    
#hystrix 配置
hystrix:
  command:
    default:
      execution:
        timeout:
        #如果enabled设置为false,则请求超时交给ribbon控制
          enabled: true
        isolation:
          strategy: SEMAPHORE

启动类

@SpringBootApplication
@EnableEurekaClient
@MapperScan(basePackages = {"com.changgou.goods.dao"})
public class GoodsApplication {
    public static void main(String[] args) {
        SpringApplication.run(GoodsApplication.class);
    }
}

注意:@MapperScan是tk.mybatis.spring.annotation包下的,用于扫描Mapper接口

商品微服务-品牌增删改查

实体

@Table(name="tb_brand")
public class Brand implements Serializable{
    @Id
    private Integer id;//品牌id
    private String name;//品牌名称
    private String image;//品牌图片地址
    private String letter;//品牌的首字母
    private Integer seq;//排序
    
    // getter and setter  .....(省略)
}

@Table和@Id都是JPA注解,@Table用于配置表与实体类的映射关系,@Id用于标识主键属性。对应 javax.persistence 依赖

接口

public interface BrandMapper extends Mapper<Brand> {
}

继承了Mapper接口,就自动实现了增删改查的常用方法。Mapper是tk.mybatis.spring.annotation包下的,

相关的接口方法:

brandMapper.selectAll()
brandMapper.selectByPrimaryKey(id)
brandMapper.insertSelective(brand) 一般有多少个值就插入多少个值 如 insert A value(1,“小米”)而不是insert A value(1,“小米”,null,null)
brandMapper.updateByPrimaryKeySelective(brand)
brandMapper.deleteByPrimaryKey(id)

/***
 * 修改品牌数据
 * @param brand
 * @param id
 * @return
 */
@PutMapping(value="/{id}")
public Result update(@RequestBody Brand brand,@PathVariable Integer id){
    brand.setId(id);
    brandService.update(brand);
    return new Result(true,StatusCode.OK,"修改成功");
}

修改测试
修改测试
条件查询

/**
 * 条件查询
 * @param searchMap
 * @return
 */
@Override
public List<Brand> findList(Map<String, Object> searchMap){
    Example example=new Example(Brand.class);
    Example.Criteria criteria = example.createCriteria();
    if(searchMap!=null){
        // 品牌名称
        if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
            criteria.andLike("name","%"+searchMap.get("name")+"%");
        }
        // 品牌的首字母
        if(searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))){
            criteria.andEqualTo("letter",searchMap.get("letter"));
        }
    }
    return brandMapper.selectByExample(example);
}
/***
 * 多条件搜索品牌数据
 * @param searchMap
 * @return
 */
@PostMapping(value = "/search" )
public Result findList(@RequestParam Map searchMap){
    List<Brand> list = brandService.findList(searchMap);
    return new Result(true,StatusCode.OK,"查询成功",list);
}

@RequestParam
品牌列表分页查询

(1) 业务层接口 修改com.changgou.goods.service.BrandService添加分页方法

/***
 * 分页查询
 * @param page
 * @param size
 * @return
 */
public Page<Brand> findPage(int page, int size);

(2)业务层实现 修改com.changgou.goods.service.impl.BrandServiceImpl添加分页方法实现

/**
 * 分页查询
 * @param page
 * @param size
 * @return
 */
@Override
public Page<Brand> findPage(int page, int size){
    PageHelper.startPage(page,size);
    return (Page<Brand>)brandMapper.selectAll();
}

(3) 控制层 BrandController新增方法

/***
 * 分页搜索实现
 * @param page
 * @param size
 * @return
 */
@GetMapping(value = "/search/{page}/{size}" )
public Result findPage(@PathVariable  int page, @PathVariable  int size){
    Page<Brand> pageList = brandService.findPage(page, size);
    PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
    return new Result(true,StatusCode.OK,"查询成功",pageResult);
}

品牌列表条件+分页查询

/**
     * 条件+分页查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
@Override
public Page<Brand> findPage(Map<String,Object> searchMap, int page, int size){
    PageHelper.startPage(page,size);
    Example example=new Example(Brand.class);
    Example.Criteria criteria = example.createCriteria();
    if(searchMap!=null){
        // 品牌名称
        if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
            criteria.andLike("name","%"+searchMap.get("name")+"%");
        }
        // 品牌的首字母
        if(searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))){
            criteria.andEqualTo("letter",searchMap.get("letter"));
        }
    }
    return (Page<Brand>)brandMapper.selectByExample(example);
}

(3)控制层 BrandController新增方法

/***
 * 分页搜索实现
 * @param searchMap
 * @param page
 * @param size
 * @return
 */
@GetMapping(value = "/search/{page}/{size}" )
public Result findPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size){
    Page<Brand> pageList = brandService.findPage(searchMap, page, size);
    PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
    return new Result(true,StatusCode.OK,"查询成功",pageResult);
}

在这里插入图片描述

公共异常处理

/**
 * 统一异常处理类
 */
@ControllerAdvice
public class BaseExceptionHandler {
    
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result error( Exception e) {
        e.printStackTrace();        
        return new Result(false, StatusCode.ERROR, e.getMessage());
    }
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页