java 对象映射文件_【spring boot】映射properties文件属性--到Java对象

描述

将*.properties中的内容映射到java对象中;

主要步骤

添加 @Component 注解;

使用 @PropertySource 注解指定配置文件位置;

使用 @ConfigurationProperties 注解,设置相关属性;

my.properties

author.name=ssslinppp

author.age=128

author.student.name=studentName

author.student.age=88

AuthorSettings.java

package com.sssppp;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.PropertySource;

import org.springframework.stereotype.Component;

@Component

@ConfigurationProperties(prefix = "author")

@PropertySource("classpath:my.properties")

public class AuthorSettings {

private String name;

private Long age;

private Student student;

public Student getStudent() {

return student;

}

public void setStudent(Student student) {

this.student = student;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Long getAge() {

return age;

}

public void setAge(Long age) {

this.age = age;

}

}

Student.java

package com.sssppp;

public class Student {

private String name;

private int age;

public Student() {

super();

}

public Student(String name, int age) {

super();

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public String toString() {

return "Student [name=" + name + ", age=" + age + "]";

}

}

测试类

package com.sssppp;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class Ch623 {

@Autowired

private AuthorSettings authorSettings;

@RequestMapping("/ch623")

public AuthorSettings index() {

return authorSettings;

}

}

测试结果

{"name":"ssslinppp","age":128,"student":{"name":"studentName","age":88}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PageHelper 是一个 MyBatis 分页插件,可以帮助开发者在 MyBatis 中方便地实现分页功能。而 pagehelper-spring-boot-starter 是一个 PageHelper 在 Spring Boot 中的集成,使用起来非常方便。以下是使用 pagehelper-spring-boot-starter 实现分页的步骤: 1. 引入依赖 在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> </dependency> ``` 2. 配置 PageHelper 在 application.properties 文件中添加以下配置: ``` # 分页插件配置 pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.supportMethodsArguments=true pagehelper.params=count=countSql ``` 其中,helperDialect 为数据库方言,reasonable 为是否开启合理化分页,supportMethodsArguments 为是否支持 Mapper 方法参数为 Page 类型,params 为分页参数映射。 3. 使用分页 在 Mapper 接口的方法中添加 Page 参数,即可实现分页查询: ``` List<User> getUsers(@Param("name") String name, Page<User> page); ``` 在 Service 层中调用该方法,并传入 Page 参数即可实现分页查询: ``` Page<User> page = PageHelper.startPage(pageNum, pageSize); List<User> users = userMapper.getUsers(name, page); ``` 其中,pageNum 和 pageSize 分别为当前页码和每页显示数量。PageHelper.startPage 方法会自动拦截下一次查询,并将查询转为分页查询。查询结果会封装在 Page 对象中,可以通过该对象获取分页信息和查询结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值