Spring Cloud Alibaba 工程搭建连接数据库(2)

项目的源码地址
Spring Cloud Alibaba 工程搭建(1)
Spring Cloud Alibaba 工程搭建连接数据库(2)
Spring Cloud Alibaba 集成 nacos 以及整合 Ribbon 与 Feign 实现负载调用(3)
Spring Cloud Alibaba Ribbon 负载调用说明(4)
Spring Cloud Alibaba 核心理论 CAP与BASE理论简单理解(5)
Spring Cloud Alibaba Sentinel 集成与限流实战(6)
Spring Cloud Alibaba 网关 Gateway 集成(7)

回顾

故事紧接上回,在上一篇中,我们完成以下部分:

  • 搭建完数据库,以及建完对应库的表结构
  • 完成了项目工程搭建
    回顾上篇的工程目录

源码继续搭建

依赖以及配置文件相关

common 模块

这个模块是用来存放系统上面用到的 bean 对象,方便与不同的微服务模块引用。这里我们创建几个 bean 对象:
User

public class User {

    private Integer id;
    private String name;
    private String pwd;
    private String headImg;
    private String phone;
    private Date createTime;
    private String wechat;

	// 省略 getter 与 setter
}

Video

public class Video {

    private Integer id;
    private String title;
    private String summary;
    private String coverImg;
    private Integer price;
    private Date createTime;
    private Double point;

	// 省略 getter 与 setter
}

VideoOrder

public class VideoOrder {

    private Integer id;
    private String outTradeNo;
    private Integer state;
    private Date createTime;
    private Integer totalFee;
    private Integer videoId;
    private String videoTitle;
    private String videoImg;
    private Integer userId;
    private String serverInfo;
	
	// 省略 getter 与 setter
}
order 模块

对应 order 模块,我们主要是引入对应的依赖包,以及增加对应的数据库里连接相关的配置

pom 文件

主要是增加 mybatis 的依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
application.yml 文件
server:
  port: 9000

spring:
  application:
    name: demo-order

  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.1.102:3306/cloud_video?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: admin
    password: 123456

# 控制台输出sql、下划线转驼峰
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
video 模块

对应 video 模块,和上面的 order 模块一样,我们主要是引入对应的依赖包,以及增加对应的数据库里连接相关的配置

pom 文件

主要是增加 mybatis 的依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
application.yml 文件
server:
  port: 8000

spring:
  application:
    name: demo-video

  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.152.129:3306/cloud_video?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: admin
    password: 123456


# 控制台输出sql、下划线转驼峰
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
video 模块功能完善

补充完之后的工程

主要是增加对数据库的操作,以及用于接口访问的逻辑

controller
@RestController
@RequestMapping("api/v1/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    // http://localhost:9000/api/v1/video/find_by_id?videoId=30
    @RequestMapping("find_by_id")
    public Object findById(int videoId, HttpServletRequest httpRequest){
        Video video = videoService.findById(videoId);
        String serverInfo = httpRequest.getServerName() + ":"+ httpRequest.getServerPort();
        video.setServerInfo(serverInfo);
        return video;
    }

    /**
     * 这里是接收传入过来的参数,使用的注解可以使用 @RequestMapping("saveByFeign") 或者 @PostMapping("saveByFeign")
     * @param video Video
     * @return 返回值
     */
    @PostMapping("saveByFeign")
    public int saveByFeign(@RequestBody Video video){
        System.out.println("========>"+video.getTitle());
        return 1;
    }
}

service
@Service
public class VideoService {

    @Autowired
    private VideoMapper videoMapper;

    public Video findById(int videoId) {
        return videoMapper.findById(videoId);
    }
}
mapper
@Repository
public interface VideoMapper {

    @Select("select * from video where id=#{videoId}")
    Video findById(@Param("videoId") int videoId);
}
增加 mapper 映射地址
@SpringBootApplication
@MapperScan("com.demo.mapper")
public class VideoApplication {

    public static void main(String[] args) {
        SpringApplication.run(VideoApplication.class, args);
    }
}
测试访问
http://localhost:8000/api/v1/video/find_by_id?videoId=30

测试

最后,到这里为止,整个工程项目就搭建完成了,后面开始集成 nacos,作为注册中心来使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wayfreem

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值