Spring Boot项目配置Mybatis Plus

Spring Boot项目配置Mybatis Plus

要在 Spring Boot 项目中配置 MyBatisPlus,可以遵循以下详细步骤:

1. 添加依赖项

首先,你需要在 pom.xml 文件中添加 MyBatisPlus 的依赖项。确保版本号是最新的或与你的项目兼容的版本。

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.x.x</version> <!-- 使用合适的版本号 -->
</dependency>

2. 配置数据源

application.propertiesapplication.yml 文件中配置数据源的相关信息。

# application.properties 示例
spring.datasource.url=jdbc:mysql://localhost:3306/yourdatabase?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

3. 配置 MyBatisPlus

在 Spring Boot 的配置类中,注册 MyBatisPlus 相关的 Bean,包括 SqlSessionFactoryPaginationInterceptor

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;

@Configuration
@MapperScan("com.example.demo.mapper")
public class MyBatisPlusConfig {

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setPlugins(new PaginationInterceptor());
        return factory.getObject();
    }

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

这里使用了 @MapperScan 注解来指定 Mapper 接口所在的包路径。

4. 创建实体类

根据数据库表结构创建对应的实体类,并可以使用 MyBatisPlus 提供的一些注解来简化映射关系。

package com.example.demo.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

@TableName("users")
public class User {

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    // 其他属性和getter/setter方法
}

5. 创建 Mapper 接口

创建 Mapper 接口,并继承 BaseMapper<T>,其中 <T> 是你的实体类。

package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Select;

public interface UserMapper extends BaseMapper<User> {

    @Select("SELECT * FROM users WHERE name = #{name}")
    User findUserByName(String name);
}

6. 创建 Service 类

在 Service 类中注入 Mapper 接口,并使用它来执行数据库操作。

package com.example.demo.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.stereotype.Service;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    private final UserMapper userMapper;

    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public User findUserById(Long id) {
        return userMapper.selectById(id);
    }
}

7. 创建 Controller 类

创建 Controller 类,用来处理来自客户端的请求,并调用 Service 层的方法。

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/user/{id}")
    public User getUser(@PathVariable Long id) {
        return userService.findUserById(id);
    }
}

8. 启动 Spring Boot 应用

最后,运行 Spring Boot 应用,测试 API 是否正常工作。

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

通过上述步骤,你就可以在 Spring Boot 中成功配置并使用 MyBatisPlus 了。如果遇到任何问题,请确保所有的依赖项版本一致,并且所有的配置信息都是正确的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值