springboot访问数据库以json形式输出

springboot访问数据库以json形式输出

springboot访问数据库以json形式输出_第1张图片
pojo中Test

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Test {
    private Integer id;
    private String name;
    private Boolean gender;
    private Integer age;
    private Date birth;
}

TestMapper.xml




    
    


Test.mapper

public interface TestMapper {

    Test queryTestByID(@Param("testID") Integer testID);
    List queryTest();

}

application.yml配置

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/springboot02?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver

server:
  port: 8080
  servlet:
    context-path: /

mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml

xxxxApplication添加

@MapperScan(basePackages = "com.gsj.mapper")

TestService

public interface TestService {
    Test queryTestByID(@Param("testID") Integer testID);
    List queryTest();
}

TestServiceImpl

@Service
public class TestServiceImpl implements TestService {
    private TestMapper testMapper;


    @Autowired(required=false)
    public void setTestMapper(TestMapper testMapper) {
        this.testMapper = testMapper;
    }
    
    @Override
    public Test queryTestByID(Integer testID) {
        return testMapper.queryTestByID(testID);
    }

    @Override
    public List queryTest() {
        return testMapper.queryTest();
    }
}

TestController

@RestController
public class TestController {

    @Autowired
    private TestService testService;


//    @ResponseBody
    @RequestMapping("/queryTest")
    private List queryTest(){
        List testList = testService.queryTest();

        return testList;

    }

依赖有


            org.projectlombok
            lombok
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.1
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            com.alibaba
            druid
            1.1.10
        

springboot访问数据库以json形式输出_第2张图片

你可能感兴趣的:(springboot访问数据库以json形式输出)