一、介绍
用户的登录注册,修改个人信息,更改密码,找回密码等功能在该模块完成
二、创建用户模块
1.1、选择需要的依赖
1.2、主程序配置
添加注解@EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class HbuLiveUserApplication {
public static void main(String[] args) {
SpringApplication.run(HbuLiveUserApplication.class, args);
}
}
1.3、配置文件
server.port=1111
spring.application.name=hbu-live-user
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/live?useSSL=true&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
三、启动
四、编写一个接口(这里没有实际功能,是为了后面调通接口测试)
4.1 数据库结构
4.1.1 创建数据库 名称为live
4.1.2 导入sql文件
地址:https://github.com/CoqCow/hbu-live-user/tree/master/src/main/resources
4.1.3 导入后在user表手动添加几干行数据
4.2 添加两个依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
第一个依赖:feign服务发现的一种方式 下一节会讲到
第二个依赖:lombok 可通过注解直接生成get set tostring 构造等方法
4.3 user实体类
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.persistence.*;
import java.util.Date;
@Getter
@Setter
@ToString
@Entity(name = "user")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "userid")
private int userId;
@Column(name = "organizationname")
private String organizationName;
private int number;
private String password;
private String name;
private String college;
@Column(name = "nickname")
private String nickName;
private String avatar;
private String signature;
@Column(name = "phonenumber")
private String phoneNumber;
@Column(name = "whetheranchor")
private int whetherAnchor;
@Column(name = "follownumber")
private int followNumber;
@Column(name = "registrationtime")
private Date registrationTime;
}
4.4 dao层
使用jpa操作数据库很简单,这样就可以进行对应表的增删改查操作了
public interface UserRepository extends CrudRepository<User, Integer> {
User findUserByUserId(int userId);
User findUserByNameAndPassword(String name,String password);
}
4.5 service 层
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User getUserByUserId(int userId) {
return userRepository.findUserByUserId(userId);
}
}
4.6 controller层
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity getUser(@RequestParam("userId") int userId) {
User user = null;
user = userService.getUserByUserId(userId);
return ResponseEntity.ok().body(user);
}
}
4.7 访问 http://127.0.0.1:1111/users?userId=1
如果数据库有数据会返回相应 json串
五、其他模块
主播模块、直播模块创建过程类似,根据业务进行改进
下一节介绍,总后端微服务的创建,通过feign服务发现的方式,调用user模块