spring boot集成mybatis 并加入分页

pom文件依赖包

<!-- mysql 数据库驱动. -->

    <dependency>

  <groupId>mysql</groupId>

  <artifactId>mysql-connector-java</artifactId>

</dependency>

 

<!--  

  spring-boot mybatis依赖:

       -->

  <dependency>

      <groupId>org.mybatis.spring.boot</groupId>

      <artifactId>mybatis-spring-boot-starter</artifactId>

      <version>1.1.1</version>

  </dependency>

 

 

<!--

      MyBatis提供了拦截器接口,我们可以实现自己的拦截器,

      将其作为一个plugin装入到SqlSessionFactory中。

  Github上有位开发者写了一个分页插件,我觉得使用起来还可以,挺方便的。

  Github项目地址: https://github.com/pagehelper/Mybatis-PageHelper

     --> 

    <dependency>

      <groupId>com.github.pagehelper</groupId>

      <artifactId>pagehelper</artifactId>

      <version>4.1.0</version>

  </dependency>

配置

@Configuration
public class MyBatisConfiguration {
    
    @Bean
    public PageHelper pageHelper() {
        System.out.println("MyBatisConfiguration.pageHelper()");
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }
}

 mapper文件

@Select("select * from Student where name = #{name}")
    public List<Student> likeName(String name);

application.properties

 

########################################################
###datasource
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

 

使用方法

    @RequestMapping("/likeName")
    public List<Student> likeName(String name){
        System.out.println("in=========");
        PageHelper.startPage(1,1);
        return demoService.likeName(name);
    }

 

访问

http://127.0.0.1:8080/likeName?name=张三

注意:

需要建表

springboot学习笔记

=========================

spring boot jpa + hibernate

spring boot jpa + jdbctemplate

全局异常捕捉

thymeleaf 或者 freemaker ,两个模板引擎可以同时使用

返回jsp页面;

 

==========================================

                                                                                                                               伤心的刺猬-----记录

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值