SpringBoot整合Mybatis

SpringBoot使用mybatis只需引入对应的starter即可。

该starter不是由spring官方写的,我们在引 你 入依赖时需要自己指定版本号。

github地址

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.4.6</version>
</dependency>

引入依赖后只需要在yml配置文件中写入分页插件的配置即可,此处数据库配置省略

# 分页插件
pagehelper:
  # 数据库方言
  helper-dialect: mysql
  # 分页合理化
  reasonable: true
  # 启用注解分页参数
  support-methods-arguments: true

CityMapper

package top.yiyu.ch05mybatis.mapper;

import org.apache.ibatis.annotations.Param;
import top.yiyu.ch05mybatis.domain.City;

import java.util.List;

/**
 * @ClassName CityMapper
 * @Author THF
 * @Date 2023/2/15 10:41
 **/
public interface CityMapper {
    List<City>listCity();

    List<City>listCityParams(@Param("pageNum")int pageNum,@Param("pageSize")int pageSize);
}

City

package top.yiyu.ch05mybatis.domain;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @ClassName City
 * @Author THF
 * @Date 2023/2/15 10:40
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class City {
    private Integer cityId;
    private String cityName;
    private String cityCode;
    private String province;
}

CityMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.yiyu.ch05mybatis.mapper.CityMapper">
    <resultMap id="cityMap" type="city">
        <id column="city_id" property="cityId"/>
        <result column="city_name" property="cityName"/>
        <result column="city_code" property="cityCode"/>
        <result column="province" property="province"/>
    </resultMap>
    <select id="listCity" resultMap="cityMap">
        select city_id,city_name,city_code,province from city_info
    </select>
    <select id="listCityParams" resultMap="cityMap">
        select city_id,city_name,city_code,province from city_info
    </select>
</mapper>

测试:

package top.yiyu.ch05mybatis;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import top.yiyu.ch05mybatis.domain.City;
import top.yiyu.ch05mybatis.mapper.CityMapper;

import java.util.List;

@Slf4j
@SpringBootTest
class Ch05MybatisApplicationTests {

    @Autowired
    private CityMapper cityMapper;

    @Test
    void contextLoads() {
        // 方式一
        Page<City> page = PageHelper.startPage(1, 10);
        List<City> cities = cityMapper.listCity();
        cities.forEach(city -> {
            log.info(city.toString());
        });
        log.info(page.getTotal()+"");
    }

    @Test
    void testMapperParamsPageInfo(){
        //方式二
        List<City> cities = cityMapper.listCityParams(1, 10);
        PageInfo<City> cityPageInfo = new PageInfo<>(cities);
        log.info(cityPageInfo.getTotal()+"");
    }

}
最后修改:2023 年 02 月 20 日
如果觉得我的文章对你有用,请随意赞赏