Springboot集成ShardingSphere和mybatis-plus实现分表

在数据库中创建两张表

CREATE TABLE `t_user_0` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `t_user_1` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

新建SpringBoot工程

 

引入相关依赖

    <!-- mysql 相关依赖 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <!-- mybatis-plus 相关依赖 -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.3</version>
    </dependency>

    <!--shardingsphere最新版本-->
    <dependency>
        <groupId>org.apache.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
        <version>4.1.1</version>
    </dependency>

 

属性文件(application.properties)配置如下:

server.port=8080

spring.shardingsphere.datasource.names=master

# 数据源 主库
spring.shardingsphere.datasource.master.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.jdbc-url=jdbc:mysql://192.168.1.138:3306/test?characterEncoding=utf-8
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=8ql6!Qqq

#数据分表规则
#指定所需分的表
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=master.t_user_$->{0..1}
#指定主键
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column=id
#分表规则为主键除以3取模
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression=t_user_$->{id % 2}

#打印sql
spring.shardingsphere.props.sql.show=true

 

ShardingJdbcApplication启动类
@SpringBootApplication
@MapperScan("com.gaopeng.sharding.jdbc.mapper")
public class ShardingJdbcApplication {

    public static void main(String[] args) {
        SpringApplication.run(ShardingJdbcApplication.class, args);
    }

}

User:
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName("t_user")
public class User extends Model<User> {
    /**
     * 主键Id
     */
    private int id;
    /**
     * 名称
     */
    private String name;
    /**
     * 年龄
     */
    private int age;

}

 

UserMapper:

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gaopeng.sharding.jdbc.entity.User;

public interface UserMapper extends BaseMapper<User> {

}

 

UserService:

import com.baomidou.mybatisplus.extension.service.IService;
import com.gaopeng.sharding.jdbc.entity.User;

import java.util.List;

public interface UserService extends IService<User> {

    /**
     * 保存用户信息
     *
     * @param entity
     * @return
     */
    @Override
    boolean save(User entity);

    /**
     * 查询全部用户信息
     *
     * @return
     */
    List<User> getUserList();

}

 

UserServiceImpl:
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaopeng.sharding.jdbc.entity.User;
import com.gaopeng.sharding.jdbc.mapper.UserMapper;
import com.gaopeng.sharding.jdbc.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public boolean save(User entity) {
        return super.save(entity);
    }

    @Override
    public List<User> getUserList() {
        return baseMapper.selectList(Wrappers.<User>lambdaQuery());
    }
}

 

UserController:
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/select")
    public List<User> select() {
        return userService.getUserList();
    }

    @GetMapping("/insert")
    public Boolean insert(User user) {
        return userService.save(user);
    }
}

 

posted @ 2021-06-08 18:43  gaopengpy  阅读(2280)  评论(0编辑  收藏  举报