[SSM]MyBatis的注解式开发与PageHelper

目录

十五、MyBatis使用PageHelper

15.1 limit分页

15.2PageHelper插件

第一步:引入依赖pom.xml

第二步:在mybatis-config.xml文件中配置插件

第三步:编写Java代码

十六、MyBatis的注解式开发

16.1@Insert

16.2@Delete

16.3@Update

16.4@Select


十五、MyBatis使用PageHelper

15.1 limit分页

[SSM]MyBatis的注解式开发与PageHelper_第1张图片

  • mysql的limit后面有两个数字:

    • 第一个数字:startIndex(起始下标,下标从0开始)

    • 第二个数字:pageSize(每页显示的记录条数)

  • 假设已知页码pageNum,还有每页显示的记录条数pageSize。则startIndex=(pageNum-1)*pageSize。

  • 标准通用的mysql分页SQL:

select
 *
from
 tableName ......
limit
 (pageNum - 1) * pageSize, pageSize

 

CarMapper接口

List selectByPage(@Param("startIndex") int startIndex, @Param("pageSize") int pageSize);

CarMapper.xml

 

CarMapperTest

@Test
    public void testSelectByPage() {
        //获取每页显示的记录条数
        int pageSize = 3;
        //显示第几页
        int pageNum = 4;
        //计算开始的下标
        int startIndex = (pageNum - 1) * pageSize;
​
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        List cars = mapper.selectByPage(startIndex, pageSize);
        cars.forEach(car -> System.out.println(car));
        sqlSession.commit();
        sqlSession.close();
    }

15.2PageHelper插件

  • 使用PageHelper插件进行分页,更加的便捷。

第一步:引入依赖pom.xml


 com.github.pagehelper
 pagehelper
 5.3.1

第二步:在mybatis-config.xml文件中配置插件

  • typeAliases标签下面进行配置:


 

第三步:编写Java代码

CarMapper接口

List selectAll();

CarMapper.xml

  • 注意:select语句最后不加 ;

PageTest.testPageHelper

  @Test
    public void testSelectAll(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
​
        //注意:在执行DQL语句之前,开启分页功能。
        int pageNum=2;
        int pageSize=3;
        PageHelper.startPage(pageNum,pageSize);
​
        List cars = mapper.selectAll();
        //cars.forEach(car -> System.out.println(car));
​
        //封装分页信息对象 new PageInfo()
        //pageInfo对象是PageHelper插件提供的,用来封装分页相关相关信息的对象。
        //navigatePages用来设置导航栏中的页码数量
        PageInfo carPageInfo = new PageInfo<>(cars, 3);
        System.out.println(carPageInfo);
        sqlSession.commit();
        sqlSession.close();
    }

执行结果

PageInfo{
 pageNum=2, pageSize=2, size=2, startRow=3, endRow=4, total=6, pages=3,
 list=Page{count=true, pageNum=2, pageSize=2, startRow=2, endRow=4, total=
6, pages=3, reasonable=false, pageSizeZero=false}
 [Car{id=86, carNum='1234', brand='丰⽥霸道', guidePrice=50.5, produceTime
='2020-10-11', carType='燃油⻋'},
 Car{id=87, carNum='1234', brand='丰⽥霸道', guidePrice=50.5, produceTime
='2020-10-11', carType='燃油⻋'}],
 prePage=1, nextPage=3, isFirstPage=false, isLastPage=false, hasPreviousPa
ge=true, hasNextPage=true,
 navigatePages=5, navigateFirstPage=1, navigateLastPage=3, navigatepageNum
s=[1, 2, 3]
}

十六、MyBatis的注解式开发

  • mybatis中提供了注解式开发方式,采用注解可以减少sql映射文件的配置。

  • 使用注解式开发的话,sql语句是写在java程序中的,这种方式会给sql语句的维护带来成本。

  • 使用注解来映射简单语句会使代码显得更加简洁,但对于稍微复杂⼀点的语句,Java 注解不仅⼒不从心,还会让你本就复杂的 SQL 语句更加混乱不堪。 因此,如果你需要做⼀些很复杂的操作,最好⽤ XML 来映射语句。

  • 原则:简单sql可以注解,复杂sql使用xml。

16.1@Insert

CarMapper接口

@Insert("insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})")
    int insert(Car car);

AnnotationTest.testInsert

   @Test
    public void testInsert() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = new Car(null, "6666", "丰田霸道", 32.0, "2020-11-11", "燃油车");
        int insert = mapper.insert(car);
        System.out.println(insert);
        sqlSession.commit();
        sqlSession.close();
    }

16.2@Delete

CarMapper接口

@Delete("delete from t_car where id = #{id}")
    int deleteById(Long id);

AnnotationTest.testInsert

   @Test
    public void testDeleteById(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        mapper.deleteById(41L);
        sqlSession.commit();
        sqlSession.close();
    }

16.3@Update

CarMapper接口

 @Update("update t_car set car_num=#{carNum},brand=#{brand},guide_price=#{guidePrice},produce_time=#{produceTime},car_type=#{carType} where id=#{id}")
    int update(Car car);

AnnotationTest.testInsert

    @Test
    public void testUpdate(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = new Car(47L, "6666", "丰田霸道", 32.0, "2020-11-11", "燃油车");
        int insert = mapper.update(car);
        System.out.println(insert);
        sqlSession.commit();
        sqlSession.close();
    }

16.4@Select

CarMapper接口

@Select("select * from t_car where id=#{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "carNum", column = "car_num"),
            @Result(property = "brand", column = "brand"),
            @Result(property = "guidePrice", column = "guide_price"),
            @Result(property = "produceTime", column = "produce_time"),
            @Result(property = "carType", column = "car_type")
    })
    Car selectById(Long id);

AnnotationTest.testInsert

@Test
    public void testSelectById() {
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = mapper.selectById(39L);
        System.out.println(car);
        sqlSession.commit();
        sqlSession.close();
    }

你可能感兴趣的:(java,mybatis)