关于java中的知识是非常繁杂的,这往往需要大家花费很多的时间和精力去学习。今天就来为大家介绍一下,java spring
boot后端实现怎么做?一起来了解一下吧。
1.首先是创建实体类,代码展示如下:
import javax.persistence.Entity; import javax.persistence.Id; @Entity public class User { @Id private Long id; private String userName; private String password; private String name; //添加 get 和set 方法 }
2.创建DAO接口,代码展示如下:
import org.springframework.data.jpa.repository.JpaRepository; import cn.cnn.info.pojo.User; public interface UserDao extends JpaRepository < User, Long > {}
3.创建业务逻辑接口,代码展示如下:
import java.util.List; import cn.cnn.info.pojo.User; public interface UserService { List < User > findAll(); }
4.创建业务逻辑实现类,代码展示如下:
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import cn.cnn.info.dao.UserDao; import cn.cnn.info.pojo.User; import cn.cnn.info.service.UserService; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public List < User > findAll() { List < User > list = this.userDao.findAll(); return list; } }
5.创建Controller,代码展示如下:
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import cn.cnn.info.pojo.User; import cn.cnn.info.service.UserService; @RestController @RequestMapping("user") public class UserControlelr { @Autowired private UserService userService; @RequestMapping("list") public List < User > queryUserAll() { List < User > list = this.userService.findAll(); return list; } }
6.创建引导类,代码展示如下:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
以上就是关于java spring boot后端实现怎么做的实例展示了。如果你对java知识感兴趣,想要了解更多java实例,敬请关注奇Q工具网。
推荐阅读: