在HBuilder中创建
运行时可能会报
下载插件即可(工具–>插件安装)
创建一个数据库表,用来存放所有的图片
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for images
-- ----------------------------
DROP TABLE IF EXISTS `images`;
CREATE TABLE `images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`src` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`background` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`type` int(255) NULL DEFAULT NULL,
`url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
SET FOREIGN_KEY_CHECKS = 1;
在IDEA中搭建最基础的项目结构(更加精细的SpringCloud结构)
我这里贴出的项目顺序是按照图片的顺序来的,导入项目依赖的时候自行注意,(因为shop-plug是使用easy code插件自动生成的,这里就不再贴出,对easy code有兴趣的可以参考:https://blog.csdn.net/qq_43222167/article/details/106643584)
总的父项目的依赖
<!--SpringBoot依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Eureka项目
结构
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--Eureka的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<!--Springcloud的安全议认证-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
启动类
package com.jbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
配置文件application.yml
server:
port: 7776
eureka:
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://localhost:7776/eureka/
server:
enable-self-preservation: false
#安全机制
security:
basic:
enabled: true #是否生成随机密码
user:
name: root
password: root
配置完成后启动服务并在浏览器中测试是否成功:http://localhost:7776/
输入设置的用户名和密码进入即可
shop-dao
结构
依赖
<dependencies>
<!--Images的实体依赖-->
<dependency>
<groupId>com.jbit.project</groupId>
<artifactId>shop-entity</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--数据库依赖-->
<!--mysql驱动-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!-- mybatis整合springBoot -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.jbit.project</groupId>
<artifactId>shop-entity</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
ImagesDao
package com.jbit.dao;
import com.jbit.entity.Images;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* (Images)表数据库访问层
*
* @author makejava
* @since 2020-06-09 16:35:17
*/
public interface ImagesDao {
/**
* 获取所有的轮播图
* @param type
* @return
*/
public List<Images> getImagesByType(String type);
}
ImagesDao.xml
<?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="com.jbit.dao.ImagesDao">
<!--获取所有的轮播图-->
<select id="getImagesByType" resultType="Images">
select * from images where type=0
</select>
</mapper>
shop-entity
Dto
package com.jbit.entity;
/**
* 数据传输对象(后端输出对象)
* @param <T>
* Created by hanlu on 2017/5/7.
*/
public class Dto<T> {
private String success; //判断系统是否出错做出相应的true或者false的返回,与业务无关,出现的各种异常
private String errorCode;//该错误码为自定义,一般0表示无错
private String msg;//对应的提示信息
private T data;//具体返回数据内容(pojo、自定义VO、其他)
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getSuccess() {
return success;
}
public void setSuccess(String success) {
this.success = success;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
Images
package com.jbit.entity;
import java.io.Serializable;
/**
* (Images)实体类
*
* @author makejava
* @since 2020-06-09 16:35:16
*/
public class Images implements Serializable {
private static final long serialVersionUID = -37701020491306158L;
private Integer id;
private String src;
private String background;
private Integer type;
private String url;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public String getBackground() {
return background;
}
public void setBackground(String background) {
this.background = background;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
shop-images
结构
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
</dependencies>
images-provider
结构
pom.xml
<dependencies>
<!--Service接口的依赖-->
<dependency>
<groupId>com.jbit.project</groupId>
<artifactId>shop-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--Dao层的依赖-->
<dependency>
<groupId>com.jbit.project</groupId>
<artifactId>shop-dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
ImagesServiceImpl
package com.jbit.service.impl;
import com.jbit.dao.ImagesDao;
import com.jbit.entity.Images;
import com.jbit.service.ImagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@Service("imagesServiceImpl")
public class ImagesServiceImpl implements ImagesService {
@Autowired
private ImagesDao imagesDao;
@RequestMapping(value = "getImagesByType")
@Override
public List<Images> getImagesByType(String type) {
return imagesDao.getImagesByType(type);
}
}
ProviderApplication
package com.jbit;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
@MapperScan(basePackages = "com.jbit.dao")
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}
application.yml
server:
port: 8081
spring:
application:
name: images-provider
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/project?useUnicode=true&characterEncoding=utf-8&useSSL=false
password: root
username: root
mybatis:
type-aliases-package: com.jbit.entity
mapper-locations: classes:com.jbt.mapper/*.xml
eureka:
client:
service-url:
defaultZone: http://root:root@localhost:7776/eureka/
images-consumer
pom.xml
<dependencies>
<dependency>
<groupId>com.jbit.project</groupId>
<artifactId>shop-entity</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!--dto-->
<dependency>
<groupId>com.jbit.project</groupId>
<artifactId>shop-utils</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
ImagesService
package com.jbit.service;
import com.jbit.entity.Images;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@FeignClient(name = "images-provider") //生产者名称
public interface ImagesService {
//服务名称
@RequestMapping(value = "getImagesByType")
public List<Images> getImagesByType(String type);
}
ConsumerApplication
package com.jbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //Feign
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
ImagesController
package com.jbit.controller;
import com.jbit.dto.DtoUtil;
import com.jbit.entity.Dto;
import com.jbit.entity.Images;
import com.jbit.service.ImagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("images")
public class ImagesController {
@Autowired
private ImagesService imagesService;
@RequestMapping(value = "getImagesByType")
public Dto<Images> getImagesByType(String type) {
System.out.println("controller----getImagesByType--->type:"+type);
return DtoUtil.returnDataSuccess(imagesService.getImagesByType(type));
}
}
application.yml
server:
port: 8080
spring:
application:
name: images-consumer
eureka:
client:
service-url:
defaultZone: http://root:root@localhost:7776/eureka/
shop-service
结构
pom.xml
<dependencies>
<!--实体依赖-->
<dependency>
<groupId>com.jbit.project</groupId>
<artifactId>shop-entity</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
ImagesService
package com.jbit.service;
import com.jbit.entity.Images;
import java.util.List;
/**
* (Images)表服务接口
*
* @author makejava
* @since 2020-06-09 16:35:18
*/
public interface ImagesService {
/**
* 获取所有的轮播图
* @param type
* @return
*/
public List<Images> getImagesByType(String type);
}
shop-utils
结构
pom.xml
<dependencies>
<dependency>
<groupId>com.jbit.project</groupId>
<artifactId>shop-entity</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
DtoUtil
package com.jbit.dto;
import com.jbit.entity.Dto;
/**
* 用于返回Dto的工具类
* Created by XX on 17-5-8.
*/
public class DtoUtil {
public static String success="true";
public static String fail="false";
public static String errorCode="0";
/***
* 统一返回成功的DTO
*/
public static Dto returnSuccess(){
Dto dto=new Dto();
dto.setSuccess(success);
return dto;
}
/***
* 统一返回成功的DTO 带数据
*/
public static Dto returnSuccess(String message,Object data){
Dto dto=new Dto();
dto.setSuccess(success);
dto.setMsg(message);
dto.setErrorCode(errorCode);
dto.setData(data);
return dto;
}
/***
* 统一返回成功的DTO 不带数据
*/
public static Dto returnSuccess(String message){
Dto dto=new Dto();
dto.setSuccess(success);
dto.setMsg(message);
dto.setErrorCode(errorCode);
return dto;
}
/***
* 统一返回成功的DTO 带数据 没有消息
*/
public static Dto returnDataSuccess(Object data){
Dto dto=new Dto();
dto.setSuccess(success);
dto.setErrorCode(errorCode);
dto.setData(data);
return dto;
}
public static Dto returnFail(String message,String errorCode){
Dto dto=new Dto();
dto.setSuccess(fail);
dto.setMsg(message);
dto.setErrorCode(errorCode);
return dto;
}
}
依赖启动eureka的服务,images-provider服务,images-consumer服务,启动后浏览中访问http://localhost:8080/images/getImagesByType?type=0
因为我之前在数据库表中添加了一条测试的数据,就已经搭建好了
项目已经上传到github上:https://github.com/HB-java/j341-firstProject-Shop.git