博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring_Boot 简单例子
阅读量:3935 次
发布时间:2019-05-23

本文共 3295 字,大约阅读时间需要 10 分钟。

 

 第一步创建项目:

 创建项目地址:

 

 

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

 

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

1470521-20181230155951497-1814316679.pnguploading.4e448015.gif转存失败

 

 

 

 

 

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

项目结构:

 

 

dao层:

package com.nf147.demo.dao;import com.nf147.demo.entity.News;import org.springframework.data.jpa.repository.JpaRepository;public interface NewsMapper extends JpaRepository
{ //第一个参数是实体类,第二个是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
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;@Servicepublic class NewsServiceImp implements NewsService {    @Autowired    private NewsMapper newsMapper;    @Override    @Cacheable("listNews")    public List
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;@RestControllerpublic class NewsController {    @Autowired    private NewsServiceImp newsServiceImp;    //查询    @RequestMapping(value = "/listNews", method = RequestMethod.GET)    public List
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); }}

 

 

测试:

1470521-20181230164209621-750201006.gifuploading.4e448015.gif转存失败

 

项目下载地址:

 

 
 
 
 
 
 
 
posted @ 2018-12-30 16:24 阅读(...) 评论(...)

转载地址:http://zthgn.baihongyu.com/

你可能感兴趣的文章
1分钟学会用git管理代码
查看>>
git服务端配置
查看>>
印刷工艺- 喷墨印刷
查看>>
纸张大小、规格、度量详解
查看>>
常用纸张规格介绍
查看>>
印刷工艺流程
查看>>
印刷业ERP启蒙
查看>>
如何正确实施印刷业ERP(二)
查看>>
如何正确实施印刷业ERP(一)
查看>>
[读书社区]值得珍藏的81句名言...
查看>>
ASCⅡ码表
查看>>
Server-U4架设FTP服务器
查看>>
电脑端口基础知识
查看>>
富人和穷人的12大经典差异
查看>>
市场营销学经典语句摘录!
查看>>
谈IT技术人员的创业---作者:李立辉-----转自www.01ch.net
查看>>
背单词最科学的方法
查看>>
杨凡老师谈怎样背好单词
查看>>
很有用的哦。。历年考研英语翻译精解(2003-1994)
查看>>
如何突破英语口语  
查看>>