发表于: 2019-12-31 20:36:42
1 1060
今天完成的事:
1、简单查看微信静默授权,获取用户openId。
也就是说,这里只是将微信授权的步骤代替了正常注册的步骤,会返回一个openId,相当于主键id来区分用户,而后用户根据自己的个人信息进行相应的昵称等修改。
2、拆分各层,创建springcloud项目。
本来是打算将实体类、dao层、service接口分为一个基础模块,service实现类分为一个业务逻辑模块,controller层一个模块,但是在调用mapper的bean时,总是无法调取,试了很多,添加注解啥的,都不管用。
最后,按照一个例子,将实体类、service接口分为一个被依赖的基础模块common,dao层、service实现类分为一个模块,controller层一个模块。当然还有服务注册中心模块。
但是这是在service接口上添加 @FeginClient 注解,在service实现类上添加 @RestController 注解,这就相当于将 feign 创建的自定义接口,以及调用自己接口的控制层融合了,就显得代码很混乱。
正常应该是dao层、service层、controller层一个模块,服务调用的feign模块会自定义一个接口,来对应服务提供者的controller接口,再创建一个controller来调用自己自定义的接口,这样一层一层结构清晰,只是这样如果再将单体结构分为几个模块,分的模块就太多了。。
(1)基础模块common:
pom文件中添加feign依赖,因为需要在service接口指定调用的客户端(service实现类所在模块),删除不必要的测试类、启动类、资源包。
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springcloud2</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 后添加 -->
<packaging>jar</packaging>
<name>common</name>
<description>Demo project for Spring Boot</description>
<!-- 后添加,父模块 -->
<parent>
<groupId>com.springcloud2</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
实体类就不放了,正常的实体类。
service接口:
添加 @FeignClient 注解、@RequestMapping注解,并且需要注意在入参内添加 @RequestParam 注解。
package com.springcloud2.common.service;
import com.springcloud2.common.pojo.Direction;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@FeignClient(name = "admin-service")
public interface DirectionService {
// 按开发方向查询
@RequestMapping(value = "/u/job",method = RequestMethod.GET )
List<Direction> selectByDirection(@RequestParam("direction") String direction);
}
(2)业务逻辑模块(依赖common模块)
dao层、service实现类分为一个模块,这里就是客户端client。
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springcloud2</groupId>
<artifactId>admin-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 后添加 -->
<packaging>jar</packaging>
<name>admin-service</name>
<description>Demo project for Spring Boot</description>
<!-- 后添加,父模块 -->
<parent>
<groupId>com.springcloud2</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- common 模块 -->
<dependency>
<groupId>com.springcloud2</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 连接池依赖包-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
application.yml文件:
server:
port: 8891
spring:
application:
name: admin-service
datasource:
url: jdbc:mysql://localhost:3306/xzy_task4?characterEncoding=UTF-8
username: root
password: Zhang123!
driver-class-name: com.mysql.jdbc.Driver
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
mybatis:
mapper-locations: classpath:com.springcloud2.adminservice.dao/*.xml
启动类:
package com.springcloud2.adminservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class AdminServiceApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServiceApplication.class, args);
}
}
dao接口:
xml文件中的sql语句需要注意实体类的路径,以及dao接口的路径。
package com.springcloud2.adminservice.dao;
import com.springcloud2.common.pojo.Direction;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import java.util.List;
@Mapper
@Component("directionMapper")
public interface DirectionMapper {
// 按开发方向查询
List<Direction> selectByDirection(String direction);
}
service实现类:
package com.springcloud2.adminservice.service.impl;
import com.springcloud2.adminservice.dao.DirectionMapper;
import com.springcloud2.common.pojo.Direction;
import com.springcloud2.common.service.DirectionService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class DirectionServiceImpl implements DirectionService {
@Resource(name = "directionMapper")
DirectionMapper directionMapper;
@Override
@RequestMapping(value = "/u/job",method = RequestMethod.GET )
public List<Direction> selectByDirection(@RequestParam("direction") String direction) {
List<Direction> directions = directionMapper.selectByDirection(direction);
return directions;
}
}
(3)控制层模块(依赖common模块)
controller层为一个模块,是feign服务调用者。
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springcloud2</groupId>
<artifactId>admin-controller</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 后添加 -->
<packaging>jar</packaging>
<name>admin-controller</name>
<description>Demo project for Spring Boot</description>
<!-- 后添加,父模块 -->
<parent>
<groupId>com.springcloud2</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- common 模块 -->
<dependency>
<groupId>com.springcloud2</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
application.yml文件
server:
port: 8892
spring:
application:
name: admin-controller
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
启动类
package com.springcloud2.admincontroller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.springcloud2.common")
public class AdminControllerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminControllerApplication.class, args);
}
}
controller类:
package com.springcloud2.admincontroller.controller;
import com.springcloud2.common.pojo.Direction;
import com.springcloud2.common.service.DirectionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class DirectionController {
private static final Logger logger = LoggerFactory.getLogger("DirectionController");
@Resource
DirectionService directionService;
@RequestMapping(value = "/u/job",method = RequestMethod.GET )
public Map selectByDirection(@RequestParam(value = "name") String name){
List<Direction> javaList = directionService.selectByDirection("后端方向");
for (Direction java : javaList) {
System.out.println(java);
}
Map<String,Object> map = new HashMap<>();
map.put("javaList",javaList);
map.put("name",name);
return map;
}
}
成功运行。
明天计划的事:
SVN上传、求学大作战方案设计
遇到的问题:
无
收获:
1、简单查看微信静默授权,获取用户openId。
2、将springcloud基础模块common打包。
3、拆分各层,创建springcloud项目。
评论