1、建库建表
1.1、新建数据库
为了简化操作,演示里所有子模块都连接同一个数据库,在实际项目中,可以根据需要进行数据库的组织,不同的子模块可以各自连接不同的数据库。
此演示中,数据库名为: edu ;库里只建立一个测试表: user ,通过编写 user 微服务的数据库功能来演示操作过程。
Sql语句如下:
DROP DATABASE IF EXISTS edu;
CREATE DATABASE edu DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
1.2、新建数据表
新建用户表: user , Sql 语句如下:
DROP TABLE IF EXISTS user;
CREATE TABLE user (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(255) DEFAULT NULL,
password varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
);
INSERT INTO user VALUES (1, '金庸', '123456');
INSERT INTO user VALUES (2, '古龙', '654321');
2、添加依赖及配置文件
2.1、添加依赖
修改 microsvc 的 pom.xml 文件,增加连接 mysql 需要的 MySql 及 JPA 组件依赖,以后 microsvc 下所有的微服务子项目都可以拥有连接 mysql 的组件了。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
修改后的结果如下图:

2.2、修改 yml 配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/edu?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
jpa:
show-sql: true
修改后的结果如下图

注意:用户名、密码是我测试机里的 mysql 配置,需要根据实际情况换成您的正确配置。
3、编写代码
3.1、创建实体类
在 com.tanbushi.edu.microsvc.user 下新建包 entity

在 entity 包里面新建实体类:UserEntity,映射 User 数据表。

编写实体类的代码如下:
package com.tanbushi.edu.minisvc.user.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* Created by Administrator on 2019/5/26.
*/
@Entity(name="user")
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserEntity(){
}
}
3.2、创建一个 Repository 接口类,继承JpaRepository类
在 com.tanbushi.edu.microsvc.user 下新建包 repository;
在 包 repository 下新建接口类: UserRepository ,文件为: UserRepository.java ,内容如下:
package com.tanbushi.edu.microsvc.user.repository;
import com.tanbushi.edu.microsvc.user.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Created by Administrator on 2019/5/28.
*/
public interface UserRepository extends JpaRepository<UserEntity,Integer>{
}
3.3、编写 Controller ,实现 User 数据表的增删改查
在 com.tanbushi.edu.microsvc.user 下如果没有 controller包,则新建包 controller;
在 包 controller 下如果没有类 UserController ,则新建类: UserController ,文件为: UserController.java ,内容如下:
package com.tanbushi.edu.microsvc.user.controller;
import com.tanbushi.edu.microsvc.user.entity.UserEntity;
import com.tanbushi.edu.microsvc.user.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* Created by Administrator on 2019/5/27.
*/
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
/**
* 查
* @return
*/
@GetMapping(value = "/list")
public List<UserEntity> getUserList(){
return userRepository.findAll();
}
/**
* 增
* @param username
* @param password
* @return
*/
@PostMapping(value = "/addUser")
public UserEntity addUser(@RequestParam("username") String username,
@RequestParam("password") String password){
UserEntity userEntity = new UserEntity();
userEntity.setUsername(username);
userEntity.setPassword(password);
return userRepository.save(userEntity);
}
/**
* 改
* @param id
* @param username
* @param password
* @return
*/
@PutMapping(value = "updUser/{id}")
public UserEntity updUser(@PathVariable("id") Integer id,
@RequestParam("username") String username,
@RequestParam("password") String password){
UserEntity userEntity = new UserEntity();
userEntity.setId(id);
userEntity.setUsername(username);
userEntity.setPassword(password);
return userRepository.save(userEntity);
}
/**
* 删
* @param id
*/
@DeleteMapping(value = "delUser/{id}")
public void delUser(@PathVariable("id") Integer id){
UserEntity userEntity = new UserEntity();
userEntity.setId(id);
userRepository.delete(userEntity);
}
}
运行用户微服务模块的启动程序 UserApplication


4、测试微服务的数据库操作
利用 Postman 进行 http 通讯测试
4.1、查数据

4.2、增加数据

注意:127.0.0.1/addUser 里的大小写,否者可能会添加失败!
4.3、修改数据

4.4、删除数据


此篇主要是验证 springboot 数据库的基本操作,虽逻辑不严谨,但验证成功! RestApi 的规范也没严格遵循,在下一篇会专门说一下 RestApi 规范及开发实现!
上一篇:Spring Cloud 零基础实战(四):最简单的多模块应用框架搭建
下一篇:Spring Cloud 零基础实战(六):Rest Api 开发
所属文集:Spring Cloud 零基础实战
网友评论