Loading example-common/repository-api/src/main/java/org/apache/shardingsphere/example/common/repository/CountryRepository.java +1 −1 Original line number Diff line number Diff line Loading @@ -19,5 +19,5 @@ package org.apache.shardingsphere.example.common.repository; import org.apache.shardingsphere.example.common.entity.Country; public interface CountryRepository extends CommonRepository<Country> { public interface CountryRepository extends CommonRepository<Country,String> { } example-common/repository-jdbc/src/main/java/org/apache/shardingsphere/example/common/jdbc/repository/CountryRepositroyImpl.java +5 −5 Original line number Diff line number Diff line Loading @@ -68,7 +68,7 @@ public class CountryRepositroyImpl implements CountryRepository { } @Override public Long insert(final Country country) { public String insert(final Country country) { String sql = "INSERT INTO t_country (name, code, language) VALUES (?, ?, ?)"; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { Loading @@ -82,15 +82,15 @@ public class CountryRepositroyImpl implements CountryRepository { } } catch (final SQLException ignored) { } return country.getId(); return country.getCode(); } @Override public void delete(final Long id) { String sql = "DELETE FROM t_country WHERE id =?"; public void delete(final String code) { String sql = "DELETE FROM t_country WHERE code =?"; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { preparedStatement.setLong(1, id); preparedStatement.setString(1, code); preparedStatement.executeUpdate(); } catch (final SQLException ignored) { } Loading example-common/repository-jdbc/src/main/java/org/apache/shardingsphere/example/common/jdbc/service/CommonServiceImpl.java +4 −73 Original line number Diff line number Diff line Loading @@ -17,17 +17,14 @@ package org.apache.shardingsphere.example.common.jdbc.service; import org.apache.shardingsphere.example.common.entity.Country; import org.apache.shardingsphere.example.common.entity.Order; import org.apache.shardingsphere.example.common.entity.OrderItem; import org.apache.shardingsphere.example.common.repository.CountryRepository; import org.apache.shardingsphere.example.common.repository.OrderItemRepository; import org.apache.shardingsphere.example.common.repository.OrderRepository; import org.apache.shardingsphere.example.common.service.CommonService; import java.util.ArrayList; import java.util.List; import java.util.Locale; public final class CommonServiceImpl implements CommonService { Loading @@ -35,14 +32,6 @@ public final class CommonServiceImpl implements CommonService { private OrderItemRepository orderItemRepository; private CountryRepository countryRepository; public CommonServiceImpl(final OrderRepository orderRepository, final OrderItemRepository orderItemRepository, final CountryRepository countryRepository) { this.orderRepository = orderRepository; this.orderItemRepository = orderItemRepository; this.countryRepository = countryRepository; } public CommonServiceImpl(final OrderRepository orderRepository, final OrderItemRepository orderItemRepository) { this.orderRepository = orderRepository; this.orderItemRepository = orderItemRepository; Loading @@ -52,25 +41,22 @@ public final class CommonServiceImpl implements CommonService { 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 public void processSuccess() { System.out.println("-------------- Process Success Begin ---------------"); InsertResult insertResult = insertData(); List<Long> orderIds = insertData(); printData(); deleteData(insertResult.getOrderIds(), insertResult.getCountryIds()); deleteData(orderIds); printData(); System.out.println("-------------- Process Success Finish --------------"); } Loading @@ -83,14 +69,8 @@ public final class CommonServiceImpl implements CommonService { throw new RuntimeException("Exception occur for transaction test."); } private InsertResult insertData() { private List<Long> 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 @@ -107,37 +87,12 @@ public final class CommonServiceImpl implements CommonService { return result; } 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) { private void deleteData(final List<Long> orderIds) { System.out.println("---------------------------- Delete Data ----------------------------"); for (Long each : orderIds) { orderRepository.delete(each); orderItemRepository.delete(each); } for (Long each: countryIds) { countryRepository.delete(each); } } @Override Loading @@ -150,29 +105,5 @@ public final class CommonServiceImpl implements CommonService { 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; InsertResult(final List<Long> orderIds, final List<Long> countryIds) { this.orderIds = orderIds; this.countryIds = countryIds; } public List<Long> getOrderIds() { return orderIds; } public List<Long> getCountryIds() { return countryIds; } } } example-common/repository-jdbc/src/main/java/org/apache/shardingsphere/example/common/jdbc/service/CountryServiceImpl.java 0 → 100644 +102 −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 org.apache.shardingsphere.example.common.jdbc.service; import org.apache.shardingsphere.example.common.entity.Country; import org.apache.shardingsphere.example.common.repository.CountryRepository; import org.apache.shardingsphere.example.common.service.CommonService; import java.util.ArrayList; import java.util.List; import java.util.Locale; public class CountryServiceImpl implements CommonService { private final CountryRepository countryRepository; public CountryServiceImpl(CountryRepository countryRepository) { this.countryRepository = countryRepository; } @Override public void initEnvironment() { countryRepository.createTableIfNotExists(); countryRepository.truncateTable(); } @Override public void cleanEnvironment() { countryRepository.dropTable(); } @Override public void processSuccess() { System.out.println("-------------- Process Success Begin ---------------"); List<String> countryCodes = insertData(); printData(); deleteData(countryCodes); printData(); System.out.println("-------------- Process Success Finish --------------"); } @Override public void processFailure() { System.out.println("-------------- Process Failure Begin ---------------"); insertData(); System.out.println("-------------- Process Failure Finish --------------"); throw new RuntimeException("Exception occur for transaction test."); } @Override public void printData() { System.out.println("---------------------------- Print Country Data -------------------"); for (Object each : countryRepository.selectAll()) { System.out.println(each); } } private void deleteData(final List<String> countryCodes) { System.out.println("---------------------------- Delete Data ----------------------------"); for (String each: countryCodes) { countryRepository.delete(each); } } private List<String> insertData() { System.out.println("---------------------------- Insert Data ----------------------------"); List<String> 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.getCode()); if (++i == 10) { break; } } return result; } } example-common/repository-jpa/src/main/java/org/apache/shardingsphere/example/common/jpa/entity/CountryEntity.java +3 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,8 @@ import org.apache.shardingsphere.example.common.entity.Country; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; Loading @@ -30,6 +32,7 @@ public class CountryEntity extends Country { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) @Override public long getId() { return super.getId(); Loading Loading
example-common/repository-api/src/main/java/org/apache/shardingsphere/example/common/repository/CountryRepository.java +1 −1 Original line number Diff line number Diff line Loading @@ -19,5 +19,5 @@ package org.apache.shardingsphere.example.common.repository; import org.apache.shardingsphere.example.common.entity.Country; public interface CountryRepository extends CommonRepository<Country> { public interface CountryRepository extends CommonRepository<Country,String> { }
example-common/repository-jdbc/src/main/java/org/apache/shardingsphere/example/common/jdbc/repository/CountryRepositroyImpl.java +5 −5 Original line number Diff line number Diff line Loading @@ -68,7 +68,7 @@ public class CountryRepositroyImpl implements CountryRepository { } @Override public Long insert(final Country country) { public String insert(final Country country) { String sql = "INSERT INTO t_country (name, code, language) VALUES (?, ?, ?)"; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { Loading @@ -82,15 +82,15 @@ public class CountryRepositroyImpl implements CountryRepository { } } catch (final SQLException ignored) { } return country.getId(); return country.getCode(); } @Override public void delete(final Long id) { String sql = "DELETE FROM t_country WHERE id =?"; public void delete(final String code) { String sql = "DELETE FROM t_country WHERE code =?"; try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { preparedStatement.setLong(1, id); preparedStatement.setString(1, code); preparedStatement.executeUpdate(); } catch (final SQLException ignored) { } Loading
example-common/repository-jdbc/src/main/java/org/apache/shardingsphere/example/common/jdbc/service/CommonServiceImpl.java +4 −73 Original line number Diff line number Diff line Loading @@ -17,17 +17,14 @@ package org.apache.shardingsphere.example.common.jdbc.service; import org.apache.shardingsphere.example.common.entity.Country; import org.apache.shardingsphere.example.common.entity.Order; import org.apache.shardingsphere.example.common.entity.OrderItem; import org.apache.shardingsphere.example.common.repository.CountryRepository; import org.apache.shardingsphere.example.common.repository.OrderItemRepository; import org.apache.shardingsphere.example.common.repository.OrderRepository; import org.apache.shardingsphere.example.common.service.CommonService; import java.util.ArrayList; import java.util.List; import java.util.Locale; public final class CommonServiceImpl implements CommonService { Loading @@ -35,14 +32,6 @@ public final class CommonServiceImpl implements CommonService { private OrderItemRepository orderItemRepository; private CountryRepository countryRepository; public CommonServiceImpl(final OrderRepository orderRepository, final OrderItemRepository orderItemRepository, final CountryRepository countryRepository) { this.orderRepository = orderRepository; this.orderItemRepository = orderItemRepository; this.countryRepository = countryRepository; } public CommonServiceImpl(final OrderRepository orderRepository, final OrderItemRepository orderItemRepository) { this.orderRepository = orderRepository; this.orderItemRepository = orderItemRepository; Loading @@ -52,25 +41,22 @@ public final class CommonServiceImpl implements CommonService { 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 public void processSuccess() { System.out.println("-------------- Process Success Begin ---------------"); InsertResult insertResult = insertData(); List<Long> orderIds = insertData(); printData(); deleteData(insertResult.getOrderIds(), insertResult.getCountryIds()); deleteData(orderIds); printData(); System.out.println("-------------- Process Success Finish --------------"); } Loading @@ -83,14 +69,8 @@ public final class CommonServiceImpl implements CommonService { throw new RuntimeException("Exception occur for transaction test."); } private InsertResult insertData() { private List<Long> 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 @@ -107,37 +87,12 @@ public final class CommonServiceImpl implements CommonService { return result; } 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) { private void deleteData(final List<Long> orderIds) { System.out.println("---------------------------- Delete Data ----------------------------"); for (Long each : orderIds) { orderRepository.delete(each); orderItemRepository.delete(each); } for (Long each: countryIds) { countryRepository.delete(each); } } @Override Loading @@ -150,29 +105,5 @@ public final class CommonServiceImpl implements CommonService { 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; InsertResult(final List<Long> orderIds, final List<Long> countryIds) { this.orderIds = orderIds; this.countryIds = countryIds; } public List<Long> getOrderIds() { return orderIds; } public List<Long> getCountryIds() { return countryIds; } } }
example-common/repository-jdbc/src/main/java/org/apache/shardingsphere/example/common/jdbc/service/CountryServiceImpl.java 0 → 100644 +102 −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 org.apache.shardingsphere.example.common.jdbc.service; import org.apache.shardingsphere.example.common.entity.Country; import org.apache.shardingsphere.example.common.repository.CountryRepository; import org.apache.shardingsphere.example.common.service.CommonService; import java.util.ArrayList; import java.util.List; import java.util.Locale; public class CountryServiceImpl implements CommonService { private final CountryRepository countryRepository; public CountryServiceImpl(CountryRepository countryRepository) { this.countryRepository = countryRepository; } @Override public void initEnvironment() { countryRepository.createTableIfNotExists(); countryRepository.truncateTable(); } @Override public void cleanEnvironment() { countryRepository.dropTable(); } @Override public void processSuccess() { System.out.println("-------------- Process Success Begin ---------------"); List<String> countryCodes = insertData(); printData(); deleteData(countryCodes); printData(); System.out.println("-------------- Process Success Finish --------------"); } @Override public void processFailure() { System.out.println("-------------- Process Failure Begin ---------------"); insertData(); System.out.println("-------------- Process Failure Finish --------------"); throw new RuntimeException("Exception occur for transaction test."); } @Override public void printData() { System.out.println("---------------------------- Print Country Data -------------------"); for (Object each : countryRepository.selectAll()) { System.out.println(each); } } private void deleteData(final List<String> countryCodes) { System.out.println("---------------------------- Delete Data ----------------------------"); for (String each: countryCodes) { countryRepository.delete(each); } } private List<String> insertData() { System.out.println("---------------------------- Insert Data ----------------------------"); List<String> 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.getCode()); if (++i == 10) { break; } } return result; } }
example-common/repository-jpa/src/main/java/org/apache/shardingsphere/example/common/jpa/entity/CountryEntity.java +3 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,8 @@ import org.apache.shardingsphere.example.common.entity.Country; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; Loading @@ -30,6 +32,7 @@ public class CountryEntity extends Country { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) @Override public long getId() { return super.getId(); Loading