Springboot整合mybatis,配置驼峰命名转换(例:userId --user_id)

如题:两种配置方式

1.

springboot的配置文件 添加:

mybatis.configuration.mapUnderscoreToCamelCase=true 或

mybatis.configuration.map-underscore-to-camel-case=true(根据版本不同)

2.通过@configuration配置的方式

@Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean() throws IOException {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setMapperLocations(resolver.getResources("classpath:/mapper/**/**.xml"));
        try {
            //开启驼峰命名转换
            bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

 

<think>我们正在处理一个关于在Spring Boot 3项目中整合MyBatis-Plus并实现多表联合查询的问题。根据用户需求,我们需要完成以下步骤: 1. 整合MyBatis-Plus到Spring Boot 3项目。 2. 实现多表联合查询的功能。 参考已有的引用信息,我们可以得到一些整合MyBatis-Plus的基本步骤和注意事项。但是,由于用户要求实现多表联合查询,我们需要特别注意MyBatis-Plus在处理多表查询时的方案,因为MyBatis-Plus主要专注于单表操作,多表查询通常需要自定义SQL。 ### 步骤1:创建Spring Boot 3项目并添加依赖 首先,我们需要创建一个Spring Boot 3项目(使用Java 17或更高版本),并在`pom.xml`中添加MyBatis-Plus的Spring Boot Starter依赖。 根据引用[3]的提示,我们只需要添加`mybatis-plus-boot-starter`依赖,不要同时添加`mybatis-spring-boot-starter`(因为前者已经包含了必要的依赖)。 ```xml <dependencies> <!-- Spring Boot Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis-Plus Starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> <!-- 请使用与Spring Boot 3兼容的版本,如3.5.0以上 --> </dependency> <!-- 数据库驱动,如MySQL --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <!-- Lombok(可选) --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> ``` 注意:请确保使用与Spring Boot 3兼容的MyBatis-Plus版本(目前推荐3.5.0以上)。 ### 步骤2:配置数据源 在`application.yml`或`application.properties`中配置数据库连接信息。如: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC username: your_username password: your_password driver-class-name: com.mysql.cj.jdbc.Driver # MyBatis-Plus配置 mybatis-plus: configuration: map-underscore-to-camel-case: true # 开启驼峰命名自动映射 # 可以配置mapper.xml的位置,如果使用注解方式则不需要 # mapper-locations: classpath:mapper/*.xml # 配置日志 global-config: db-config: id-type: auto # 主键自增策略 ``` ### 步骤3:创建实体类和Mapper接口 假设我们有两个表:`user`和`order`,一个用户可以有多个订单。我们需要创建两个实体类:`User`和`Order`。 **User.java** ```java import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import java.util.List; @Data @TableName("user") public class User { @TableId private Long id; private String name; private Integer age; private String email; // 非数据库字段,用于关联查询结果 @TableField(exist = false) private List<Order> orders; } ``` **Order.java** ```java import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("order") public class Order { @TableId private Long id; private String orderNo; private Long userId; private Double amount; } ``` 然后,为每个实体创建Mapper接口,并继承MyBatis-Plus的`BaseMapper`。 **UserMapper.java** ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; import com.example.demo.entity.User; @Mapper public interface UserMapper extends BaseMapper<User> { // 自定义方法:查询用户及其订单(多表查询) User selectUserWithOrders(Long userId); } ``` **OrderMapper.java** ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; import com.example.demo.entity.Order; @Mapper public interface OrderMapper extends BaseMapper<Order> { } ``` ### 步骤4:实现多表联合查询 由于MyBatis-Plus本身不直接支持多表联合查询,我们需要自定义SQL语句来实现。这里有两种方式: 1. 使用XML文件编写SQL。 2. 使用注解方式编写SQL。 这里我们使用XML方式(因为多表查询SQL可能较长,使用XML更清晰)。 在`resources/mapper`目录下创建`UserMapper.xml`文件: ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.mapper.UserMapper"> <resultMap id="UserWithOrdersResultMap" type="com.example.demo.entity.User"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="age" property="age"/> <result column="email" property="email"/> <!-- 一对多关联:订单 --> <collection property="orders" ofType="com.example.demo.entity.Order"> <id column="order_id" property="id"/> <result column="order_no" property="orderNo"/> <result column="user_id" property="userId"/> <result column="amount" property="amount"/> </collection> </resultMap> <select id="selectUserWithOrders" resultMap="UserWithOrdersResultMap"> SELECT u.id, u.name, u.age, u.email, o.id as order_id, o.order_no, o.user_id, o.amount FROM user u LEFT JOIN order o ON u.id = o.user_id WHERE u.id = #{userId} </select> </mapper> ``` 注意:在`application.yml`中配置了`mybatis-plus.mapper-locations=classpath:mapper/*.xml`,这样MyBatis才能找到这个XML文件。 ### 步骤5:编写Service和Controller 创建UserService和UserController来调用这个多表查询方法。 **UserService.java** ```java import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserWithOrders(Long userId) { return userMapper.selectUserWithOrders(userId); } } ``` **UserController.java** ```java import com.example.demo.entity.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; 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 { @Autowired private UserService userService; @GetMapping("/user/{userId}") public User getUserWithOrders(@PathVariable Long userId) { return userService.getUserWithOrders(userId); } } ``` ### 步骤6:启动类添加Mapper扫描 在Spring Boot启动类上添加`@MapperScan`注解,指定Mapper接口所在的包。 ```java import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.demo.mapper") // 替换成你的Mapper包路径 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` ### 注意事项 1. **表名和字段名映射**:确保数据库表名和字段名与实体类中的`@TableName`和`@TableField`(如果需要)匹配,或者开启驼峰命名转换配置`map-underscore-to-camel-case: true`)。 2. **SQL注入**:使用MyBatis-Plus的条件构造器时,注意防止SQL注入。在自定义SQL中,使用`#{}`而不是`${}`来防止注入。 3. **性能问题**:多表查询尤其是连接查询可能影响性能,特别是数据量大时。注意合理设计查询和索引。 ### 总结 以上就是在Spring Boot 3中整合MyBatis-Plus并实现多表联合查询的完整步骤。我们通过自定义SQL的方式实现了多表查询,并利用MyBatis的`resultMap`进行结果映射。MyBatis-Plus提供了强大的单表操作能力,而多表查询则通过MyBatis的原生特性实现,两者结合可以满足大部分业务需求。 §§相关问题§§ 1. 如何在MyBatis-Plus中使用条件构造器进行复杂查询? 2. MyBatis-Plus如何实现分页查询? 3. 在Spring Boot中整合MyBatis-Plus时,如何处理事务?</think>### 在 Spring Boot 3 中整合 MyBatis-Plus 并实现多表联合查询 #### 步骤 1:添加依赖 在 `pom.xml` 中添加 MyBatis-Plus 和数据库驱动依赖: ```xml <dependencies> <!-- MyBatis-Plus Starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.3.2</version> <!-- 确保与Spring Boot 3兼容 --> </dependency> <!-- MySQL 驱动 --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> </dependencies> ``` > **注意**:不要同时添加 `mybatis-spring-boot-starter`,避免依赖冲突[^3]。 --- #### 步骤 2:配置数据源 在 `application.yml` 中配置数据库: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test_db?useSSL=false&serverTimezone=UTC username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: configuration: map-underscore-to-camel-case: true # 开启驼峰命名映射 mapper-locations: classpath:mapper/*.xml # XML映射文件位置 ``` --- #### 步骤 3:创建实体类 假设有 `User` 和 `Order` 两个表: ```java // User.java @Data @TableName("user") public class User { @TableId(type = IdType.AUTO) private Long id; private String name; private Integer age; @TableField(exist = false) // 非数据库字段 private List<Order> orders; // 关联的订单 } ``` ```java // Order.java @Data @TableName("order") public class Order { @TableId(type = IdType.AUTO) private Long id; private String orderNo; private Long userId; // 关联用户ID } ``` --- #### 步骤 4:实现多表联合查询 ##### 方式 1:使用 XML 映射文件 1. **Mapper 接口**: ```java public interface UserMapper extends BaseMapper<User> { User selectUserWithOrders(@Param("userId") Long userId); } ``` 2. **XML 映射文件** (`resources/mapper/UserMapper.xml`): ```xml <select id="selectUserWithOrders" resultMap="UserWithOrdersResultMap"> SELECT u.*, o.id AS order_id, o.order_no FROM user u LEFT JOIN order o ON u.id = o.user_id WHERE u.id = #{userId} </select> <resultMap id="UserWithOrdersResultMap" type="User"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> <!-- 一对多映射 --> <collection property="orders" ofType="Order"> <id property="id" column="order_id"/> <result property="orderNo" column="order_no"/> </collection> </resultMap> ``` ##### 方式 2:使用注解 + Wrapper ```java @Select("SELECT u.*, o.id AS order_id, o.order_no " + "FROM user u LEFT JOIN order o ON u.id = o.user_id " + "${ew.customSqlSegment}") @ResultMap("UserWithOrdersResultMap") User selectUserWithOrders(@Param("ew") Wrapper wrapper); ``` --- #### 步骤 5:Service 层调用 ```java @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserWithOrders(Long userId) { QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("u.id", userId); return userMapper.selectUserWithOrders(wrapper); } } ``` --- #### 步骤 6:事务管理 在启动类添加注解: ```java @SpringBootApplication @MapperScan("com.example.mapper") // 扫描Mapper接口 @EnableTransactionManagement // 启用事务 public class Application { ... } ``` --- #### 关键注意事项 1. **实体类映射**:确保数据库字段名与实体类属性名匹配(开启驼峰命名映射)[^2]。 2. **SQL 注入防护**:使用 `#{}` 参数占位符,避免 `${}` 拼接 SQL[^2]。 3. **性能优化**:大数据量时启用分页: ```java Page<User> page = new Page<>(1, 10); userMapper.selectPage(page, wrapper); ``` 4. **多表查询限制**:MyBatis-Plus 的 `LambdaQueryWrapper` 主要用于单表操作,多表建议手写 SQL[^4]。 > 完整示代码:[MyBatis-Plus 多表查询示](https://github.com/baomidou/mybatis-plus-samples)
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值