springboot入门教程和mysql数据库

本文介绍了SpringBoot结合MyBatis-Plus的使用,包括@MapperScan、@Controller、@PathVariable、@RequestParam注解的解释,以及如何配置MySQL数据库。提供了实体类、Mapper接口和测试用例的示例。还提到了MyBatis的常用注解,如sql、insert、update、delete。此外,讲解了MySQL数据库的基本操作,如字段类型、创建数据库和表、增删查改语句、IN、BETWEEN、LIKE操作以及JOIN查询。文章适合SpringBoot和MySQL初学者。
摘要由CSDN通过智能技术生成

8:@MapperScan

主要作用于启动类上,用于生成DAO接口的实现类,如果DAO接口比较多,推荐使用@MapperScan注解,写法如@MapperScan(“com.example.demo.dao”).

9:@Controller

主要作用于控制层类上,用于处理http请求等。

10:@PathVariable

主要是用于取url中的变量的值,比如 @RequestMapping(“/student/{studentName}”),那么在对应的方法入参中可以写成:(@PathVariable  String  studentName).

11:@RequestParam

将请求参数绑定到Controller的方法上面,@RequestParam(value=”参数名”)。

(5) 如何使用mybaits-plus(用法可以到官网查询 https://baomidou.com/ )

引入 Spring Boot Starter 父工程:

org.springframework.boot

spring-boot-starter-parent

undefined

引入 spring-boot-starter、spring-boot-starter-test、mybatis-plus-boot-starter、h2 依赖:

在 application.yml 配置文件中添加 H2 数据库的相关配置:

# DataSource Config

spring:

datasource:

driver-class-name: org.h2.Driver

schema: classpath:db/schema-h2.sql

data: classpath:db/data-h2.sql

url: jdbc:h2:mem:test

username: root

password: test

在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:

@SpringBootApplication

@MapperScan(“com.baomidou.mybatisplus.samples.quickstart.mapper”)

public class Application {

public static void main(String[] args) {

SpringApplication.run(QuickStartApplication.class, args);

}

}

编写实体类 User.java(此处使用了 Lombok (opens new window)简化代码)

@Data

public class User {

private Long id;

private String name;

private Integer age;

private String email;

}

编写Mapper类 UserMapper.java

public interface UserMapper extends BaseMapper {

}

添加测试类,进行功能测试:

@SpringBootTest

public class SampleTest {

@Autowired

private

  • 9
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值