需求
- 搭建用户测试服务
基本环境
- 项目名:nacos-config-mysql-2.1
- 添加坐标
<dependencies> <!-- web 启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- nacos 配置--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!--swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> </dependency> <!-- mybatis plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--lombok , @Data 等--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
- 编写yml文件:bootstrap.yml
# 服务端口号 server: port: 7777 # 服务名 spring: application: name: user-service datasource: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/cloud_db5?useUnicode=true&characterEncoding=utf8 username: root password: 1234 cloud: nacos: config: server-addr: 127.0.0.1:8848 # nacos 服务地址 prefix: user # 3.1 前缀,默认 ${spring.application.name} file-extension: yaml # 3.2 后缀 group: DEFAULT_GROUP # 3.3 组名 #开启log4j打印SQL语句 logging: level: com: czxy: changgou4: mapper: debug # mp日志打印 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- 编写sql语句
create database cloud_db5; use cloud_db5; CREATE TABLE t_user( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) ); create database cloud_db6; use cloud_db6; CREATE TABLE t_user( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) );
- 编写启动类
package com.czxy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class,args); } }
- 拷贝配置类
- 编写domain
package com.czxy.domain; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @TableName("t_user") @Data public class User { @TableId(type = IdType.AUTO) //自动增强 private Integer id; private String username; }
- 编写mapper
package com.czxy.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.czxy.domain.User; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper extends BaseMapper<User> { }
- 编写service
- 接口
package com.czxy.service; import com.baomidou.mybatisplus.extension.service.IService; import com.czxy.domain.User; public interface UserService extends IService<User> { }
- 实现类
package com.czxy.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.czxy.domain.User; import com.czxy.mapper.UserMapper; import com.czxy.service.UserService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { }
- 编写controller
package com.czxy.controller; import com.czxy.domain.User; import com.czxy.service.UserService; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @RequestMapping("/user") public class UserController { @Resource private UserService userService; @PostMapping public String save(@RequestBody User user) { //添加 boolean result = userService.save(user); //返回 if(result) { return "添加成功"; } return "添加失败"; } }
测试配置
默认
- 提供数据库:cloud_db5
- 对t_user表进行操作
项目打包
- 以jar包的方式运行(运维人员)
- 修改pom.xml文件,添加插件,用于指定jar的启动类
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--启动类--> <mainClass>com.czxy.UserApplication</mainClass> </configuration> </plugin> </plugins> </build>
打包
运行jar,在cmd命令窗口中
java -jar nacos-config-mysql-2.1-1.0-SNAPSHOT.jar
nacos配置数据库
- 提供数据库:cloud_db6
- 在nacos中配置
server.port: 7776 spring.datasource.url: jdbc:mysql://127.0.0.1:3306/cloud_db6?useUnicode=true&characterEncoding=utf8
- 必须操作:将服务重启 【不需要修改源码】
- 测试