[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分页

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

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

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

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

  • 标准通用的mysql分页SQL:

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

 

CarMapper接口

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

CarMapper.xml

 <select id="selectByPage" resultType="car">
        select *
        from t_car limit #{startIndex},#{pageSize};
    </select>

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<Car> cars = mapper.selectByPage(startIndex, pageSize);
        cars.forEach(car -> System.out.println(car));
        sqlSession.commit();
        sqlSession.close();
    }

15.2PageHelper插件

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

第一步:引入依赖pom.xml
<dependency>
 <groupId>com.github.pagehelper</groupId>
 <artifactId>pagehelper</artifactId>
 <version>5.3.1</version>
</dependency>
第二步:在mybatis-config.xml文件中配置插件
  • typeAliases标签下面进行配置:

<plugins>
 <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
第三步:编写Java代码

CarMapper接口

List<Car> selectAll();

CarMapper.xml

<select id="selectAll" resultType="Car">
 select * from t_car
</select>
  • 注意: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<Car> cars = mapper.selectAll();
        //cars.forEach(car -> System.out.println(car));
​
        //封装分页信息对象 new PageInfo()
        //pageInfo对象是PageHelper插件提供的,用来封装分页相关相关信息的对象。
        //navigatePages用来设置导航栏中的页码数量
        PageInfo<Car> 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();
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ja kar ta

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值