Springboot整合mybaits plus实现分页条件多表查询

1.实体类中所需注解

@TableName(value = "tbl_student")实体类与表名进行映射

 //使用递增  要想使用递增策略:必须保证表中的id递增  需要在主键上在加上  @TableId(type = IdType.AUTO)

@Tableid注解的value属性值为表中主键的字段名既可以对应上

@TableField(exist = false) 表中不存在该字段

@TableField(value = "name")   实体类的属性名与表中列名不一致时要进行修改

//@TableName(value = "tbl_student")实体类与表名进行映射
public class Student {
    //表中的主键名不为id时,要进行人为修改
    //mp中默认主键的生成策略:  雪花算法 唯一id值
    //使用递增  要想使用递增策略:必须保证表中的id递增  需要在主键上在加上  @TableId(type = IdType.AUTO)
    @TableId(type = IdType.AUTO)
    private int sid;
    //@TableField(value = "name")   实体类的属性名与表中列名不一致时要进行修改
    private String sname;
    private int age;
    private int cid;


    @TableField(exist = false)   //表中不存在该字段
    private Class aClass;
}

2.实现分页条件多表查询

 1.创建分页插件

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

2.StudentMapper接口

public interface StudentMapper extends BaseMapper<Student> {
    IPage<Student> findALl(IPage<Student> page,@Param("ew") Wrapper<Student> wrapper);
}

3.mapper.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.zyx.springboot3.mapper.StudentMapper">

    <resultMap id="BaseResultMap" type="com.zyx.springboot3.pojo.Student">
            <id property="sid" column="sid" jdbcType="INTEGER"/>
            <result property="sname" column="sname" jdbcType="VARCHAR"/>
            <result property="age" column="age" jdbcType="INTEGER"/>
            <result property="cid" column="cid" jdbcType="INTEGER"/>
        <association property="aClass" javaType="com.zyx.springboot3.pojo.Class" autoMapping="true">
            <id property="cid" column="cid"/>
        </association>
    </resultMap>

    <sql id="Base_Column_List">
        sid,sname,age,
        cid,c.cname
    </sql>
    <select id="findALl" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from student s join class c on s.cid=c.cid
        //判断条件
        <if test="ew !=null">
            <where>
                ${ew.sqlSegment}
            </where>
        </if>
    </select>
</mapper>

4.Test测试类

@SpringBootTest
class Springboot3ApplicationTests {
    @Autowired
    private StudentMapper studentMapper;
     @Test
    void findAllPage(){
        Page<Student> page = new Page<>(1,3);
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.le("sid",5);
        studentMapper.findALl(page, wrapper);
        System.out.println("当前页的记录"+page.getRecords());//获取当前页的记录
        System.out.println("获取总页数"+page.getPages());//获取当前页的记录
        System.out.println("获取总条数"+page.getTotal());//获取当前页的记录
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值