Sharding_JDBC:定位为轻量级Java框架,在Java的JDBC层提供的额外服务。 它使用客户端直连数据库,以jar包形式提供服务,无需额外部署和依赖,可理解为增强版的JDBC驱动,完全兼容JDBC和各种ORM框架。
-适用于任何基于Java的ORM框架,如:JPA, Hibernate, Mybatis, Spring JDBC Template或直接使用JDBC。
-基于任何第三方的数据库连接池,如:DBCP, C3P0, BoneCP, Druid, HikariCP等。
-支持任意实现JDBC规范的数据库。目前支持MySQL,Oracle,SQLServer和PostgreSQL。
实现步骤
1、创建数据库和数据表
CREATE DATABASE nginxdemo;
USE nginxdemo;
create table user(
id int primary key not null AUTO_INCREMENT,
name VARCHAR(50)
);
2、使用Idea新建Springboot项目,file-->new-->project,左边列表选择Spring Initializr。
3、点击next,修改Group和Artifact,点击next。
4、点击next。
5、不用修改,点击finish。
6、打开项目跟目录下pom.xml文件,在dependencies父节点下添加如下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc</artifactId>
<version>4.0.0-RC2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC2</version>
</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>
7、点击右下角import changes
8、打开src-->main-->resources-->application.properties文件,添加如下代码
spring.shardingsphere.datasource.names=master,slave0
#将url、username、password改为自己数据库的配置
spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://121.199.78.138:3306/nginxdemo
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456
spring.shardingsphere.datasource.slave0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave0.url=jdbc:mysql://114.55.33.216:3306/nginxdemo
spring.shardingsphere.datasource.slave0.username=root
spring.shardingsphere.datasource.slave0.password=123456
# 读写分离配置
#查询时的负载均衡算法round_robin(轮询)和random(随机)
spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robin
spring.shardingsphere.masterslave.name=ms
spring.shardingsphere.masterslave.master-data-source-name=master
spring.shardingsphere.masterslave.slave-data-source-names=slave0
spring.shardingsphere.props.sql.show=true
9、根据下图所示项目结构。新建entity包,User类;controller包,UserController类;dao包,UserDao接口。
10、User类代码
package com.ljessie.nginxdemo.entity;
import javax.persistence.*;
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
11、UserDao代码
package com.ljessie.nginxdemo.dao;
import com.ljessie.nginxdemo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserDao extends JpaRepository<User,Integer> {
}
12、UserController代码
package com.ljessie.nginxdemo.controller;
import com.ljessie.nginxdemo.dao.UserDao;
import com.ljessie.nginxdemo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;
@RestController
public class UserController {
@Autowired
private UserDao userDao;
@GetMapping("save_user/{name}")
public User save(@PathVariable String name){
User user = new User();
user.setName(name);
return userDao.save(user);
}
@GetMapping("find_all")
public Object findAll(){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("receive request at:"+sdf.format(date));
return userDao.findAll();
}
}
13、在Application类名上面添加注释
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
package com.ljessie.nginxdemo;
//import io.shardingjdbc.core.api.MasterSlaveDataSourceFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class NginxdemoApplication {
public static void main(String[] args) {
SpringApplication.run(NginxdemoApplication.class, args);
}
}
整合完成,可以启动项目,访问http://localhost:8080/find_all进行测试。