分类 默认分类 下的文章

function timeConvert(time) {
    const date = new Date(time)
    const year = date.getFullYear()
    const month = (date.getMonth()+1).toString().padStart(2, '0')
    const day = date.getDate().toString().padStart(2, '0')
    const hour = date.getHours().toString().padStart(2, '0')
    const minute = date.getMinutes().toString().padStart(2, '0')
    const second = date.getSeconds().toString().padStart(2, '0')
    return year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second
}

下载jdk并传到服务器

https://repo.huaweicloud.com/java/jdk/8u202-b08/

https://www.oracle.com/java/technologies/javase/javase8-archive-downloads.html

执行命令安装jdk

rpm -ivh jdk-8u202-linux-x64.rpm

配置环境变量

vi /etc/profile

放入以下代码

#set java environment
JAVA_HOME=/usr/java/jdk1.8.0_202-amd64
JRE_HOME=/usr/java/jdk1.8.0_202-amd64/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH

测试是否成功

java -version

新建SpringBoot项目



配置pom.xml

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3.1</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.20</version>
</dependency>

在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹

@SpringBootApplication
@MapperScan("mybatis.plus.test.mapper")
public class MybatisplustestApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisplustestApplication.class, args);
    }
}

编写实体类bean.User

表结构根据 baomidou.com/guide/quick-start.html 设计

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

配置application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver # 如果是MySQL5.x则用com.mysql.jdbc.Driver,否则报错
    url: jdbc:mysql://ip:port/database_name?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false
    username: username
    password: password

编写Mapper类 mapper.UserMapper

public interface UserMapper extends BaseMapper<User> {
}

添加测试类,进行功能测试

@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
    List<User> userList = userMapper.selectList(null);
    System.out.print(userList);
}

增删改查的使用,这篇文章写得不错:
《mybatis-plus教程-Mybatis-Plus增删改查》
blog.csdn.net/weixin_44062339/article/details/103078554

用Spring Intitlizr新建一个工程,Project SDK选择你的JDK8根目录,Dependicies勾选Spring Web
新建一个类,例如叫controller.RequestTest.java,编辑内容如下:

@RestController // 没加这个会直接404
public class RequestTest {
    @GetMapping("/car/{id}/owner/{username}")
    public Map<String,Object> getCar(
         @PathVariable("id") Integer id,
         @PathVariable("username") String name,
         @PathVariable Map<String,String> pv, // 获取所有PathVariable
         @RequestHeader("User-Agent") String userAgent,
         @RequestHeader Map<String,String> header, // 获取所有RequestHeader
         @RequestParam(value = "age", defaultValue = 0) Integer age, // 可以配置缺省时的默认值
         @RequestParam("inters") List<String> inters,
         @RequestParam Map<String,String> params // 获取所有RequestParam
    ){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("name",name);
        map.put("userAgent",userAgent);
        map.put("age",age);
        map.put("inters",inters);
        return map; // 返回的Content-Type为application/json
    }

    @PostMapping("/save")
    public Map postMethod(@RequestBody String content){
        Map<String,Object> map = new HashMap<>();
        map.put("content",content);
        return map;
    }
}

resources里新建文件夹public,然后新建index.html,编辑内容如下

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>请求测试</title>
</head>
<body>
    <p><textarea id="textarea" style="height: 100px; width: 100%;" placeholder="请求的返回结果将显示在这里"></textarea></p>
    <p><a href="javascript: getRequest()">GET请求</a></p>
    <p><a href="javascript: postRequest()">POST请求</a></p>
    <script>
        function getRequest() {
            fetch('/car/123/owner/zhangsan?age=23&inters=football&inters=basketball').then(res=>res.json()).then(json=>{
                document.getElementById('textarea').value = JSON.stringify(json)
            })
        }
        function postRequest() {
            fetch('/save',{
                method: 'POST',
                body: 'body, string'
            }).then(res=>res.json()).then(json=>{
                document.getElementById('textarea').value = JSON.stringify(json)
            })
        }
    </script>
</body>
</html>

DELETE、PUT和POST差不多,只需更改请求的method即可,详情可看bilibili.com/video/BV19K4y1L7MT

运行项目即可进行测试