一篇打通微服务架构,Nacos + Gateway + Redis + MySQL + Docker( 三 )

2、配置文件 spring:application:name: managementcloud:nacos:discovery:server-addr: 127.0.0.1:8848config:server-addr: 127.0.0.1:8848file-extension: ymlext-config:- data-id: datasource-share-config.ymlgroup: SHARE_GROUPrefresh: true- data-id: log-share-config.ymlgroup: SHARE_GROUPrefresh: true server:port: 8081 spring:application:name: managementversion: 1.0.0mvc:static-path-pattern: /management/**resources:static-locations:- file:../../web/management- file:../../web/common3、启动类 package com.guor; import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.context.config.annotation.RefreshScope; @EnableDiscoveryClient@SpringBootApplication(scanBasePackages = "com.guor")@MapperScan("com.guor.management.dao")@RefreshScopepublic class ManagementApplication {public static void main(String[] args) {SpringApplication.run(ManagementApplication.class, args);} }

一篇打通微服务架构,Nacos + Gateway + Redis + MySQL + Docker

文章插图
 
五、整合mybatis1、user表设计数据库选择的是最常用的MySQL 。
 CREATE TABLE `user` (`user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,`username` varchar(100) NOT NULL,`password` varchar(40) NOT NULL,`age` int(11) DEFAULT NULL,`sex` int(11) DEFAULT NULL,`telephone` varchar(100) DEFAULT NULL,`address` varchar(100) DEFAULT NULL,`create_date` date DEFAULT NULL,`update_date` date DEFAULT NULL,`deleted` int(11) DEFAULT NULL,`version` int(11) DEFAULT NULL,PRIMARY KEY (`user_id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;2、UserController package com.guor.management.controller; import com.guor.management.bean.User;import com.guor.management.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PutMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestControllerpublic class UserController {@Autowiredprivate UserService userService;@GetMapping("/getUserList")public List<User> getUserList(){return userService.getUserList();}@PutMapping("/insertUser")public void insertUser(@RequestBody User user){userService.insertUser(user);}}3、UserService package com.guor.management.service; import com.guor.management.bean.User; import java.util.List; public interface UserService {List<User> getUserList();void insertUser(User user);} package com.guor.management.service.impl; import com.guor.management.bean.User;import com.guor.management.dao.UserMapper;import com.guor.management.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service; import java.util.List; @Servicepublic class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic List<User> getUserList() {return userMapper.getUserList();}@Overridepublic void insertUser(User user) {userMapper.insertUser(user);}}4、UserMapper package com.guor.management.dao; import com.guor.management.bean.User; import java.util.List; public interface UserMapper {public List<User> getUserList();public void insertUser(User user);} <?xml versinotallow="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace="com.guor.management.dao.UserMapper"><select id="getUserList" resultType="java.util.LinkedHashMap">select * from user;</select><insert id="insertUser" parameterType="com.guor.management.bean.User">INSERT INTO gooreey.`user` (username, password) VALUES (#{username}, #{password});</insert></mapper>5、User package com.guor.management.bean; import com.guor.base.bean.BaseBean;import lombok.Data; @Datapublic class User extends BaseBean {private Integer userId;private String username;private String password;private Integer age;private Integer sex;private String telephone;private String address;} package com.guor.base.bean; import lombok.Data; import java.util.Date; @Datapublic class BaseBean {private Date createDate;private Date updateDate;private Integer deleted;private Integer version;}6、postman接口测试 
一篇打通微服务架构,Nacos + Gateway + Redis + MySQL + Docker

文章插图
 
本文转载自微信公众号「哪吒编程」




推荐阅读