-
首先新建springboot
2.dependencies 选择 spring Web
3.然后在pom.xml中加入
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
4.在application.properties中加入
spring.datasource.url=jdbc:mysql://localhost:3306/zdlc
spring.datasource.username=root
spring.datasource.password=2YteBLBfHgl52KNa
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
5.新建model层
package com.zdlc.naifen.model;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class User {
private Long id;
private String wxappOpenid;
private String phone;
private String nickName;
private String avatar;
private String api_token;
public User(String nickName, String phone, String api_token, String avatar, String wxappOpenid) {
this.nickName = nickName;
this.phone = phone;
this.api_token = api_token;
this.avatar = avatar;
this.wxappOpenid = wxappOpenid;
}
}
6.新建mapper
package com.zdlc.naifen.mapper;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Map;
@Mapper
public interface UserMapper {
@Select("SELECT * FROM lc_user WHERE nickName = #{name}")
List<Map<String,Object>> findByName(@Param("name") String name);
@Select("SELECT * FROM lc_user WHERE wxappOpenid = #{wxappOpenid}")
Map<String,Object> findByOpenId(@Param("wxappOpenid") String wxappOpenid);
@Select("SELECT * FROM lc_user WHERE id = #{id}")
Map<String,Object> findByStringId(@Param("id") String id);
@Select("SELECT * FROM lc_user WHERE phone = #{phone}")
List<Map<String,Object>> findByPhone(@Param("phone") String phone);
}
7.controller层调用
package com.zdlc.naifen.controller;
import com.auth0.jwt.JWT;
import com.zdlc.naifen.mapper.CertMapper;
import com.zdlc.naifen.mapper.UserMapper;
import com.zdlc.naifen.until.NationalPolicyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@RestController
@RequestMapping(value = "/users")
public class UserController {
@Autowired
private UserMapper userMapper;
@Autowired
private CertMapper certMapper;
private NationalPolicyResult nationalPolicyResult;
@GetMapping("/")
public String user() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
String token = request.getHeader("token");
JWT jwt = JWT.decode(token);
String userId = jwt.getClaim("userId").asString();
Map<String,Object> user = userMapper.findByStringId(userId);
Map<String, Object> cert = certMapper.findByStringUserId(userId);
user.put("faceImg", cert.get("faceImg"));
return nationalPolicyResult.success(user);
}
}