.net(C#)与spring(java)对比参考汇总

一、对比参考表(在线编辑器

 

条目名称 .Net(C#) Spring(java) 内容描述
开发工具 Visual Studio Idea/Eclipse/MyEclipse  VS2019/2021、Idea2021
包管理器 nuget Maven
相关依赖 .net JDK  .net core和.net framework已经整合为.net5或.net6;JDK1.8、JDK11/16等
命名空间 namespace package namespace abc.com;package abc.com
引用命名空间 using import namespace abc.com;package abc.com
 大小写敏感  区分  严格区分  web地址访问时对于.net大小不区分,而spring严格区分
 类文件扩展名 .cs  .java
 视图文件  Razor  JSP/Thymeleaf/FreeMarker
 编译生成  .dll/.exe .jar/.war
 部署代理 IIS/Ngix  Tomcat/Appache  需要安装上面的依赖
 ORM框架  EF/Dapper/SqlSugar  MyBatis/Hibernate/  Spring Data JPA和EF差不多,可以根据类生成表结构
 静态文件存储  Views与wwwroot  resources/templates与static  static下存静态文件,templates存视图模板,视图中写路径时不用写wwwroot和static
 配置文件  appsettings.json/web.config  application.yml/
application.properties
webapp/WEB-INF/web.xml
 .net framework与.net ocre/.net配置也有差异。
spring boot与spring mvc配置文件有差异
 启动文件  Startup.cs  xxApplication  DemoApplication
 请求路由  [Route("/index")] @RequestMapping("/index")  修饰在controller或handler类上和其方法名上
 View方法返回类型  IActionResult/ActionResult  ModleAndView
控制器 mvc继承Controller
[Route("api/[controller]")]
[ApiController]继承ControllerBase
@Controller和@ResponeBody
@RestController
Restful [HttpGet]
[HttpPost]
[HttpPut]
[HttpDelete]
@RequestMapping("index")
@RequestMapping(value = "index",method = RequestMethod.POST)
@RequestMapping(value = "index",method = RequestMethod.PUT)
@RequestMapping(value = "index",method = RequestMethod.DELETE)
spring另一种注解
@GetMapping("index")
@PostMapping("index")
@PutMapping("index")
@DeleteMapping("index")
获取参数POST使用 @RequestBody
get使用@PathVariable @RequestParam
 View中数据交互  @Model、@ViewBag、@ViewData["user"] ${user}  MVC视图中获取对象
 控制器与视图数据传递与返回值  ViewBag.Student、ViewData["student"]
return View()、Redirect、RedirectToAction
 modelAndView.addObject("student",student)
ModelAndView中的setViewName("/user/index")、modelAndView.addObject("student","hello student");
 1)forword(默认) 一般用于用户登录的时候,根据角色转发到相应的模块2) redirect:/user/index一般用于用户注销登录时返回主页面或者跳转到其他网站
 热加载  运行旁边红色火焰图标  运行旁边绿色斧头重新编译Ctr+F9/Ctr+Shift+F9  vs热加载,老板娘vs2019.16以上及vs2021
 获取当前日期时间  DateTime.Now  new Date()  SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date();
System.out.println(formatter.format(date));
 Controller控制器  [Route("api/[controller]")]
[ApiController]
 @RestController
@RequestMapping("book")
 RESTful方法  [HttpGet][HttpPost]  @GetMapping@PostMapping  put和patch和delete同理
 Lambda表达式  C#(使用(入参)=>{方法处理体},与要传入或要实例化的委托方法签名相同即可)  JAVA(使用(入参)->{方法处理体},与要传入或要实例化的方法签名相同,且传入或实例化的类型必需是函数式接口【可以理解为自定义的委托类型】,注意与C#不同,Lambda方法体内不能引用外部非final的变量,与C# Lambda有本质不同)

 .net core 与spring API 样式

概述
API 描述 请求正文 响应正文
GET /api/todoitems 获取所有待办事项 None 待办事项的数组
GET /api/todoitems/{id} 按 ID 获取项 None 待办事项
POST /api/todoitems 添加新项 待办事项 待办事项
PUT /api/todoitems/{id} 更新现有项 待办事项 None
DELETE /api/todoitems/{id} 删除项 None None

C# .net core RESTful

 

java spring RESTful

 

 

二、数据类型对比

 名称 C# Java 说明描述
 int int/Integer  java有包装类
bool  boolean
 string String
 float float/Float
 double double/Double
 sealed final
 const static final
 Dictionary<T,K> Map
Dictionary<T,V>/TreeNode HashMap/HashTable/LinkedHashMap/TreeMap
Tuple<T1,T2...T8>
List/ArrayList/LinkedList List/ArrayList/LinkedList
 HashSet Set/HashSet/LinkedHashSet/TreeSet  set不存放重复

Collection是单列集合的根接口,主要用于存储一系列符合某种规则的元素,它有两个重要的子接口List和Set。
List接口的特点是元素有序可重复
Set接口的特点是元素无序,不可重复
ArrayList和LinkedList是List接口的实现类
HashSet和TreeSet是Set接口的实现类
Map是双列集合的根接口,特用蓝色标注,用于存储具有键(Key),值(Value)映射关系的元素,每个元素都包含一个键-值对,在使用该集合时可以通过指定的键找的对应的值。
Map为独立接口
HashMap和TreeMap是Map接口的实现类
注: 假设Collection接口里有a,b,c三个方法,list接口里有d,f方法,set接口里有e,g方法,则它们的实现类里就拥有不同的方法。

 

C#元组Tuple与值元组ValueTuple

(int x1, string s1) = (1, "a1");
var (x2, s2) = (2, "a2");

int x=x1;

string s=s2;

 

Spring Framework JdbcTemplate 参考文档

 

 

 

 

 

声明:本站内容来源于原创和互联网,尊重作者版权,转载请注明来源网址,欢迎收藏,谢谢!