青岛专业做网站,网站建设维护预算,美空间网站,查降权网站原文地址#xff1a;PHP与Spring Boot在实现功能上的比较 - Pleasure的博客
下面是正文内容#xff1a; 前言 最近在学Spring Boot后端框架#xff0c;在功能实现上发现了与PHP存在着一定的可比性。 这篇就单独拎出来进行比较一下。 正文 就例举一些功能上的重叠以及大致的…原文地址PHP与Spring Boot在实现功能上的比较 - Pleasure的博客
下面是正文内容 前言 最近在学Spring Boot后端框架在功能实现上发现了与PHP存在着一定的可比性。 这篇就单独拎出来进行比较一下。 正文 就例举一些功能上的重叠以及大致的代码实现思路 变量类型的转换 显示类型的转换使用强制转换的语法 php中 $stringVar 10; // 字符串变量
$intVar (int) $stringVar;
echo $intVar; SpringBoot中 String stringVar 10;
int intVar Integer.parseInt(stringVar);
System.out.println(intVar);键值对数据结构 php中的关联数组和SpringBoot中的Map类型存在一定的相似性 php中 $map array(key1 value1,key2 123,key3 true
);
echo $map[key1]; // 输出value1
echo $map[key2]; // 输出123
echo $map[key3]; // 输出trueSpringBoot中 MapString, Object map new HashMap();
map.put(key1, value1);
map.put(key2, 123);
map.put(key3, true);
System.out.println(map.get(key1)); // 输出value1
System.out.println(map.get(key2)); // 输出123
System.out.println(map.get(key3)); // 输出true不同路径的访问与响应 返回HTTP响应 在功能上php和SpringBoot都支持返回HTTP响应下面举几个具体的例子。 php中 设置状态码 ?php
http_response_code(200);设置重定向 ?php
header(Location: /hello, true, 302);设置响应头 ?php
header(Content-Type: text/plain);设置响应内容 ?php
echo Hello, world!;SpringBoot中 设置状态码 import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;RestController
public class MyController {GetMapping(/hello)public ResponseEntityString hello() {return new ResponseEntity(Hello, world!, HttpStatus.OK);}
}设置重定向 import org.springframework.web.bind.annotation.*;RestController
public class MyController {GetMapping(/redirect)public void redirect(HttpServletResponse response) throws IOException {response.sendRedirect(/hello);}
}设置响应头 import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;RestController
public class MyController {GetMapping(/hello)public ResponseEntityString hello() {HttpHeaders headers new HttpHeaders();headers.setContentType(MediaType.TEXT_PLAIN);return new ResponseEntity(Hello, world!, headers, HttpStatus.OK);}
}设置响应内容略 函数的使用 php中 函数声明 function add($a, $b) {return $a $b;
} 参数传递 function greet($name) {echo Hello, . $name . !;
}
greet(John); 返回值 function multiply($a, $b) {return $a * $b;
}
$result multiply(2, 3); 函数调用 $sum add(3, 5); 函数作用域 function myFunction() {// 在函数中定义的函数只能在该函数中使用
} SpringBoot中 函数声明 public int add(int a, int b) {return a b;
} 参数传递 public void greet(String name) {System.out.println(Hello, name !);
}
greet(John); 返回值 public int multiply(int a, int b) {return a * b;
}
int result multiply(2, 3); 函数调用 int sum add(3, 5); 函数作用域 public class MyService {public void myMethod() {// 在类中定义的函数可以在整个类中使用}
} 数据库操作 php和SpringBoot中都存在连接数据库的操作 php中 数据库连接配置 $servername localhost;
$username root;
$password 123456; 执行SQL查询 $stmt $conn-prepare(SELECT * FROM my_table);
$stmt-execute();
$rows $stmt-fetchAll(); 事务管理略 SpringBoot中 数据库连接配置 spring.datasource.urljdbc:mysql://localhost:3306/mydb
spring.datasource.usernameroot
spring.datasource.password123456 执行SQL查询 ListMapString, Object rows jdbcTemplate.queryForList(SELECT * FROM my_table); 事务管理 Transactional
public void performTransaction() {// 执行数据库操作
} 储存用户登录状态 php中 在php中通常通过会话管理或者cookie设置来储存用户的登录状态。在不同请求间共享用户信息。 SpringBoot中 在SpringBoot中通常通过拦截器加上ThreadLocal来实现登录的拦截以及线程范围内用户信息的数据共享。 尾声 作为实现后端功能两种语言在功能和使用上肯定还有其他的相似之处上面是我目前整理的内容。另外的有待大家自己发掘。