springboot下整合mybatis - 1 - 多表查询

1.  控制层下通过  注解 的方式 整合mybaits  编写处理逻辑和前端返回数据

  @RequestMapping("/air/findAll")
    public List findAll(){
        List<User> users = userMapper.selectAllAirById();
        users.forEach(System.out::println);
        List<User> users2 = new ArrayList<>();
       users.forEach(user -> {
           if(user.getAir()!=null){
               users2.add(user);
           }
       });
        return users2;
    }
    @RequestMapping("/User/findAll")
    public List findAll1(){
        List<air> airs = airMapper2.selectAllUserById();
        List<air> airs2 = new ArrayList<>();
        airs.forEach(air -> {
            if(air.getUser()!=null){
                airs2.add(air);
            }
        });
        return  airs2;
    }

2, Mapper层下 做 相应类和sql语句的映射  

查询的sql字段对应类的属性值 从而完成映射连接

说明  ;接口继承的BaseMapper可实现 Mybatisplus的作用 。 

package com.example.springboot1.Mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springboot1.pojo.User;
import com.example.springboot1.pojo.air;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper // 运行时 实现类对象 (动态代理对象)
public interface AirMapper extends BaseMapper<air> {
   @Select("select * from airinfro where Id=#{id}")
   public air selectById(String id);

   @Select("select * from airinfro")
   @Results({
           @Result(column = "id",property = "id"),
           @Result(column = "planeName",property = "planeName"),
           @Result(column = "ran",property = "ran"),
           @Result(column = "id",property = "user",javaType = User.class ,  // 指定映射关系 id => user
                   one=@One(select = "com.example.springboot1.Mapper.UserMapper.selectByAirId") //
           )
   })
   List<air> selectAllUserById();

 // 查询全部用户信息
    // 配置sql提示
    @Select("select * from airinfro")
    public List<air> list();
    @Delete("delete from airinfro where id=#{id}")
    public int delete(String id);
    @Select("select * from airinfro where id=#{id}")
    public air query(String id);
    @Insert("insert into airinfro values(#{id},#{planeName},#{ran})")
    public int insert(air air);
    @Update("update airinfro set id=#{id} ,planeName=#{planeName},ran=#{ran} where id=#{UPid}")
    public int update(String id,String planeName,String ran,String UPid);
}

package com.example.springboot1.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springboot1.pojo.User;
import com.example.springboot1.pojo.air;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    // 配置sql提示
    @Select("select * from clentinf")
    public List<air> list();

    @Select("select * from clentinf where airId=#{airId}")
    public User selectByAirId(String airId);  // 选择

    @Select("select * from clentinf")
    @Results({
            @Result(column = "Cid",property = "Cid"),
            @Result(column = "payment",property = "payment"),
            @Result(column = "Cname",property = "Cname"),
            @Result(column = "Connection",property = "Connection"),
            @Result(column = "airId",property = "airId"),
            @Result(column = "airId",property = "air",javaType = air.class ,  // 指定映射关系
                    one=@One(select = "com.example.springboot1.Mapper.AirMapper.selectAllUserById")
            )})
    List<air> selectAllAirById();
    @Delete("delete from clentinf where Cid=#{id}")
    public int delete(String id);
    @Select("select * from clentinf where Cid=#{id}")
    public air query(String id);
    @Insert("insert into clentinf values(#{Cname},#{Connenction},#{Cid},#{payment},#{airId})")
    public int insert(User user);
    @Update("update clentinf set Cname=#{Cname} ,payment=#{payment},airId=#{id},Connection=#{Connection},airId=#{airId} where Cid=#{UPid}")
    public int update(String Cname,Integer payment,String Connection,String Cid,String airId ,String UPid);
}

MybatisPlus适用于实现 单表查询 功能 ,多表查询语句需自己编写

//  mybaitsplus源码
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.baomidou.mybatisplus.core.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;

public interface BaseMapper<T> extends Mapper<T> {
    int insert(T entity);

    int deleteById(Serializable id);

    int deleteByMap(@Param("cm") Map<String, Object> columnMap);

    int delete(@Param("ew") Wrapper<T> queryWrapper);

    int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);

    int updateById(@Param("et") T entity);

    int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);

    T selectById(Serializable id);

    List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);

    List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);

    T selectOne(@Param("ew") Wrapper<T> queryWrapper);

    Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);

    List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);

    List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);

    List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);

    <E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);

    <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param("ew") Wrapper<T> queryWrapper);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!要在Spring Boot整合MyBatis-Plus,你可以按照以下步骤进行操作: 步骤1:添加依赖 在你的Spring Boot项目的pom.xml文件中,添加MyBatis-Plus的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本号</version> </dependency> ``` 请确保将`最新版本号`替换为MyBatis-Plus最新的版本号。 步骤2:配置数据源 在application.properties(或application.yml)文件中,配置数据库连接信息,例如: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/db_example spring.datasource.username=db_username spring.datasource.password=db_password spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 请将`db_example`、`db_username`和`db_password`分别替换为你的数据库名称、用户名和密码。 步骤3:创建实体类和Mapper接口 创建对应的实体类,并使用`@TableName`注解指定数据库表名。然后创建Mapper接口,继承自`BaseMapper`。 ```java import com.baomidou.mybatisplus.annotation.TableName; @TableName("user") public class User { private Long id; private String username; private String email; // getters and setters } ``` ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface UserMapper extends BaseMapper<User> { } ``` 步骤4:编写Service和Controller层代码 在Service层中,可以通过注入Mapper对象来使用MyBatis-Plus提供的便捷方法。例如: ```java import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class UserServiceImpl implements UserService { @Resource private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.selectById(id); } // 其他业务逻辑方法 } ``` 在Controller层中,可以直接调用Service层的方法来处理请求。例如: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController public class UserController { @Resource private UserService userService; @GetMapping("/users/{id}") public User getUserById(@PathVariable Long id) { return userService.getUserById(id); } // 其他请求处理方法 } ``` 这样,你就完成了Spring BootMyBatis-Plus的整合。你可以根据自己的需求,使用MyBatis-Plus提供的便捷方法来进行数据库操作。 希望对你有所帮助!如果还有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值