springboot+mybatis+mybatis_plus+pageHelper整合

本文主要涉及到mybatis,mybatis-plus,pageHelper三种技术
1、pom文件



    com.github.pagehelper
    pagehelper-spring-boot-starter
    1.2.3



    com.baomidou
    mybatis-plus
    2.1.9


    com.baomidou
    mybatisplus-spring-boot-starter
    1.0.5



    com.fasterxml.jackson.datatype
    jackson-datatype-jsr310
    2.8.6


    org.projectlombok
    lombok

2、application.yml

mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true
  global-config:
    db-column-underline: true
    id-type: 1
    capital-mode: true
pagehelper:
  reasonable: false
  support-methods-arguments: true
  params: count=countSql
  row-bounds-with-count: true
  helper-dialect: mysql

3、MybatisPlusConfig.java

@Configuration
public class MybatisPlusConfig {

    @Bean
    public PerformanceInterceptor performanceInterceptor(){
        return new PerformanceInterceptor();
    }

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

4、PhoneUser.java

@Data
@EqualsAndHashCode(callSuper = false)
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@TableName("mybatis_plus")
public class PhoneUser extends Model {
    /**
     * 编号
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 联系方式
     */
    @NotEmpty(message = "联系方式不可为空")
    private String phoneNumber;

    /**
     * 密码
     */
    private String password;

    /**
     * 注册时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime registerDate;

    @TableField(exist = false)
    private String remarks;

    private MybatisPlusChild mybatisPlusChild;

    @Override
    protected Serializable pkVal() {
        return this.id;
    }
}

5、PhoneUserMapper.java

@Repository
public interface PhoneUserMapper extends BaseMapper {

    /**
     * 获取mybatis_plus的联系方式列表
     *
     * @return 联系方式列表
     */
    @Select("SELECT * FROM mybatis_plus")
    @Results(id = "mybatisPlusMap", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "id", property = "mybatisPlusChild",
                    one = @One(select = "com.example.mybatis.mapper.PhoneUserMapper.getChild")),
            @Result(column = "phone_number", property = "phoneNumber")
    })
    List getList();

    /**
     * 获取mybatis_plus_child的记录
     *
     * @param id mybatis_plus_id
     * @return
     */
    @Select("SELECT * FROM mybatis_plus_child WHERE 1=1 AND mybatis_plus_id=#{id}")
    MybatisPlusChild getChild(@Param("id") Long id);

    /**
     * 此处使用sqlProvider实现查询
     *
     * @param user
     * @return 查询结果
     */
    @SelectProvider(type = PhoneUserProvider.class, method = "select")
    @ResultMap(value = "mybatisPlusMap")
    List getListBySqlProvider(PhoneUser user);

    /**
     * 批量更新
     *
     * @return 是否成功,1,代表成功
     */
    @Update({""})
    int updateBatchById(@Param("list") List list);

    /**
     * 批量删除
     * @param list id列表
     * @return 删除结果
     */
    @Delete({""})
    int deleteBatchByClassifyIds(@Param("list") List list);

}

6、PhoneUserProvider.java

public class PhoneUserProvider {

    public String select(PhoneUser user) {
        StringBuffer sql = new StringBuffer("SELECT * FROM mybatis_plus WHERE 1 = 1");
        if (user.getPassword() != null) {
            sql.append(" AND password = #{password}");
        }
        return sql.toString();
    }
}

7、PhoneUserService.java

@Service
public class PhoneUserService extends ServiceImpl {

    @Autowired
    PhoneUserMapper mapper;

    public List selectList() {
        List array = new ArrayList<>();
        array.add("id");

        List list = mapper.selectList(
                new EntityWrapper()
                        .where("password", "123")
//                        .eq("password", "123")
                        .or("password=123456")
//                        .andNew("id={0}", "1")
                        .like("phone_number", "1316")
//                        .groupBy("password")
                        .orderDesc(array)
        );
        return list;
    }
}

你可能感兴趣的:(springboot+mybatis+mybatis_plus+pageHelper整合)