栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

回顾

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

  • 搭建完数据库,以及建完对应库的表结构
  • 完成了项目工程搭建
源码继续搭建 依赖以及配置文件相关 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 的依赖


    org.mybatis.spring.boot
    mybatis-spring-boot-starter



    mysql
    mysql-connector-java

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 的依赖


    org.mybatis.spring.boot
    mybatis-spring-boot-starter



    mysql
    mysql-connector-java

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;
    }

    
    @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,作为注册中心来使用。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1035341.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号