项目实战:新增@Controller和@Service@Repository@Autowire四个注解

本文主要是介绍项目实战:新增@Controller和@Service@Repository@Autowire四个注解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、@Controller

package com.csdn.mymvc.annotation;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Controller {
}

2、@Service

package com.csdn.mymvc.annotation;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Service {
}

3、@Repository

package com.csdn.mymvc.annotation;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Repository {
}

4、@Autowire

package com.csdn.mymvc.annotation;
import java.lang.annotation.*;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Autowire {
}

 1、FruitController

package com.csdn.fruit.servlet;
import com.csdn.fruit.dto.PageInfo;
import com.csdn.fruit.dto.PageQueryParam;
import com.csdn.fruit.dto.Result;
import com.csdn.fruit.pojo.Fruit;
import com.csdn.fruit.service.FruitService;
import com.csdn.fruit.util.RequestUtil;
import com.csdn.fruit.util.ResponseUtil;
import com.csdn.mymvc.annotation.*;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
@RequestMapping("/fruit")
public class FruitController {@Autowireprivate FruitService fruitService;@GetMapping("/index")protected void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Integer pageNo = 1;String pageNoStr = req.getParameter("pageNo");if (pageNoStr != null && !"".equals(pageNoStr)) {pageNo = Integer.parseInt(pageNoStr);}String keyword = "";String keywordStr = req.getParameter("keyword");if (keywordStr != null) {keyword = keywordStr;}PageQueryParam pageQueryParam = new PageQueryParam(pageNo, 5, keyword);PageInfo<Fruit> pageInfo = fruitService.getFruitPageInfo(pageQueryParam);Result result = Result.OK(pageInfo);ResponseUtil.print(resp, result);}@PostMapping("/add")protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Fruit fruit = (Fruit) RequestUtil.readObject(req, Fruit.class);fruitService.addFruit(fruit);ResponseUtil.print(resp, Result.OK());}@GetMapping("/del")protected void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Integer fid = Integer.parseInt(req.getParameter("fid"));fruitService.delFruit(fid);ResponseUtil.print(resp, Result.OK());}@GetMapping("/edit")protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Integer fid = Integer.parseInt(req.getParameter("fid"));Fruit fruit = fruitService.getFruitById(fid);ResponseUtil.print(resp, Result.OK(fruit));}@GetMapping("/getFname")public void getFname(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String fname = req.getParameter("fname");Fruit fruit = fruitService.getFruitByFname(fname);ResponseUtil.print(resp, fruit == null ? Result.OK() : Result.Fail());}@PostMapping("/update")protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Fruit fruit = (Fruit) RequestUtil.readObject(req, Fruit.class);fruitService.updateFruit(fruit);ResponseUtil.print(resp, Result.OK());}
}

2、FruitServiceImpl

package com.csdn.fruit.service.impl;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.dto.PageInfo;
import com.csdn.fruit.dto.PageQueryParam;
import com.csdn.fruit.pojo.Fruit;
import com.csdn.fruit.service.FruitService;
import com.csdn.mymvc.annotation.Autowire;
import com.csdn.mymvc.annotation.Service;
import java.util.List;
@Service
public class FruitServiceImpl implements FruitService {@Autowireprivate FruitDao fruitDao ;@Overridepublic PageInfo<Fruit> getFruitPageInfo(PageQueryParam pageQueryParam) {Integer pageNo = pageQueryParam.getPageNo();Integer pageSize = pageQueryParam.getPageSize();String keyword = pageQueryParam.getKeyword();List<Fruit> fruitList = fruitDao.getFruitList(pageNo, pageSize, keyword);Integer recordCount = fruitDao.getRecordCount(pageQueryParam.getKeyword());PageInfo<Fruit> fruitPageInfo = new PageInfo<>(fruitList, pageNo, recordCount);return fruitPageInfo;}@Overridepublic void addFruit(Fruit fruit) {if (fruitDao.getFruitByFname(fruit.getFname()) == null) {fruitDao.addFruit(fruit);}}@Overridepublic void updateFruit(Fruit fruit) {fruitDao.updateFruit(fruit);}@Overridepublic void delFruit(Integer fid) {fruitDao.delFruitByFid(fid);}@Overridepublic Fruit getFruitById(Integer fid) {return fruitDao.getFruitByFid(fid);}@Overridepublic Fruit getFruitByFname(String fname) {return fruitDao.getFruitByFname(fname);}
}

3、FruitDaoImpl

package com.csdn.fruit.dao.impl;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.pojo.Fruit;
import com.csdn.mymvc.annotation.Repository;
import com.csdn.mymvc.dao.BaseDao;
import java.util.List;
@Repository
public class FruitDaoImpl extends BaseDao<Fruit> implements FruitDao {@Overridepublic void addFruit(Fruit fruit) {String sql = "insert into t_fruit values (0,?,?,?,?)";super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark());}@Overridepublic void delFruit(String fname) {String sql = "delete from t_fruit where fname=?";super.executeUpdate(sql, fname);}//通过 fid 删除水果库存记录@Overridepublic void delFruitByFid(Integer fid) {super.executeUpdate("delete from t_fruit where fid = ? ", fid);}//通过 fid 可以修改所有的属性值@Overridepublic void updateFruit(Fruit fruit) {String sql = "update  t_fruit set fname=?,price=?,fcount=?,remark=? where fid = ?";super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark(), fruit.getFid());}@Overridepublic List<Fruit> getFruitList() {return super.executeQuery("select * from t_fruit");}@Overridepublic List<Fruit> getFruitList(Integer pageNo, Integer pageSize) {return super.executeQuery("select * from t_fruit limit ?,?", (pageNo - 1) * pageSize, pageSize);}@Overridepublic List<Fruit> getFruitList(Integer pageNo, Integer pageSize, String keyword) {return super.executeQuery("select * from t_fruit where fname like ? or remark like?  limit ?,?", "%" + keyword + "%", "%" + keyword + "%", (pageNo - 1) * pageSize, pageSize);}@Overridepublic Fruit getFruitByFname(String fname) {return load("select * from t_fruit where fname = ?", fname);}@Overridepublic Fruit getFruitByFid(Integer fid) {return load("select * from t_fruit where fid=?", fid);}@Overridepublic Integer getRecordCount() {String sql = "select count(*) from t_fruit";return ((Long) executeComplexQuery(sql).get(0)[0]).intValue();}@Overridepublic Integer getRecordCount(String keyword) {String sql = "select count(*) from t_fruit where fname like ? or remark like ? ";return ((Long) executeComplexQuery(sql, "%" + keyword + "%", "%" + keyword + "%").get(0)[0]).intValue();}
}

这篇关于项目实战:新增@Controller和@Service@Repository@Autowire四个注解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/355738

相关文章

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

Python日期和时间完全指南与实战

《Python日期和时间完全指南与实战》在软件开发领域,‌日期时间处理‌是贯穿系统设计全生命周期的重要基础能力,本文将深入解析Python日期时间的‌七大核心模块‌,通过‌企业级代码案例‌揭示最佳实践... 目录一、背景与核心价值二、核心模块详解与实战2.1 datetime模块四剑客2.2 时区处理黄金法

Spring Boot项目打包和运行的操作方法

《SpringBoot项目打包和运行的操作方法》SpringBoot应用内嵌了Web服务器,所以基于SpringBoot开发的web应用也可以独立运行,无须部署到其他Web服务器中,下面以打包dem... 目录一、打包为JAR包并运行1.打包为可执行的 JAR 包2.运行 JAR 包二、打包为WAR包并运行

Spring Boot 常用注解整理(最全收藏版)

《SpringBoot常用注解整理(最全收藏版)》本文系统整理了常用的Spring/SpringBoot注解,按照功能分类进行介绍,每个注解都会涵盖其含义、提供来源、应用场景以及代码示例,帮助开发... 目录Spring & Spring Boot 常用注解整理一、Spring Boot 核心注解二、Spr

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

Spring Boot集成Logback终极指南之从基础到高级配置实战指南

《SpringBoot集成Logback终极指南之从基础到高级配置实战指南》Logback是一个可靠、通用且快速的Java日志框架,作为Log4j的继承者,由Log4j创始人设计,:本文主要介绍... 目录一、Logback简介与Spring Boot集成基础1.1 Logback是什么?1.2 Sprin

Nginx部署React项目时重定向循环问题的解决方案

《Nginx部署React项目时重定向循环问题的解决方案》Nginx在处理React项目请求时出现重定向循环,通常是由于`try_files`配置错误或`root`路径配置不当导致的,本文给大家详细介... 目录问题原因1. try_files 配置错误2. root 路径错误解决方法1. 检查 try_f

Java Jackson核心注解使用详解

《JavaJackson核心注解使用详解》:本文主要介绍JavaJackson核心注解的使用,​​Jackson核心注解​​用于控制Java对象与JSON之间的序列化、反序列化行为,简化字段映射... 目录前言一、@jsonProperty-指定JSON字段名二、@JsonIgnore-忽略字段三、@Jso

Linux高并发场景下的网络参数调优实战指南

《Linux高并发场景下的网络参数调优实战指南》在高并发网络服务场景中,Linux内核的默认网络参数往往无法满足需求,导致性能瓶颈、连接超时甚至服务崩溃,本文基于真实案例分析,从参数解读、问题诊断到优... 目录一、问题背景:当并发连接遇上性能瓶颈1.1 案例环境1.2 初始参数分析二、深度诊断:连接状态与

C#实现高性能Excel百万数据导出优化实战指南

《C#实现高性能Excel百万数据导出优化实战指南》在日常工作中,Excel数据导出是一个常见的需求,然而,当数据量较大时,性能和内存问题往往会成为限制导出效率的瓶颈,下面我们看看C#如何结合EPPl... 目录一、技术方案核心对比二、各方案选型建议三、性能对比数据四、核心代码实现1. MiniExcel