spring boot notes -01

  • model (bottom)
    is directly connected to the database, the database can be built form, directly in the code field and the like
@Entity
//代替get,set方法,写了@Data之后就不用get,set
@Data
public class User {
    @Id//主键
    @GeneratedValue(strategy= GenerationType.AUTO)//id自增
    public Integer id;

    public String username;

    public String password;

    public String openid;

}
  • repository (on the floor model)
    invoke a method on a database in the above layer needs to be defined in advance here, as there should be in accordance with openID you need to look here first define a findByOpenid
//User为表名
public interface UserRepo extends JpaRepository<User,Integer> {

   User findByOpenid(String opid);
}
  • service layer (the penultimate layer)
    is mainly used when some method to achieve a lot of times when, carried a package? Anyway, that is, the same set of code together, for easy recall of view and so on. (Not yet in contact)
  • controller (top layer)
    for implementing each method? Page returns a numerical example, a method of direct call other layers in this place for CRUD, and then returns to the data page.
@RestController
public class TestController {
    @Autowired
    private UserRepo userRepo;

    /**
     * 1.接收到前端传递回的openid ,
     * 2.根据openid查询数据库表,看该id是否登录
     * 3.如已登录,则返回已登录状态
     * 4.如未登录,且该openid查不到,创建一行该openid的记录,并通知前端,该openid用户
     * 为第一次登录,需要进行用户注册,(填报个人信息之后,需要信息产业中心对后端权限进行相应的设置)
     * 5.如已登录,则返回已登录信息
     * @param userDto
     * @return simpleresponse code 0对应3 1对4 2对应5 body msg 已登录 未登录  新用户
     */
    @PostMapping("/smapplogin")///这里是网页的地址,post是指从前端返回数据,get是指从服务器把东西给前端*********
    public SimpleResponse testSmAppLogin(@RequestBody UserDto userDto){ ///***
        System.out.println("&&&&&&&&&&&&&&&&&="+userDto.openid);//调取前端post来的数据

        userDto.openid = "123456";


        /**
         * 操作数据库 c
         */
        User user = new User();
        user.username="123";
        user.password="456";

        userRepo.save(user);
//        User theOne = userRepo.findByOpenid("fdsafds");
//        theOne.status;
//        if(status == ""){
//            return
//        }else{
//            return
//        }
        //创建返回的数据,回复给前端
         SimpleResponse simpleResponse = new SimpleResponse();//SimpleResponse是指返回给前端的字段的定义(在dto中定义了)
         simpleResponse.body=userRepo.findAll();///******
         simpleResponse.code="200";
         simpleResponse.msg="请求成功";

        return simpleResponse;
    }
    @GetMapping("/hello/{id}")
    public String test(@RequestParam("id") String id){
//        id
        return "return";
    }
}
  • DTO (definition data to the front end of field)
    as field UserDto define the front end of the return
@Data
public class UserDto {
    public String openid;
    public String msg;
}

SimpleResponse means is a defined field returns to the front end, for example three fields is transmitted to the front end

@Data
public class SimpleResponse {
    public String code;
    public Object body;//什么东西都可以往里面装
    public String msg;

}

Released eight original articles · won praise 0 · Views 138

Guess you like

Origin blog.csdn.net/leelelea/article/details/104249167