• Spring Cloud Alibaba 工程搭建连接数据库


    回顾

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

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

    源码继续搭建

    依赖以及配置文件相关

    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
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    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
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    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
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    order 模块

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

    pom 文件

    主要是增加 mybatis 的依赖

    <dependency>
        <groupId>org.mybatis.spring.bootgroupId>
        <artifactId>mybatis-spring-boot-starterartifactId>
    dependency>
    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    video 模块

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

    pom 文件

    主要是增加 mybatis 的依赖

    <dependency>
        <groupId>org.mybatis.spring.bootgroupId>
        <artifactId>mybatis-spring-boot-starterartifactId>
    dependency>
    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    service
    @Service
    public class VideoService {
    
        @Autowired
        private VideoMapper videoMapper;
    
        public Video findById(int videoId) {
            return videoMapper.findById(videoId);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    mapper
    @Repository
    public interface VideoMapper {
    
        @Select("select * from video where id=#{videoId}")
        Video findById(@Param("videoId") int videoId);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    增加 mapper 映射地址
    @SpringBootApplication
    @MapperScan("com.demo.mapper")
    public class VideoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(VideoApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    测试访问

    http://localhost:8000/api/v1/video/find_by_id?videoId=30
    
    • 1

    测试

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

  • 相关阅读:
    JVM简单理解
    隗华:OceanBase 企业服务助力客户实现业务无忧
    JavaWeb03-HTTP协议,Tomcat,Servlet
    密码加密传输
    ubuntu 22.04.2 LTS 上安装旧版jekyll所遇到的问题总结
    解决SpringBoot项目端口被占用的问题
    浅谈单点登录SSO实现方案 | StartDT Tech Lab 06
    redis的性能管理
    计算机毕业设计ssm餐饮外卖系统v22fo系统+程序+源码+lw+远程部署
    通过数据导入导出功能批量重命名文件名称更简单
  • 原文地址:https://blog.csdn.net/qq_18948359/article/details/126158337