zoukankan      html  css  js  c++  java
  • Spring_Boot 简单例子

     第一步创建项目:

     创建项目地址:https://start.spring.io/

     接下来就下载到本地了 跟着加压

    接着用idea打开:等待资源下载完成

    我写了个简单的:增删改查

    项目结构:

    dao层:

    package com.nf147.demo.dao;
    
    import com.nf147.demo.entity.News;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    
    public interface NewsMapper extends JpaRepository<News,Integer> {  //第一个参数是实体类,第二个是id的类型
    }

    entity层:

    package com.nf147.demo.entity;
    
    import javax.persistence.*;
    
    @Entity
    @Table(name = "news") //表名
    public class News {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)  //标明该字段是自动增长
        private int id;
        private String title;
        private String body;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public String getBody() {
            return body;
        }
    
        public void setBody(String body) {
            this.body = body;
        }
    }

    service层:

    package com.nf147.demo.service;
    
    import com.nf147.demo.entity.News;
    
    import java.util.List;
    
    public interface NewsService {
        List<News> listAll();
        void add (News news);
        void del (int id);
        void update(News news);
    }

    实现服务接口:

    package com.nf147.demo.service;
    
    
    import com.nf147.demo.dao.NewsMapper;
    import com.nf147.demo.entity.News;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class NewsServiceImp implements NewsService {
    
        @Autowired
        private NewsMapper newsMapper;
    
    
        @Override
        @Cacheable("listNews")
        public List<News> listAll() {
            return newsMapper.findAll();
        }
    
        @Override
        public void add(News news) {
            newsMapper.save(news);
    
        }
    
        @Override
        public void del(int id) {
            newsMapper.deleteById(id);
        }
    
        @Override
        public void update(News news) {
            newsMapper.save(news);
        }
    }

    controller层:

    package com.nf147.demo.controller;
    
    import com.nf147.demo.entity.News;
    import com.nf147.demo.service.NewsServiceImp;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    @RestController
    
    public class NewsController {
    
        @Autowired
        private NewsServiceImp newsServiceImp;
    
        //查询
        @RequestMapping(value = "/listNews", method = RequestMethod.GET)
        public List<News> getNews() {
            return newsServiceImp.listAll();
        }
    
        //添加 http://localhost:8082/listNewsAdd?title=标题&body=随便给的内容
        @RequestMapping(value = "/listNewsAdd", method = RequestMethod.GET)
        public void add(News news) {
            newsServiceImp.add(news);
        }
    
        //删除
        //地址栏写法 http://localhost:8082/listNewsdel?id=7
        @RequestMapping(value = "/listNewsdel", method = RequestMethod.GET)
        public void del(int id) {
            newsServiceImp.del(id);
        }
    
        //修改
        //地址栏写法  http://localhost:8082/listNewsupdate?id=6&title=好好&body=学习
        @RequestMapping(value = "/listNewsupdate", method = RequestMethod.GET)
        public void del(News news) {
            newsServiceImp.update(news);
        }
    
    }

    测试:

    项目下载地址:https://github.com/nongzihong/Spring_Boot

  • 相关阅读:
    测试amqplib实例,报错 Error: connect ECONNREFUSED 127.0.0.1:5672
    启动vue项目,npm run dev服务起不来报错Error: listen EACCES 0.0.0.0:8080
    win10上安装Docker
    mongodb存储过程
    Versions 出现 SVN Working Copy xxx locked
    Mac OSX Versions输入username按1下都会出现2个字符,并且不能create,解决方法
    Mac OSX 安装nvm(node.js版本管理器)
    jade模板引擎学习笔记(WebsStorm9.0.3+ nodejs+express+jade)
    asp.net首页设置
    两个大数组foreach,找出相同的key数量,所用的时间对比
  • 原文地址:https://www.cnblogs.com/nongzihong/p/10199790.html
Copyright © 2011-2022 走看看