从idea新建项目
选择spring启动 * help、mvnw 文件可以删除
* help、mvnw 文件可以删除
- springBoot3.0需要的最小JDK是JDK17,当低于17的时候会报错。
 改成2.7.6
新建控制层Controller、Mapper层和Model文件夹
- 必须在springBoot启动项下面新建,不然无法识别。
允许XML进入target
在pom.xml里面写入
<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
</build>
引入必要的依赖包
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.xmlunit</groupId>
            <artifactId>xmlunit-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>
配置数据库账号密码
application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=testUser
spring.datasource.password=testnlp#123
在model导入一个新的类
和数据库对应,构建一个类
 
实现GetSet方法、各种有参构造、无参构造。
 通过@value实现默认值设定
@Data
@Component
@AllArgsConstructor
@NoArgsConstructor
public class Cat {
    @NotNull(message="必须为空")
    Integer age;
    @Value("dog01")
    String name;
    @Value("1")
    Integer id;
}
写入mapper文件
在mapper文件夹下写入一个接口
@Mapper
public interface CatMapper {
    @Insert("insert into cat(id, name, age) values(#{id}, #{name}, #{age})")
    void addOneCat(Cat cat);
}
- 加入@mapper注解或者在主类加入@MapperScan(“com/example/test02/test02/mapper”)
- 可以在接口上写注释实现简单的SQL查询
写入一个控制层
- 类上加入控制注解
- 将mapper注入这个类
- 实现控制类方法
@RestController
public class testController {
    @Resource
    CatMapper catMapper;
    @RequestMapping(value = "/cat", method = RequestMethod.POST)
    @ResponseBody
    Cat addCat(Cat cat) {
        catMapper.addOneCat(cat);
        return cat;
    }
}
测试接口
启动SpringBoot服务
 
 使用postman发送对应的请求,检查返回值
 
检查数据库是否有新增的数据:
 
 成功写入数据。



















