SpringBoot整合注解式mybatis

1. 创建Spring Boot项目: 创建一个Spring Boot项目,可以使用Spring Initializer或手动创建

2. 添加依赖:pom.xml文件中,添加Spring Boot、MyBatis和数据库驱动程序的依赖,就像之前所示。

<dependencies>
        <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.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>2.3.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

3. 配置数据源:application.propertiesapplication.yml文件中配置数据库连接信息。例如:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useSSL=true
    username: root
    password: 123456

4. 创建实体类: 创建实体类和数据库表之间的映射,可以使用@Entity注解,也可以使用@Table@Id等MyBatis或JPA的注解。

package com.example.demo.entity;

import lombok.Data;

@Data
public class Sample {
    private Long id;
    private String name;
    private String sex;
    private int age;

}

5. 创建Mapper接口: 创建一个MyBatis的Mapper接口,并使用@Mapper注解进行标记。

package com.example.demo.mapper;

import com.example.demo.entity.Sample;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface SampleMapper {
    List<Sample> findAll();
}

6.创建Service层: 创建Service层,处理业务逻辑,并将Mapper接口注入Service中。

public interface SampleService {
    public List<Sample> findAll();

}
import java.util.List;
@Service
public class SampleServiceImpl implements SampleService{
    @Autowired
    SampleMapper sampleMapper;
    @Override
    public List<Sample> findAll() {
        return sampleMapper.findAll();
    }
}

 7. 创建Controller: 创建一个Controller层,处理HTTP请求,调用Service层的方法。

这里注意要使用RestController注解不能用Controller不然前端调用会报404

@RequestMapping("sample")
@RestController
public class SampleController {
    @Resource
    SampleService sampleService;
    @GetMapping("find-all")
    public List<Sample> list(){
        List<Sample> all = sampleService.findAll();
        return all;
    }
}

8. 配置Mapper扫描路径: 使用@MapperScan注解来指定MyBatis的Mapper接口扫描路径,通常在Spring Boot应用的启动类上使用。

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值