java14 record

时间:2024-04-14 19:44:58

文章目录

  • 语法
  • JSON序列化/反序列化
  • mybatis支持情况

  Java14新推出的record关键字确实让人耳目一新,减少了大量数据类的代码。在写实体类的时候,set方法和get方法,虽然可以用lombok自动生成,但毕竟不是官方的实现。现在有了官方实现就不一样了。

语法

  语法上,record是一种类型,跟枚举一样,是被严格限制的类型,下面是一个简单的record:

public record House(String building, String unit, String number) {
}

  创建和读取属性的语法如下:

public class RecordDemo {

    public static void main(String[] args) {
        House house = new House("3栋", "一单元", "1508");
        System.out.println(house.building());
        System.out.println(house.unit());
        System.out.println(house.number());
    }
}

  Record的所有属性都是只读的,而且类型体里面只能定义静态字段。我比较关心的是能不能与现有的框架集成。

JSON序列化/反序列化

  spring默认的json序列化实现是Jackson,那么看看Jackson能不能record。先写代码试试吧:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.0</version> <!-- 请替换为最新的版本号 -->
</dependency>
public static void main(String[] args) {
    House house = new House("3栋", "一单元", "1508");
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        String jsonString = objectMapper.writeValueAsString(house);
        System.out.println(jsonString);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
}

  结果表明是支持的:

{"building":"3栋","unit":"一单元","number":"1508"}

  那么反序列化呢?

public class RecordDeserializeDemo {
    public static void main(String[] args) throws IOException {
        String s = """
                {"building":"3栋","unit":"一单元","number":"1508"}
                """;
        ObjectMapper objectMapper = new ObjectMapper();
        House house = objectMapper.readerFor(House.class).readValue(s, House.class);
        System.out.println(house);
    }
}

  反序列化也是支持的:

House[building=3栋, unit=一单元, number=1508]

  国产框架fastjson,从2.0.3才开始支持Java 14record的。

mybatis支持情况

  那就写一个demo测试一下呗。首先加上pom.xml配置:

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>


        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.36</version>
        </dependency>

  然后加上yml配置:

# 数据源
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8

  最后写一个简单的mapper类:

public interface HouseMapper {

    @Select("select '7栋' as building, '一单元' as unit, '901' as number")
    House select();
}

  然后写个main函数就可以测试了:

@SpringBootApplication
@MapperScan
public class MyBatisDemo {

    @Autowired
    private HouseMapper houseMapper;
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MyBatisDemo.class);
        MyBatisDemo bean = context.getBean(MyBatisDemo.class);
        House house = bean.houseMapper.select();
        System.out.println(house);
    }
}

  测试结果表明mybatis是支持record的。其他的框架,比如spring webmvc我没去尝试,但是我觉得肯定是会支持的,比较java14都出来好几年了。