Loading example-common/repository-mybatis/src/main/java/io/shardingsphere/example/common/mybatis/repository/MybatisCountryRepository.java 0 → 100644 +25 −0 Original line number Diff line number Diff line /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.shardingsphere.example.common.mybatis.repository; import io.shardingsphere.example.common.repository.CountryRepository; import org.apache.ibatis.annotations.Mapper; @Mapper public interface MybatisCountryRepository extends CountryRepository { } example-common/repository-mybatis/src/main/java/io/shardingsphere/example/common/mybatis/service/SpringPojoServiceImpl.java +69 −5 Original line number Diff line number Diff line Loading @@ -17,8 +17,10 @@ package io.shardingsphere.example.common.mybatis.service; import io.shardingsphere.example.common.entity.Country; import io.shardingsphere.example.common.entity.Order; import io.shardingsphere.example.common.entity.OrderItem; import io.shardingsphere.example.common.repository.CountryRepository; import io.shardingsphere.example.common.repository.OrderItemRepository; import io.shardingsphere.example.common.repository.OrderRepository; import org.springframework.stereotype.Service; Loading @@ -27,6 +29,7 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; import java.util.Locale; @Service public class SpringPojoServiceImpl implements SpringPojoService { Loading @@ -37,27 +40,33 @@ public class SpringPojoServiceImpl implements SpringPojoService { @Resource private OrderItemRepository orderItemRepository; @Resource private CountryRepository countryRepository; @Override public void initEnvironment() { orderRepository.createTableIfNotExists(); orderItemRepository.createTableIfNotExists(); countryRepository.createTableIfNotExists(); orderRepository.truncateTable(); orderItemRepository.truncateTable(); countryRepository.truncateTable(); } @Override public void cleanEnvironment() { orderRepository.dropTable(); orderItemRepository.dropTable(); countryRepository.dropTable(); } @Override @Transactional public void processSuccess() { System.out.println("-------------- Process Success Begin ---------------"); List<Long> orderIds = insertData(); InsertResult insertResult = insertData(); printData(); deleteData(orderIds); deleteData(insertResult.getOrderIds(),insertResult.getCountryIds()); printData(); System.out.println("-------------- Process Success Finish --------------"); } Loading @@ -71,8 +80,14 @@ public class SpringPojoServiceImpl implements SpringPojoService { throw new RuntimeException("Exception occur for transaction test."); } private List<Long> insertData() { private InsertResult insertData(){ System.out.println("---------------------------- Insert Data ----------------------------"); List<Long> orderIds = insertOrderData(); List<Long> countryIds = insertCountryData(); return new InsertResult(orderIds,countryIds); } private List<Long> insertOrderData() { List<Long> result = new ArrayList<>(10); for (int i = 1; i <= 10; i++) { Order order = new Order(); Loading @@ -89,12 +104,37 @@ public class SpringPojoServiceImpl implements SpringPojoService { return result; } private void deleteData(final List<Long> orderIds) { private List<Long> insertCountryData(){ List<Long> result = new ArrayList<>(); Locale[] locales = Locale.getAvailableLocales(); int i = 0; for(Locale l:locales){ final String country = l.getCountry(); if(country == null || "".equals(country)){ continue; } Country currCountry = new Country(); currCountry.setName(l.getDisplayCountry(l)); currCountry.setLanguage(l.getLanguage()); currCountry.setCode(l.getCountry()); countryRepository.insert(currCountry); result.add(currCountry.getId()); if(++i == 10){ break; } } return result; } private void deleteData(final List<Long> orderIds,final List<Long> countryIds) { System.out.println("---------------------------- Delete Data ----------------------------"); for (Long each : orderIds) { orderRepository.delete(each); orderItemRepository.delete(each); } for (Long each: countryIds){ countryRepository.delete(each); } } @Override Loading @@ -107,5 +147,29 @@ public class SpringPojoServiceImpl implements SpringPojoService { for (Object each : orderItemRepository.selectAll()) { System.out.println(each); } System.out.println("---------------------------- Print Country Data -------------------"); for (Object each : countryRepository.selectAll()) { System.out.println(each); } } private static class InsertResult{ private List<Long> orderIds; private List<Long> countryIds; public InsertResult(List<Long> orderIds, List<Long> countryIds) { this.orderIds = orderIds; this.countryIds = countryIds; } public List<Long> getOrderIds() { return orderIds; } public List<Long> getCountryIds() { return countryIds; } } } example-common/repository-mybatis/src/main/resources/META-INF/mappers/CountryMapper.xml 0 → 100644 +37 −0 Original line number Diff line number Diff line <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="io.shardingsphere.example.common.mybatis.repository.MybatisCountryRepository"> <resultMap id="baseResultMap" type="io.shardingsphere.example.common.entity.Country"> <result column="id" property="id" jdbcType="INTEGER"/> <result column="name" property="name" jdbcType="VARCHAR"/> <result column="code" property="code" jdbcType="VARCHAR"/> <result column="language" property="language" jdbcType="VARCHAR"/> </resultMap> <update id="createTableIfNotExists"> CREATE TABLE IF NOT EXISTS t_country (id BIGINT NOT NULL AUTO_INCREMENT, name VARCHAR(50),code VARCHAR(50), language VARCHAR(50), PRIMARY KEY (id)); </update> <update id="truncateTable"> TRUNCATE TABLE t_country; </update> <update id="dropTable"> DROP TABLE IF EXISTS t_country; </update> <insert id="insert" useGeneratedKeys="true" keyProperty="id"> <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" > SELECT @@IDENTITY </selectKey> INSERT INTO t_country (name, code, language) VALUES (#{name,jdbcType=VARCHAR},#{code,jdbcType=VARCHAR},#{language,jdbcType=VARCHAR}) </insert> <delete id="delete"> DELETE FROM t_country WHERE id = #{id,jdbcType=INTEGER}; </delete> <select id="selectAll" resultMap="baseResultMap"> SELECT * FROM t_country; </select> </mapper> sharding-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application-master-slave.properties +3 −3 Original line number Diff line number Diff line Loading @@ -2,19 +2,19 @@ sharding.jdbc.datasource.names=ds_master,ds_slave_0,ds_slave_1 sharding.jdbc.datasource.ds_master.type=com.zaxxer.hikari.HikariDataSource sharding.jdbc.datasource.ds_master.driver-class-name=com.mysql.jdbc.Driver sharding.jdbc.datasource.ds_master.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_master?serverTimezone=UTC&useSSL=false sharding.jdbc.datasource.ds_master.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_master?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8 sharding.jdbc.datasource.ds_master.username=root sharding.jdbc.datasource.ds_master.password= sharding.jdbc.datasource.ds_slave_0.type=com.zaxxer.hikari.HikariDataSource sharding.jdbc.datasource.ds_slave_0.driver-class-name=com.mysql.jdbc.Driver sharding.jdbc.datasource.ds_slave_0.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_slave_0?serverTimezone=UTC&useSSL=false sharding.jdbc.datasource.ds_slave_0.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_slave_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8 sharding.jdbc.datasource.ds_slave_0.username=root sharding.jdbc.datasource.ds_slave_0.password= sharding.jdbc.datasource.ds_slave_1.type=com.zaxxer.hikari.HikariDataSource sharding.jdbc.datasource.ds_slave_1.driver-class-name=com.mysql.jdbc.Driver sharding.jdbc.datasource.ds_slave_1.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_slave_1?serverTimezone=UTC&useSSL=false sharding.jdbc.datasource.ds_slave_1.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_slave_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8 sharding.jdbc.datasource.ds_slave_1.username=root sharding.jdbc.datasource.ds_slave_1.password= Loading sharding-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application.properties +2 −2 Original line number Diff line number Diff line Loading @@ -2,8 +2,8 @@ spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect spring.jpa.properties.hibernate.show_sql=false spring.profiles.active=sharding-databases #spring.profiles.active=sharding-databases #spring.profiles.active=sharding-tables #spring.profiles.active=sharding-databases-tables #spring.profiles.active=master-slave #spring.profiles.active=sharding-master-slave spring.profiles.active=sharding-master-slave Loading
example-common/repository-mybatis/src/main/java/io/shardingsphere/example/common/mybatis/repository/MybatisCountryRepository.java 0 → 100644 +25 −0 Original line number Diff line number Diff line /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.shardingsphere.example.common.mybatis.repository; import io.shardingsphere.example.common.repository.CountryRepository; import org.apache.ibatis.annotations.Mapper; @Mapper public interface MybatisCountryRepository extends CountryRepository { }
example-common/repository-mybatis/src/main/java/io/shardingsphere/example/common/mybatis/service/SpringPojoServiceImpl.java +69 −5 Original line number Diff line number Diff line Loading @@ -17,8 +17,10 @@ package io.shardingsphere.example.common.mybatis.service; import io.shardingsphere.example.common.entity.Country; import io.shardingsphere.example.common.entity.Order; import io.shardingsphere.example.common.entity.OrderItem; import io.shardingsphere.example.common.repository.CountryRepository; import io.shardingsphere.example.common.repository.OrderItemRepository; import io.shardingsphere.example.common.repository.OrderRepository; import org.springframework.stereotype.Service; Loading @@ -27,6 +29,7 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; import java.util.Locale; @Service public class SpringPojoServiceImpl implements SpringPojoService { Loading @@ -37,27 +40,33 @@ public class SpringPojoServiceImpl implements SpringPojoService { @Resource private OrderItemRepository orderItemRepository; @Resource private CountryRepository countryRepository; @Override public void initEnvironment() { orderRepository.createTableIfNotExists(); orderItemRepository.createTableIfNotExists(); countryRepository.createTableIfNotExists(); orderRepository.truncateTable(); orderItemRepository.truncateTable(); countryRepository.truncateTable(); } @Override public void cleanEnvironment() { orderRepository.dropTable(); orderItemRepository.dropTable(); countryRepository.dropTable(); } @Override @Transactional public void processSuccess() { System.out.println("-------------- Process Success Begin ---------------"); List<Long> orderIds = insertData(); InsertResult insertResult = insertData(); printData(); deleteData(orderIds); deleteData(insertResult.getOrderIds(),insertResult.getCountryIds()); printData(); System.out.println("-------------- Process Success Finish --------------"); } Loading @@ -71,8 +80,14 @@ public class SpringPojoServiceImpl implements SpringPojoService { throw new RuntimeException("Exception occur for transaction test."); } private List<Long> insertData() { private InsertResult insertData(){ System.out.println("---------------------------- Insert Data ----------------------------"); List<Long> orderIds = insertOrderData(); List<Long> countryIds = insertCountryData(); return new InsertResult(orderIds,countryIds); } private List<Long> insertOrderData() { List<Long> result = new ArrayList<>(10); for (int i = 1; i <= 10; i++) { Order order = new Order(); Loading @@ -89,12 +104,37 @@ public class SpringPojoServiceImpl implements SpringPojoService { return result; } private void deleteData(final List<Long> orderIds) { private List<Long> insertCountryData(){ List<Long> result = new ArrayList<>(); Locale[] locales = Locale.getAvailableLocales(); int i = 0; for(Locale l:locales){ final String country = l.getCountry(); if(country == null || "".equals(country)){ continue; } Country currCountry = new Country(); currCountry.setName(l.getDisplayCountry(l)); currCountry.setLanguage(l.getLanguage()); currCountry.setCode(l.getCountry()); countryRepository.insert(currCountry); result.add(currCountry.getId()); if(++i == 10){ break; } } return result; } private void deleteData(final List<Long> orderIds,final List<Long> countryIds) { System.out.println("---------------------------- Delete Data ----------------------------"); for (Long each : orderIds) { orderRepository.delete(each); orderItemRepository.delete(each); } for (Long each: countryIds){ countryRepository.delete(each); } } @Override Loading @@ -107,5 +147,29 @@ public class SpringPojoServiceImpl implements SpringPojoService { for (Object each : orderItemRepository.selectAll()) { System.out.println(each); } System.out.println("---------------------------- Print Country Data -------------------"); for (Object each : countryRepository.selectAll()) { System.out.println(each); } } private static class InsertResult{ private List<Long> orderIds; private List<Long> countryIds; public InsertResult(List<Long> orderIds, List<Long> countryIds) { this.orderIds = orderIds; this.countryIds = countryIds; } public List<Long> getOrderIds() { return orderIds; } public List<Long> getCountryIds() { return countryIds; } } }
example-common/repository-mybatis/src/main/resources/META-INF/mappers/CountryMapper.xml 0 → 100644 +37 −0 Original line number Diff line number Diff line <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="io.shardingsphere.example.common.mybatis.repository.MybatisCountryRepository"> <resultMap id="baseResultMap" type="io.shardingsphere.example.common.entity.Country"> <result column="id" property="id" jdbcType="INTEGER"/> <result column="name" property="name" jdbcType="VARCHAR"/> <result column="code" property="code" jdbcType="VARCHAR"/> <result column="language" property="language" jdbcType="VARCHAR"/> </resultMap> <update id="createTableIfNotExists"> CREATE TABLE IF NOT EXISTS t_country (id BIGINT NOT NULL AUTO_INCREMENT, name VARCHAR(50),code VARCHAR(50), language VARCHAR(50), PRIMARY KEY (id)); </update> <update id="truncateTable"> TRUNCATE TABLE t_country; </update> <update id="dropTable"> DROP TABLE IF EXISTS t_country; </update> <insert id="insert" useGeneratedKeys="true" keyProperty="id"> <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" > SELECT @@IDENTITY </selectKey> INSERT INTO t_country (name, code, language) VALUES (#{name,jdbcType=VARCHAR},#{code,jdbcType=VARCHAR},#{language,jdbcType=VARCHAR}) </insert> <delete id="delete"> DELETE FROM t_country WHERE id = #{id,jdbcType=INTEGER}; </delete> <select id="selectAll" resultMap="baseResultMap"> SELECT * FROM t_country; </select> </mapper>
sharding-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application-master-slave.properties +3 −3 Original line number Diff line number Diff line Loading @@ -2,19 +2,19 @@ sharding.jdbc.datasource.names=ds_master,ds_slave_0,ds_slave_1 sharding.jdbc.datasource.ds_master.type=com.zaxxer.hikari.HikariDataSource sharding.jdbc.datasource.ds_master.driver-class-name=com.mysql.jdbc.Driver sharding.jdbc.datasource.ds_master.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_master?serverTimezone=UTC&useSSL=false sharding.jdbc.datasource.ds_master.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_master?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8 sharding.jdbc.datasource.ds_master.username=root sharding.jdbc.datasource.ds_master.password= sharding.jdbc.datasource.ds_slave_0.type=com.zaxxer.hikari.HikariDataSource sharding.jdbc.datasource.ds_slave_0.driver-class-name=com.mysql.jdbc.Driver sharding.jdbc.datasource.ds_slave_0.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_slave_0?serverTimezone=UTC&useSSL=false sharding.jdbc.datasource.ds_slave_0.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_slave_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8 sharding.jdbc.datasource.ds_slave_0.username=root sharding.jdbc.datasource.ds_slave_0.password= sharding.jdbc.datasource.ds_slave_1.type=com.zaxxer.hikari.HikariDataSource sharding.jdbc.datasource.ds_slave_1.driver-class-name=com.mysql.jdbc.Driver sharding.jdbc.datasource.ds_slave_1.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_slave_1?serverTimezone=UTC&useSSL=false sharding.jdbc.datasource.ds_slave_1.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_slave_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8 sharding.jdbc.datasource.ds_slave_1.username=root sharding.jdbc.datasource.ds_slave_1.password= Loading
sharding-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application.properties +2 −2 Original line number Diff line number Diff line Loading @@ -2,8 +2,8 @@ spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect spring.jpa.properties.hibernate.show_sql=false spring.profiles.active=sharding-databases #spring.profiles.active=sharding-databases #spring.profiles.active=sharding-tables #spring.profiles.active=sharding-databases-tables #spring.profiles.active=master-slave #spring.profiles.active=sharding-master-slave spring.profiles.active=sharding-master-slave