1. 程式人生 > >springboot mybatis 分頁整合

springboot mybatis 分頁整合

not pri sql數據庫 cto can current map druid main

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 分頁整合