电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Spring Boot實現簡單的增刪改查

瀏覽:7日期:2022-08-25 17:28:25

在pom.xml添加相應的依賴

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions><exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId></exclusion> </exclusions> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 前端使用thymeleaf來代替jsp --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies>

配置文件配置數據庫等

#server server.port=80 #項目名:server.servlet.context-path #spring dataSource spring.datasource.url=jdbc:mysql:///dbgoods?serverTimezone=GMT%2B8&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root mybatis.mapper-locations=classpath:/mapper/*/*.xml #spring log logging.level.com.cy=debug #spring thymeleaf(假如沒有配置也會默認配置,在默認配置中prefix默認值為classpath:/templates/,后綴默認為.html) #不用重啟服務器,網頁就能刷新 spring.thymeleaf.cache=false spring.thymeleaf.prefix=classpath:/templates/pages/ spring.thymeleaf.suffix=.html

數據層添加相應注解實現sql語句(或者通過xml配置來實現)

數據層封裝了商品信息,并提供get和set方法,為Goods類

1.查詢所有數據

@Select('select * from tb_goods') List<Goods> findAll();

2.按照id刪除數據

@Delete('delete from tb_goods where id=#{id}') int deleteById(Integer id);

3.修改數據

(1)修改數據首先要新建一個界面,按照id查找內容,并將查找到的內容顯示到文本框內

@Select('select * from tb_goods where id=#{id}') Goods findById(Integer id);

(2)再添加查找的方法

@Update('update tb_goods set name=#{name},remark=# {remark},createdTime=now() where id=#{id}') int update(Goods goods);

4.新增數據

@Insert('insert into tb_goods(name,remark,createdTime) values (#{name},#{remark},now())') int add(Goods goods);

業務層提供對應接口方法和實現類

1.業務層接口

public interface GoodsService { List<Goods> findObject(); int add(Goods goods); int update(Goods goods); Goods findById(Integer id);}

2.業務層實現類

@Servicepublic class GoodsServiceImpl implements GoodsService { @Autowired private GoodsDao goodsDao; @Override public List<Goods> findObject() { long start=System.currentTimeMillis(); List<Goods> list = goodsDao.findObjects(); long end=System.currentTimeMillis(); System.out.println('query time:'+(end-start)); return list; } @Override public int add(Goods goods) { return goodsDao.add(goods); } @Override public int update(Goods goods) { return goodsDao.update(goods); } @Override public Goods findById(Integer id) { return goodsDao.findById(id); }

控制層寫具體實現

1.跳轉到首頁并且查找所有商品

@RequestMapping('doGoodsUI') public String doGoodsUI(Model model) { List<Goods> list = goodsService.findObject(); model.addAttribute('goods',list); return 'goods'; }

2.業務層實現類

@Servicepublic class GoodsServiceImpl implements GoodsService { @Autowired private GoodsDao goodsDao; @Override public List<Goods> findObject() { long start=System.currentTimeMillis(); List<Goods> list = goodsDao.findObjects(); long end=System.currentTimeMillis(); System.out.println('query time:'+(end-start)); return list; } @Override public int add(Goods goods) { return goodsDao.add(goods); } @Override public int update(Goods goods) { return goodsDao.update(goods); } @Override public Goods findById(Integer id) { return goodsDao.findById(id); }

控制層寫具體實現

1.跳轉到首頁并且查找所有商品

@RequestMapping('doGoodsUI') public String doGoodsUI(Model model) { List<Goods> list = goodsService.findObject(); model.addAttribute('goods',list); return 'goods'; }

2.刪除商品

@RequestMapping('doDeleteById/{id}')// (@PathVariable Integer id)告訴服務器,id拿到的是從網頁上同樣叫id的數據 public String dodeletebyId(@PathVariable Integer id){ int delete = goodsDao.deleteById(id); //doGoodsUI前面沒有加/的話,跳轉的網址是替代了最后一個/后面的內容 return 'redirect:/goods/doGoodsUI'; }

3.修改商品

(1)先將查找出來的商品顯示在文本框中

@RequestMapping('doFindById/{id}') public String doFindByID(@PathVariable Integer id,Model model){ Goods goods = goodsService.findById(id); model.addAttribute('goods',goods); return 'goods-update'; }

(2)實現修改

@RequestMapping('doUpdateGoods') public String doUpdateGoods(Goods goods){ goodsService.update(goods); return 'redirect:/goods/doGoodsUI'; }

4.新增商品

@RequestMapping('doSaveGoods') public String doSaveGoods(Goods goods){ goodsService.add(goods); return 'redirect:/goods/doGoodsUI'; }

前端采用html+thymeleaf模板代替jsp

1.thymeleaf的語法參考: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#link-urls

2.each表示遍歷拿到的數組,goods是從控制層拿到的model的名字

3.id,name和remark與數據庫對應,date要格式化拿到數據,該語法是thymeleaf固定寫法

<tr th:each='g:${goods}'> <td th:text='${g.id}'>1</td> <td th:text='${g.name}'>AAAAAAA</td> <td th:text='${g.remark}'>aa</td> <td th:text='${#dates.format(g.createdTime,’yyyy-MM-dd HH:mm’)}'>aa</td><!-- <td><a href='http://www.hdgsjgj.cn/bcjs/6118.html#' rel='external nofollow' rel='external nofollow' rel='external nofollow' th:href='http://www.hdgsjgj.cn/bcjs/@{/goods/doDeleteById(id=${g.id})}' rel='external nofollow' ><button>刪除</button></a></td>--> <td><a href='http://www.hdgsjgj.cn/bcjs/6118.html#' rel='external nofollow' rel='external nofollow' rel='external nofollow' th:href='http://www.hdgsjgj.cn/bcjs/@{/goods/doDeleteById/{doDeleteById}(doDeleteById=${g.id})}' rel='external nofollow' ><button>刪除</button></a></td> <td><a href='http://www.hdgsjgj.cn/bcjs/6118.html#' rel='external nofollow' rel='external nofollow' rel='external nofollow' th:href='http://www.hdgsjgj.cn/bcjs/@{/goods/doFindById/{id}(id=${g.id})}' rel='external nofollow' ><button>修改</button></a></td></tr>

4.新增商品界面

(1)標簽里的name屬性要和sql語句一致

(2)這里由于數據庫中的id列設置了自增長,所以不需要id屬性,createdTime列使用了now()獲取當前時間,所以也不需要傳值,所以在控制層的doUpdateGoods方法里可以使用封裝好的Goods來接收從html拿到的參數

<form th:action='@{/goods/doSaveGoods}' method='post'> <ul> <li>name:<input type='text' name='name'></li> <li>remark:<textarea rows='3' cols='20' name='remark'></textarea></li> <li><input type='submit' value='Save Goods'></li> </ul></form>

5.修改商品界面

(1)因為id列自增長,所以修改商品信息不需要id這一列,但傳參數有需要一起傳送過去,所以添加了一個輸入框,默認設置為隱藏,將其value設置為id的值

<form th:action='@{/goods/doUpdateGoods}' method='post'> <input type='hidden' name='id' th:value='${goods.id}'> <ul> <li>name:<input type='text' name='name' th:value='${goods.name}'></li> <li>remark:<textarea rows='3' cols='20' name='remark' th:text='${goods.remark}'></textarea></li> <li><input type='submit' value='Update Goods'></li> </ul></form>

以上就是Spring Boot實現簡單的增刪改查的詳細內容,更多關于Spring Boot增刪改查的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 智能垃圾箱|垃圾房|垃圾分类亭|垃圾分类箱专业生产厂家定做-宿迁市传宇环保设备有限公司 | 中控室大屏幕-上海亿基自动化控制系统工程有限公司 | 液压压力机,液压折弯机,液压剪板机,模锻液压机-鲁南新力机床有限公司 | 飞扬动力官网-广告公司管理软件,广告公司管理系统,喷绘写真条幅制作管理软件,广告公司ERP系统 | 航空连接器,航空插头,航空插座,航空接插件,航插_深圳鸿万科 | 温泉机设备|温泉小镇规划设计|碳酸泉设备 - 大连连邦温泉科技 | 上海平衡机-单面卧式动平衡机-万向节动平衡机-圈带动平衡机厂家-上海申岢动平衡机制造有限公司 | 苗木价格-苗木批发-沭阳苗木基地-沭阳花木-长之鸿园林苗木场 | 橡胶弹簧|复合弹簧|橡胶球|振动筛配件-新乡市永鑫橡胶厂 | 一体化隔油提升设备-餐饮油水分离器-餐厨垃圾处理设备-隔油池-盐城金球环保产业发展有限公司 | 涡轮流量计_LWGY智能气体液体电池供电计量表-金湖凯铭仪表有限公司 | 红立方品牌应急包/急救包加盟,小成本好项目代理_应急/消防/户外用品加盟_应急好项目加盟_新奇特项目招商 - 中红方宁(北京) 供应链有限公司 | 四合院设计_四合院装修_四合院会所设计-四合院古建设计与建造中心1 | 贵阳用友软件,贵州财务软件,贵阳ERP软件_贵州优智信息技术有限公司 | 门禁卡_智能IC卡_滴胶卡制作_硅胶腕带-卡立方rfid定制厂家 | 科客,主见不成见| 北京工业设计公司-产品外观设计-产品设计公司-千策良品工业设计 北京翻译公司-专业合同翻译-医学标书翻译收费标准-慕迪灵 | 环球周刊网| 高柔性拖链电缆_卷筒电缆_耐磨耐折聚氨酯电缆-玖泰特种电缆 | 泥浆在线密度计厂家-防爆数字压力表-膜盒-远传压力表厂家-江苏大亚自控设备有限公司 | 氧化锆纤维_1800度高温退火炉_1800度高温烧结炉-南京理工宇龙新材料股份有限公司 | 山东商品混凝土搅拌楼-环保型搅拌站-拌合站-分体仓-搅拌机厂家-天宇 | 淘气堡_室内儿童乐园_户外无动力儿童游乐设备-高乐迪(北京) | 【星耀裂变】_企微SCRM_任务宝_视频号分销裂变_企业微信裂变增长_私域流量_裂变营销 | 超声波_清洗机_超声波清洗机专业生产厂家-深圳市好顺超声设备有限公司 | 中式装修设计_全屋定制家具_实木仿古门窗花格厂家-喜迎门 | 东莞喷砂机-喷砂机-喷砂机配件-喷砂器材-喷砂加工-东莞市协帆喷砂机械设备有限公司 | 组织研磨机-高通量组织研磨仪-实验室多样品组织研磨机-东方天净 传递窗_超净|洁净工作台_高效过滤器-传递窗厂家广州梓净公司 | 玉米深加工设备-玉米深加工机械-新型玉米工机械生产厂家-河南粮院机械制造有限公司 | 拉力测试机|材料拉伸试验机|电子拉力机价格|万能试验机厂家|苏州皖仪实验仪器有限公司 | 磁粉制动器|张力控制器|气胀轴|伺服纠偏控制器整套厂家--台灵机电官网 | 山东臭氧发生器,臭氧发生器厂家-山东瑞华环保设备 | 2-羟基泽兰内酯-乙酰蒲公英萜醇-甘草查尔酮A-上海纯优生物科技有限公司 | 涂层测厚仪_漆膜仪_光学透过率仪_十大创新厂家-果欧电子科技公司 | 玉米深加工设备|玉米加工机械|玉米加工设备|玉米深加工机械-河南成立粮油机械有限公司 | 北京包装设计_标志设计公司_包装设计公司-北京思逸品牌设计 | 非标压力容器_碳钢储罐_不锈钢_搪玻璃反应釜厂家-山东首丰智能环保装备有限公司 | 上海新光明泵业制造有限公司-电动隔膜泵,气动隔膜泵,卧式|立式离心泵厂家 | 中控室大屏幕-上海亿基自动化控制系统工程有限公司 | 铁盒_铁罐_马口铁盒_马口铁罐_铁盒生产厂家-广州博新制罐 | 薪动-人力资源公司-灵活用工薪资代发-费用结算-残保金优化-北京秒付科技有限公司 |