Spring实现数据库读写分离
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
public class Catalog extends SimpleJdbcDaoSupport {
public List<Item> getItems() {
String query = "select name, price from item";
return getSimpleJdbcTemplate().query(query, new ParameterizedRowMapper<Item>() {
public Item mapRow(ResultSet rs, int row) throws SQLException {
String name = rs.getString(1);
double price = rs.getDouble(2);
return new Item(name, price);
}
});
}
}
public class Item {
private String name;
private double price;
public Item(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public String toString() {
return name + " (" + price + ")";
}
}
<bean id="parentDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
abstract="true">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="test"/>
</bean>
<!-- 主库 -->
<bean id="masterDataSource" parent="parentDataSource">
<property name="url" value="jdbc:mysql://localhost:${db.port.master}/blog"/>
</bean>
<!-- 从库 -->
<bean id="slaveDataSource" parent="parentDataSource">
<property name="url" value="jdbc:mysql://localhost:${db.port.slave}/blog"/>
</bean>
<!-- 用PropertyPlaceholderConfigurer来读取properties配置,此处省略配置 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/blog/datasource/db.properties"/>
</bean>
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class CustomerRoutingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return CustomerContextHolder.getCustomerType();
}
}
public class CustomerContextHolder {
private static final ThreadLocal<String> contextHolder =
new ThreadLocal<String>();
public static void setCustomerType(String customerType) {
Assert.notNull(customerType, "customerType cannot be null");
contextHolder.set(customerType);
}
public static String getCustomerType() {
return (String) contextHolder.get();
}
public static void clearCustomerType() {
contextHolder.remove();
}
}
<bean id="catalog" class="blog.datasource.Catalog">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="blog.datasource.CustomerRoutingDataSource">
<!-- 这个targetDataSource是必须要注入的 -->
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="MASTER" value-ref="masterDataSource"/>
<entry key="SLAVE" value-ref="slaveDataSource"/>
</map>
</property>
<!-- 默认的数据源 -->
<property name="defaultTargetDataSource" ref="masterDataSource"/>
</bean>
AbstractDependencyInjectionSpringContextTests {
private Catalog catalog;
public void setCatalog(Catalog catalog) {
this.catalog = catalog;
}
public void testDataSourceRouting() {
CustomerContextHolder.setCustomerType(“MASTER”);
List<Item> goldItems = catalog.getItems();
assertEquals(3, goldItems.size());
System.out.println("gold items: " + goldItems);
CustomerContextHolder.setCustomerType(“SLAVE”);
List<Item> silverItems = catalog.getItems();
assertEquals(2, silverItems.size());
System.out.println("silver items: " + silverItems);
CustomerContextHolder.clearCustomerType();
List<Item> bronzeItems = catalog.getItems();
assertEquals(1, bronzeItems.size());
System.out.println("bronze items: " + bronzeItems);
}
protected String[] getConfigLocations() {
return new String[] {"/blog/datasource/beans.xml"};
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。