手写Springboot+JPA的Demo

Demo地址https://git.coding.net/Hui20/Springboot_JPADemo.git

Demo的层次结构

手写Springboot+JPA的Demo_第1张图片

Maven依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    
    com.springbootDemo1
    springboot_jpa
    0.0.1-SNAPSHOT
    springboot_jpa
    Demo project for Spring Boot

    
        1.8
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.projectlombok
            lombok
            true
        

        
        
            mysql
            mysql-connector-java
        
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


User.java

package com.springbootdemo1.domain;

import lombok.Data;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.Date;

@Entity // jpa的注解,需要加
@Table(name = "table_user") // 指定数据库的表名
@Data // lombok
public class User {
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String userId;

    private String userNickname;

    private String userPassword;

    private String userEmail;

    private Integer actiState;

    //激活成功与激活失败常量
    public static final int ACTIVATION_SUCCESSFUL = 1;
    public static final int ACTIVATION_UNSUCCESSFUL = 0;

    private String actiCode;

    private Date tokenExptime;
}

UserRepository.java

package com.springbootdemo1.dao;

import com.springbootdemo1.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository {
}

UserService.java

package com.springbootdemo1.service;

import com.springbootdemo1.domain.User;

import java.util.List;

public interface UserService {
    List getAllUser();
}

UserServiceImpl.java

package com.springbootdemo1.service.impl;

import com.springbootdemo1.dao.UserRepository;
import com.springbootdemo1.domain.User;
import com.springbootdemo1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;
    @Override
    public List getAllUser() {
        return userRepository.findAll();
    }
}

UserController.java

package com.springbootdemo1.controller;

import com.springbootdemo1.service.UserService;
import com.springbootdemo1.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 得到所有用户
     */
    @GetMapping(value = "/user", produces = {"application/json;charset=UTF-8"})
    public void  getAllUser () {
        List allUser = userService.getAllUser();
        for (User user : allUser) {
            System.out.println(user);
        }
    }
}

 

你可能感兴趣的:(Spring,Spring,Data,JPA)