E590 第7周作业10 · JavaCourse00/JavaCourseCodes@90cc0da · GitHub
[go: up one dir, main page]

Skip to content

Commit 90cc0da

Browse files
author
nononi
committed
第7周作业10
1 parent ca0240d commit 90cc0da

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.nononi.shardingspheredemo.datasource;
2+
3+
import org.apache.commons.dbcp2.BasicDataSource;
4+
5+
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
6+
import org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
import javax.sql.DataSource;
11+
import java.sql.SQLException;
12+
import java.util.Arrays;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
import java.util.Properties;
16+
17+
@Configuration
18+
public class DataSourceConfig {
19+
20+
@Bean
21+
public DataSource dataSource() throws SQLException {
22+
// Configure actual data sources
23+
Map<String, DataSource> dataSourceMap = new HashMap<>();
24+
25+
// Configure master data source
26+
BasicDataSource masterDataSource = new BasicDataSource();
27+
masterDataSource.setDriverClassName("com.mysql.jdbc.Driver");
28+
masterDataSource.setUrl("jdbc:mysql://localhost:3316/db");
29+
masterDataSource.setUsername("root");
30+
masterDataSource.setPassword("");
31+
dataSourceMap.put("ds_master", masterDataSource);
32+
33+
// Configure the first slave data source
34+
BasicDataSource slaveDataSource1 = new BasicDataSource();
35+
slaveDataSource1.setDriverClassName("com.mysql.jdbc.Driver");
36+
slaveDataSource1.setUrl("jdbc:mysql://localhost:3326/db");
37+
slaveDataSource1.setUsername("root");
38+
slaveDataSource1.setPassword("");
39+
dataSourceMap.put("ds_slave", slaveDataSource1);
40+
41+
42+
// Configure read-write split rule
43+
MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration("ds_master_slave", "ds_master", Arrays.asList("ds_slave0"));
44+
45+
// Get data source
46+
DataSource dataSource = MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, masterSlaveRuleConfig, new Properties());
47+
return dataSource;
48+
}
49+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package io.nononi.shardingspheredemo.service;
2+
3+
public interface OrderService {
4+
void insert(int id, String name);
5+
void query();
6+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.nononi.shardingspheredemo.service.impl;
2+
3+
import io.nononi.shardingspheredemo.service.OrderService;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Component;
6+
7+
import javax.sql.DataSource;
8+
import java.sql.Connection;
9+
import java.sql.PreparedStatement;
10+
import java.sql.ResultSet;
11+
import java.sql.SQLException;
12+
13+
@Component
14+
public class OrderServiceImpl implements OrderService {
15+
16+
@Autowired
17+
private DataSource dataSource;
18+
19+
@Override
20+
public void insert(int id, String name) {
21+
try {
22+
Connection conn = dataSource.getConnection();
23+
String sql = "INSERT INTO `order` (id,name) VALUES (?,?);";
24+
PreparedStatement stmt = conn.prepareStatement(sql);
25+
stmt.setInt(1,id);
26+
stmt.setString(2,name);
27+
stmt.executeUpdate();
28+
29+
} catch (SQLException e) {
30+
e.printStackTrace();
31+
}
32+
33+
}
34+
35+
@Override
36+
public void query() {
37+
String sql = "SELECT * FROM `order`;";
38+
try {
39+
Connection conn = dataSource.getConnection();
40+
PreparedStatement stmt = conn.prepareStatement(sql);
41+
ResultSet resultSet = stmt.executeQuery();
42+
while (resultSet.next()) {
43+
System.out.print(resultSet.getInt("id") + " ");
44+
System.out.println(resultSet.getString("name") + " ");
45+
}
46+
} catch (SQLException e) {
47+
e.printStackTrace();
48+
}
49+
50+
}
51+
}

0 commit comments

Comments
 (0)
0