Mapper-Spring-Boot-Starter 使用教程
2024-08-30 05:06:34作者:郦嵘贵Just
1、项目介绍
Mapper-Spring-Boot-Starter 是一个帮助开发者将通用 Mapper 集成到 Spring Boot 中的开源项目。该项目简化了 MyBatis 在 Spring Boot 环境下的配置和使用,提供了自动化的配置和扫描功能,使得开发者可以更专注于业务逻辑的实现。
2、项目快速启动
2.1 添加依赖
首先,在 pom.xml 文件中添加 mapper-spring-boot-starter 依赖:
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mapper-starter-version}</version>
</dependency>
2.2 配置数据源
在 application.yml 文件中配置数据源:
spring:
datasource:
url: jdbc:mysql://localhost:3306/your_database
username: your_username
password: your_password
driver-class-name: com.mysql.cj.jdbc.Driver
2.3 创建实体类和Mapper接口
创建一个实体类:
package com.example.demo.entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "user")
public class User {
@Id
private Integer id;
private String name;
// Getters and Setters
}
创建一个 Mapper 接口:
package com.example.demo.mapper;
import tk.mybatis.mapper.common.Mapper;
import com.example.demo.entity.User;
public interface UserMapper extends Mapper<User> {
}
2.4 配置Mapper扫描
在 Spring Boot 启动类上添加 @MapperScan 注解:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2.5 编写测试类
编写一个简单的测试类来验证功能:
package com.example.demo.test;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelectById() {
User user = userMapper.selectByPrimaryKey(1);
System.out.println(user);
}
}
3、应用案例和最佳实践
3.1 多数据源配置
在某些场景下,可能需要配置多个数据源。可以通过配置多个 DataSource 和 SqlSessionFactory 来实现:
spring:
datasource:
primary:
url: jdbc:mysql://localhost:3306/primary_db
username: user
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
secondary:
url: jdbc:mysql://localhost:3306/secondary_db
username: user
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
然后在配置类中进行相应的配置:
@Configuration
public class DataSourceConfig {
@Bean(name = "primaryDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@ConfigurationProperties
登录后查看全文
热门项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5-w4a8GLM-5-w4a8基于混合专家架构,专为复杂系统工程与长周期智能体任务设计。支持单/多节点部署,适配Atlas 800T A3,采用w4a8量化技术,结合vLLM推理优化,高效平衡性能与精度,助力智能应用开发Jinja00
jiuwenclawJiuwenClaw 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。Python0238- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
AtomGit城市坐标计划AtomGit 城市坐标计划开启!让开源有坐标,让城市有星火。致力于与城市合伙人共同构建并长期运营一个健康、活跃的本地开发者生态。01
electerm开源终端/ssh/telnet/serialport/RDP/VNC/Spice/sftp/ftp客户端(linux, mac, win)JavaScript00
项目优选
收起
deepin linux kernel
C
27
13
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
632
4.16 K
Ascend Extension for PyTorch
Python
470
566
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
932
834
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.51 K
861
昇腾LLM分布式训练框架
Python
138
162
华为昇腾面向大规模分布式训练的多模态大模型套件,支撑多模态生成、多模态理解。
Python
131
192
暂无简介
Dart
879
210
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
383
266
AscendNPU-IR是基于MLIR(Multi-Level Intermediate Representation)构建的,面向昇腾亲和算子编译时使用的中间表示,提供昇腾完备表达能力,通过编译优化提升昇腾AI处理器计算效率,支持通过生态框架使能昇腾AI处理器与深度调优
C++
123
188