Admin 发布的文章

前几天根据官方的教程,编译了一个maven本地仓库版本
https://gateway.pinata.cloud/ipfs/QmQxEMFLsJXZqrgyVgkjA4f5d5xAB43fx1oVbVSwvBziAX?filename=maven-tradient-0.1.3.zip

解压后的文件放本地仓库的相应位置即可

然后在项目的pom里引入:

<dependency>
    <groupId>org.tron.trident</groupId>
    <artifactId>abi</artifactId>
    <version>0.1.3</version>
</dependency>
<dependency>
    <groupId>org.tron.trident</groupId>
    <artifactId>utils</artifactId>
    <version>0.1.3</version>
</dependency>
<dependency>
    <groupId>org.tron.trident</groupId>
    <artifactId>core</artifactId>
    <version>0.1.3</version>
</dependency>

主意,不要放pom,否则无法使用

如果没引入com.google.protobuf有时会报错,建议也引入一下:

<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java</artifactId>
    <version>3.12.0</version>
</dependency>

关于如何从官方的github源码编译jar,改天有空再更新

新建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

运行项目即可进行测试