当前位置: 首页 > news >正文

【Spring框架】Spring读取与存储综合练习

练习

在 Spring 项⽬中,通过 main ⽅法获取到 Controller 类,调⽤ Controller ⾥⾯通过注⼊的⽅式调⽤ Service 类,Service 再通过注⼊的⽅式获取到 Repository 类,Repository 类⾥⾯有⼀个⽅法构建⼀个 User 对象,返回给 main ⽅法。Repository ⽆需连接数据库,使⽤伪代码即可。
在这里插入图片描述
User类

package com.java.demo.model;public class User {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +'}';}
}

UserController类

package com.java.demo.controller;import com.java.demo.model.User;
import com.java.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;@Controller
public class UserController {@Autowiredprivate UserService userService;public User getUser() {return userService.getUser();}
}

UserRepository类

package com.java.demo.repository;import com.java.demo.model.User;
import org.springframework.stereotype.Repository;@Repository
public class UserRepository {public User getUser() {// 伪代码User user = new User();user.setId(1);user.setName("张三");return user;}
}

UserService类

package com.java.demo.service;import com.java.demo.model.User;
import com.java.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UserService {@Autowiredprivate UserRepository userRepository;public User getUser() {return userRepository.getUser();}
}

App类

package com.java.demo;import com.java.demo.controller.UserController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext("spring-config.xml");UserController userController = context.getBean("userController", UserController.class);System.out.println(userController.getUser());}
}

在这里插入图片描述

http://www.lryc.cn/news/97142.html

相关文章:

  • Python实现指定区域桌面变化监控并报警
  • 【数据结构】实验五:栈
  • ⚡️⚡️Java多线程编程的高效、安全实践
  • 【云原生】Docker私有仓库registry
  • 第十四届蓝桥杯大赛青少年省赛C++组试题真题 2023年5月
  • GAN论文精读
  • 数据结构:计数排序(详解)
  • 1 请使用js、css、html技术实现以下页面,表格内容根据查询条件动态变化。
  • react-native项目安卓版本升级 compileSdkVersion 29->31
  • 【学习笔记】目标跟踪领域SOTA方法比较
  • 机器学习 深度学习编程笔记
  • 18.背景轮播
  • 论文代码学习—HiFi-GAN(2)——鉴别器discriminator代码
  • Linux Shell 脚本编程学习之【第3章 正则表达式 (第二部分) grep命令】
  • 大语言模型LLM
  • 自学网络安全(黑客)的误区
  • @Conditional
  • 【Linux】网络基础之TCP协议
  • Java设计模式之装饰器(Decorator)模式
  • element ui树组件render-content 树节点的内容区的渲染另一种方式
  • html a标签换行显示
  • 关于Redis-存Long取Integer类型转换错误的问题
  • 设计模式一:简单工厂模式(Simple Factory Pattern)
  • 如何利用plotly和geopandas根据美国邮政编码(Zip-Code)绘制美国地图
  • ceph集群搭建
  • 前端密码加密 —— bcrypt、MD5、SHA-256、盐
  • 汽车UDS诊断深度学习专栏
  • macOS 下安装brew、nvm
  • 【云原生】Kubernetes工作负载-StatefulSet
  • Java:方法的重载