赞
踩
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.4.2</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</artifactId>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.1.3</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.1.23</version>
- </dependency>
- <dependency>
- <groupId>org.apache.shardingsphere</groupId>
- <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
- <version>4.1.1</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- <exclusions>
- <exclusion>
- <groupId>org.junit.vintage</groupId>
- <artifactId>junit-vintage-engine</artifactId>
- </exclusion>
- </exclusions>
- </dependency>

- package com.example.sharding.domain;
-
- import com.baomidou.mybatisplus.annotation.TableName;
-
- @TableName("orders_t")
- public class Orders {
- private Integer id;
- private Integer orderType;
- private Integer customerId;
- private Double amount;
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public Integer getOrderType() {
- return orderType;
- }
-
- public void setOrderType(Integer orderType) {
- this.orderType = orderType;
- }
-
- public Integer getCustomerId() {
- return customerId;
- }
-
- public void setCustomerId(Integer customerId) {
- this.customerId = customerId;
- }
-
- public Double getAmount() {
- return amount;
- }
-
- public void setAmount(Double amount) {
- this.amount = amount;
- }
-
- @Override
- public String toString() {
- return "Orders{" +
- "id=" + id +
- ", orderType=" + orderType +
- ", customerId=" + customerId +
- ", amount=" + amount +
- '}';
- }
- }
-

- package com.example.sharding.mapper;
-
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.example.sharding.domain.Orders;
- import org.apache.ibatis.annotations.*;
- import org.springframework.stereotype.Repository;
-
- import java.util.List;
-
- @Repository
- @Mapper
- public interface OrdersMapper extends BaseMapper<Orders> {
-
- @Insert("insert into orders_t(id,order_type,customer_id,amount) values(#{id},#{orderType},#{customerId},#{amount})")
- public int insert(Orders orders);
-
- @Select("select * from orders_t where id = #{id}")
- @Results({
- @Result(property = "id",column = "id"),
- @Result(property = "orderType",column = "order_type"),
- @Result(property = "customerId",column = "customer_id"),
- @Result(property = "amount",column = "amount")
- })
- public Orders selectOne(Integer id);
-
- @Select("select * from orders_t")
- @Results({
- @Result(property = "id",column = "id"),
- @Result(property = "orderType",column = "order_type"),
- @Result(property = "customerId",column = "customer_id"),
- @Result(property = "amount",column = "amount")
- })
- List<Orders> selectAll();
- }

mybatis: type-aliases-package: com.example.mapper mybatis-plus: mapperLocations: classpath*:mapper/*Mapper.xml spring: main: allow-bean-definition-overriding: true # 需要配置否则加载数据源报错 是否允许定义重名的bean对象覆盖原有的bean profiles: active: database sharding-sphere: datasource: names: ds1 # Configuring the second database datasource.ds1: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: "jdbc:mysql://127.0.0.1:3306/lable7?serverTimezone=UTC" username: root password: root # Configuring the database sharding strategy sharding: tables: orders_t: actual-data-nodes: ds1.orders_t_$->{1..2} key-generator: column: id type: SNOWFLAKE table-strategy: inline: sharding-column: id algorithm-expression: orders_t_${id%2+1}
- package com.example.sharding;
-
- import com.example.sharding.domain.Orders;
- import com.example.sharding.mapper.OrdersMapper;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- import java.util.List;
-
- @SpringBootTest
- class ShardingsphereDemoApplicationTests {
-
- @Autowired
- private OrdersMapper ordersMapper;
- @Test
- public void addOrders(){
- for (int i = 1; i <=10 ; i++) {
- Orders orders = new Orders();
- orders.setId(i);
- orders.setCustomerId(i);
- orders.setOrderType(i);
- orders.setAmount(1000.0*i);
- ordersMapper.insert(orders);
- }
- }
- @Test
- public void queryOrders(){
- // Orders orders = ordersMapper.selectOne(1);
- List<Orders> orderss = ordersMapper.selectAll();
- Orders orders = ordersMapper.selectById(1);
- System.out.println(orders);
- }
-
- }

7、注意mybatis-plus要添加@TableName(“你的表”)不然有可能找不到表
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。