SpringCloud Alibaba入门之用户子模块开发

在上一章的基础上进行子模块的开发SpringCloud Alibaba入门之创建多模块工程_qinxun2008081的博客-CSDN博客

一、引入SpringBoot

我们在父项目统一管理引入的jar包的版本。我们采用父项目中以depencyMangement方式引入spring-boot,子项目依赖parent父配置即可。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>myshop</artifactId><version>1.0-SNAPSHOT</version><!--设置为pom,管理依赖--><packaging>pom</packaging><modules><module>myshop-common</module><module>myshop-user</module><module>myshop-stock</module><module>myshop-order</module><module>myshop-goods</module><module>myshop-pay</module></modules><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!--统一管理项目依赖版本--><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.7.8</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>

二、引入JPA和MySql

我们在myshop-user模块下添加项目需要的依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.example</groupId><artifactId>myshop</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>myshop-user</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.31</version></dependency><!--JPA--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency></dependencies>
</project>

添加好依赖,使用Maven命令重新加载一下。

我们在模块下面的resource目录下添加配置文件application.yml,相关配置如下:

spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/myshop?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false&rewriteBatchedStatements=trueusername: rootpassword: 123456jpa:hibernate:ddl-auto: updateshow-sql: true

三、创建启动类

package com.example.myshop;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author qx* @date 2023/06/21* @desc 启动类*/
@SpringBootApplication
public class MyShopUserApplication {public static void main(String[] args) {SpringApplication.run(MyShopUserApplication.class, args);}
}

四、引入common模块和common模块的开发

我们可以在user模块引入common公共模块。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.example</groupId><artifactId>myshop</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>myshop-user</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.31</version></dependency><!--JPA--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!--添加common模块的依赖--><dependency><groupId>org.example</groupId><artifactId>myshop-common</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</project>

common模块开发如下:

添加依赖和创建统一结果返回类

 我们在common模块中创建一个统一结果返回的实体类CommonResult:

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;import java.io.Serializable;/*** @author qx* @date 2023/06/21* @desc 统一结果返回*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> implements Serializable {private Integer code;private String message;private T data;public static CommonResult success() {return CommonResult.builder().code(200).message("请求成功").build();}public static CommonResult success(Object data) {return CommonResult.builder().code(200).message("请求成功").data(data).build();}public static CommonResult fail(String message) {return CommonResult.builder().code(500).message(message).build();}
}

五、用户模块(myshop-user)开发

1.pom文件修改

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.example</groupId><artifactId>myshop</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>myshop-user</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.31</version></dependency><!--JPA--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!--hibernate-validate--><dependency><groupId>org.hibernate.validator</groupId><artifactId>hibernate-validator</artifactId></dependency><!--添加common模块的依赖--><dependency><groupId>org.example</groupId><artifactId>myshop-common</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</project>

2.使用JPA注解创建用户实体

import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.UpdateTimestamp;import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.util.Date;/*** @author qx* @date 2023/06/21* @desc 用户实体*/
@Entity
@Table(name = "t_user")
@Data
@DynamicInsert
@DynamicUpdate
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;/*** 用户名*/@NotBlank(message = "用户名不能为空")private String userName;/*** 用户性别:0:女 1:男  2:未知*/private Integer sex;/*** 手机号*/@NotBlank(message = "手机号不能为空")private String phone;/*** 邮箱*/private String email;/*** 地址*/private String address;/*** 积分*/@Column(columnDefinition = "int default 0 comment '积分'")private Integer integral;/*** 等级*/@Column(columnDefinition = "int default 1 comment '等级'")private Integer level;/*** 创建时间*/@CreationTimestampprivate Date createTime;/*** 修改时间*/@UpdateTimestampprivate Date updateTime;/*** 状态 1:正常 2:禁用*/@Column(columnDefinition = "int default 1 comment '状态'")private Integer status;
}

3.创建数据访问层

package com.example.myshop.repository;import com.example.myshop.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;public interface UserRepository extends JpaRepository<User,Long> {
}

4.创建服务层

package com.example.myshop.service;import com.example.myshop.entity.User;
import com.example.myshop.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** @author qx* @date 2023/06/21* @desc 用户服务层*/
@Service
public class UserService {@Autowiredprivate UserRepository userRepository;public void saveUser(User user) {userRepository.save(user);}public User getUserById(Long id) {return userRepository.findById(id).orElse(null);}
}

5.创建控制层

package com.example.myshop.controller;import com.example.myshop.CommonResult;
import com.example.myshop.entity.User;
import com.example.myshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;/*** @author qx* @date 2023/06/21* @desc 用户控制层*/
@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;/*** 添加用户*/@PostMapping("/add")public CommonResult addUser(@RequestBody User user) {userService.saveUser(user);return CommonResult.success();}/*** 根据id获取用户信息** @param id 用户ID* @return 用户信息*/@GetMapping("/{id}")public CommonResult getUserById(@PathVariable Long id) {return CommonResult.success(userService.getUserById(id));}
}

运行启动类,启动项目。

我们在数据库中发现了JPA自动创建的数据表

 我们使用Postman进行测试

 我们刷新下数据表发现了我们新增的数据

 我们再调用用户查询的接口,也返回了我们的数据。

 

 到这里我们基本完成了用户子模块的开发,其他功能可以自己去实现。

本文链接:https://my.lmcjl.com/post/13501.html

展开阅读全文

4 评论

留下您的评论.