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());//获取当前页的记录
}
}