(第二天)随便做一个SpringBoot+React项目

目標
1.SpringBoot 整合Mybatis
2.API從數據庫獲取Profile數據

參考:
https://www.bswen.com/2018/04/springboot-springboot-and-mybatis-and-mysql-with-mapper-xml.html
https://blog.csdn.net/u012702547/article/details/88643598

整合Mybatis

添加依賴:pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.28</version>
    <scope>runtime</scope>
</dependency>

配置數據庫連接

(第二天)随便做一个SpringBoot+React项目_第1张图片
代碼:

spring.datasource.url=jdbc:mysql://172.31.28.153/demo?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

ProfileMapper

(第二天)随便做一个SpringBoot+React项目_第2张图片
代碼:

package com.hb.demo;

import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface ProfileMapper {
    List<Profile> getAllProfile();
}

ProfileMapper.xml

(第二天)随便做一个SpringBoot+React项目_第3张图片
代碼:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hb.demo.ProfileMapper">
    <select id="getAllProfile" resultType="com.hb.demo.Profile">
        select * from profiles;
    </select>
</mapper>

配置mapper xml文件的掃描路徑

(第二天)随便做一个SpringBoot+React项目_第4张图片
代碼:

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

測試

(第二天)随便做一个SpringBoot+React项目_第5张图片

問題

  1. ProfileMapper文件為社麽會被自動掃描?

下一篇: 用戶登錄

你可能感兴趣的:(Java,React)