package com.future.finance;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
@MapperScan("com.future.finance.mapper")
public class FinanceApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(FinanceApplication.class, args);
}
}
package com.future.finance.entity;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.util.Date;
@Getter
@Setter
public class Information implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String content;
private Date publishDate;
}
package com.future.finance.mapper;
import com.future.finance.entity.Information;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface InformationMapper {
@Select("select * from information")
@Results({
@Result(property = "content", column = "content"),
@Result(property = "publishDate", column = "publish_date")
})
List getAll();
}
package com.future.finance.controller;
import com.future.finance.entity.Information;
import com.future.finance.mapper.InformationMapper;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class InformationController {
@Autowired
private InformationMapper informationMapper;
private Gson gson = new Gson();
@RequestMapping("/getAll")
public String getAll() {
List result = informationMapper.getAll();
return gson.toJson(result);
}
}
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/finance?characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=GMT%2B8
spring.datasource.username=jack
spring.datasource.password=jack123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false
build.gradle
plugins {
id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group = 'com.future'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
// 1. Use Guava in your implementation only:
implementation 'com.google.guava:guava:29.0-jre'
implementation 'com.google.code.gson:gson:2.8.5'
compile("org.springframework.boot:spring-boot-devtools") // 热部署
compileOnly 'org.projectlombok:lombok:1.18.12'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.3'
runtimeOnly 'mysql:mysql-connector-java:8.0.17'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
// debug
//bootRun {
// jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
//}
然后postman访问
localhost:8080/getAll