springboot mybatis 分页整合

Posted 自由港

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot mybatis 分页整合相关的知识,希望对你有一定的参考价值。

spring boot 整合mybatis ,分两块mybatis 整合,分页整合。

 

1.pom文件增加

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.6</version>
        </dependency>
        
         
         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.26</version>
        </dependency>

2.创建表

CREATE TABLE `sale_order` (
  `ID_` varchar(64) NOT NULL DEFAULT ‘‘ COMMENT 主键,
  `NAME_` varchar(64) DEFAULT NULL COMMENT 订单名称,
  `TOTAL_` decimal(14,2) DEFAULT NULL COMMENT 合计,
  `CREATOR_` varchar(64) DEFAULT NULL COMMENT 制单人,
  `CREATE_TIME_` datetime DEFAULT NULL COMMENT 创建时间,
  `TENANT_ID_` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`ID_`)
)

3.创建SaleOrder.map.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.neo.dao.SaleOrderDao">
    <resultMap id="SaleOrder" type="com.neo.model.SaleOrder">
        <id property="id" column="ID_" jdbcType="VARCHAR"/>
        <result property="name" column="NAME_" jdbcType="VARCHAR"/>
        <result property="total" column="TOTAL_" jdbcType="NUMERIC"/>
        <result property="creator" column="CREATOR_" jdbcType="VARCHAR"/>
        <result property="createTime" column="CREATE_TIME_" jdbcType="TIMESTAMP"/>
        
    </resultMap>
    
    <insert id="create" parameterType="com.neo.model.SaleOrder">
        INSERT INTO SALE_ORDER
        (ID_,NAME_,TOTAL_,CREATOR_,CREATE_TIME_)
        VALUES 
        (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{total,jdbcType=NUMERIC}, #{creator,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP})
    </insert>
    
    <select id="get"   parameterType="java.lang.String" resultMap="SaleOrder">
        SELECT * FROM SALE_ORDER 
        WHERE 
        ID_=#{id}
    </select>
    
    <select id="query" parameterType="java.util.Map" resultMap="SaleOrder">
        SELECT * FROM SALE_ORDER
        
    </select>
    
    <update id="update" parameterType="com.neo.model.SaleOrder">
        UPDATE SALE_ORDER SET
        NAME_=#{name,jdbcType=VARCHAR},
        TOTAL_=#{total,jdbcType=NUMERIC},
        CREATOR_=#{creator,jdbcType=VARCHAR}
        WHERE
        ID_=#{id}
    </update>
    
    <delete id="remove" parameterType="java.lang.String">
        DELETE FROM SALE_ORDER 
        WHERE
        ID_=#{id}
    </delete>
    
    
</mapper>

注意命名空间

com.neo.dao.SaleOrderDao

配置map文件扫描。

mybatis:
  typeAliasesPackage: com.neo.model
  mapperLocations: classpath:mapper/*.xml

 

4.增加Dao类

package com.neo.dao;

import com.github.pagehelper.Page;
import com.neo.model.SaleOrder;


public interface SaleOrderDao {
    
    int create(SaleOrder record);
    
    void update(SaleOrder record);
    
    SaleOrder get(String id);
    
    
    Page<SaleOrder> query();

}

5.配置dao类扫描。

package com;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.neo.filter.ApplicationStartedEventListener;
import com.neo.filter.ApplicationStartedEventListener2;
import com.neo.filter.ApplicationStartingEventListener;

@SpringBootApplication
@MapperScan({"com.neo.dao"}) 
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication app=new SpringApplication(DemoApplication.class);
        app.addListeners(new ApplicationStartedEventListener());
        app.addListeners(new ApplicationStartingEventListener());
        app.addListeners(new ApplicationStartedEventListener2());
        app.run(args);
    }
}

这里配置了MapperScan 注解。

6.配置分页

package com.neo.conf;

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.pagehelper.PageHelper;

@Configuration
public class PageConfiguration {
    
    @Bean
    public PageHelper pageHelper(){
         PageHelper pageHelper = new PageHelper();
         Properties properties = new Properties();
         properties.setProperty("offsetAsPageNum","true");
         properties.setProperty("rowBoundsWithCount","true");
         properties.setProperty("reasonable","true");
         properties.setProperty("dialect","mysql");    //配置mysql数据库的方言
        pageHelper.setProperties(properties);
        return pageHelper;
    }

}

7.编写业务类

package com.neo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.neo.dao.SaleOrderDao;
import com.neo.model.SaleOrder;
@Service
public class SaleOrderService {
    
    @Autowired
    SaleOrderDao orderDao;
    
    public void create(SaleOrder order){
        orderDao.create(order);
    }
    
    public SaleOrder get(String id){
        return orderDao.get(id);
    }
    
    public Page<SaleOrder> getAll(int page){
         PageHelper.startPage(page, 2);
         return orderDao.query();
    }
}

8.配置打印SQL

在application.yml 文件中增加

logging:
  level:
     com.neo.dao: debug

9.编写控制器代码

@RequestMapping("/add")
    public void add(){
        SaleOrder order=new SaleOrder();
        order.setId(System.currentTimeMillis() +"");
        order.setName("zyg");
        order.setCreator("AA");
        order.setTotal(33D);
        saleOrderService.create(order);
    }
    
    @RequestMapping("/get/{id}")
    public SaleOrder get(@PathVariable(value="id") String id){
   
        SaleOrder order= saleOrderService.get(id);
        return order;
    }
    
    @RequestMapping("/page/{page}")
    public Page<SaleOrder> page(@PathVariable(value="page") int page){
        Page<SaleOrder> order= saleOrderService.getAll(page);
        return order;
    }

 

以上是关于springboot mybatis 分页整合的主要内容,如果未能解决你的问题,请参考以下文章

springboot整合mybatis拦截器分页

springboot 整合Mybatis-Plus分页自动填充功能

Springboot整合Mybatis 之分页插件使用

springboot使用Mybatis分页插件

Spring和SpringBoot整合MybatisPlus配置分页查询

SpringBoot整合PageHelper实现分页查询