当前位置:Gxlcms > 数据库问题 > spring整合sharding-jdbc实现分库分表

spring整合sharding-jdbc实现分库分表

时间:2021-07-01 10:21:17 帮助过:105人阅读

com.sharding.mapper; import com.sharding.pojo.vo.Order; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.util.List; public interface OrderMapper { @Insert("insert into t_order values(#{item.id},#{item.amount},#{item.name},#{item.userId})") public void insert(@Param("item") Order order); @Select("select " + " id,amount,name,user_id as ‘userId‘ from t_order where name=#{name}") public List<Order> getOrderByName(String name); }
package com.sharding;

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

@SpringBootApplication
@ImportResource("classpath*:sharding-jdbc.xml")
@MapperScan("com.sharding.mapper")
public class ShardingTest {

    public static void main(String[] args) {
        SpringApplication.run(ShardingTest.class);
    }
}
package com.sharding.test;

import com.sharding.mapper.OrderMapper;
import com.sharding.pojo.vo.Order;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class AppTest {

    @Autowired
    private OrderMapper orderMapper;

    @Test
    public void testInsert(){

      Order order = new Order();
      order.setId(10);
      order.setUserId(20);
      order.setAmount(200);
      order.setName("Test");
      orderMapper.insert(order);
    }

    @Test
    public void testQuery(){
        List<Order> test = orderMapper.getOrderByName("Test");
        System.out.println("查询结果:" + test);
    }
}

 

  

 

spring整合sharding-jdbc实现分库分表

标签:use   sharp   space   his   mybatis   HERE   tin   engine   ati   

人气教程排行