springboot - mybatis 搭建

  1. 首先新建springboot


    1613982538(1).png

    2.dependencies 选择 spring Web
    3.然后在pom.xml中加入


            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1
        

        
            mysql
            mysql-connector-java
        

        
            org.projectlombok
            lombok
        

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> findByName(@Param("name") String name);

    @Select("SELECT * FROM lc_user WHERE wxappOpenid = #{wxappOpenid}")
    Map findByOpenId(@Param("wxappOpenid") String wxappOpenid);

    @Select("SELECT * FROM lc_user WHERE id = #{id}")
    Map findByStringId(@Param("id") String id);

    @Select("SELECT * FROM lc_user WHERE phone = #{phone}")
    List> 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 user = userMapper.findByStringId(userId);
        Map cert = certMapper.findByStringUserId(userId);
        user.put("faceImg", cert.get("faceImg"));
        return nationalPolicyResult.success(user);
    }
}

你可能感兴趣的:(springboot - mybatis 搭建)