提交 2dfa55fc 编写于 作者: H haocao

Refactor sharding-jdbc-spring examples 7th.

上级 f2d81ab2
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="log.context.name" value="sharding-jdbc-example-jdbc" />
<property name="log.context.name" value="sharding-jdbc-raw-jdbc-java-example" />
<property name="log.charset" value="UTF-8" />
<property name="log.pattern" value="[%-5level] %date --%thread-- [%logger] %msg %n" />
......
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="log.context.name" value="sharding-jdbc-example-config-yaml" />
<property name="log.context.name" value="sharding-jdbc-raw-jdbc-yaml-example" />
<property name="log.charset" value="UTF-8" />
<property name="log.pattern" value="[%-5level] %date --%thread-- [%logger] %msg %n" />
......
......@@ -17,32 +17,17 @@
package io.shardingjdbc.example.jpa;
import io.shardingjdbc.example.jpa.entity.Order;
import io.shardingjdbc.example.jpa.repository.OrderRepository;
import io.shardingjdbc.example.jpa.service.OrderService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.ArrayList;
import java.util.List;
public final class Main {
public final class SpringJpaShardingDatabaseAndTableMain {
// CHECKSTYLE:OFF
public static void main(final String[] args) {
// CHECKSTYLE:ON
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/jpa/mysql/jpaContext.xml");
OrderRepository orderRepository = applicationContext.getBean(OrderRepository.class);
List<Long> orderIds = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
Order order = new Order();
order.setUserId(51);
order.setStatus("INSERT_TEST");
orderRepository.insert(order);
}
System.out.println(orderRepository.selectAll());
System.out.println("--------------");
for (Long each : orderIds) {
orderRepository.delete(each);
}
applicationContext.close();
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/jpa/jpaShardingDatabaseAndTable.xml");
OrderService orderService = applicationContext.getBean(OrderService.class);
orderService.testAll();
}
}
......@@ -23,9 +23,9 @@ import java.util.List;
public interface OrderRepository {
void insert(Order order);
Long insert(Order order);
void delete(long orderId);
void delete(Long orderId);
List<Order> selectAll();
}
......@@ -34,12 +34,13 @@ public class OrderRepositoryImpl implements OrderRepository {
private EntityManager entityManager;
@Override
public void insert(final Order order) {
public Long insert(final Order order) {
entityManager.persist(order);
return order.getOrderId();
}
@Override
public void delete(final long orderId) {
public void delete(final Long orderId) {
Query query = entityManager.createQuery("DELETE FROM Order o WHERE o.orderId = ?1 AND o.userId = 51");
query.setParameter(1, orderId);
query.executeUpdate();
......
package io.shardingjdbc.example.jpa.service;
import io.shardingjdbc.example.jpa.entity.Order;
import io.shardingjdbc.example.jpa.repository.OrderRepository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class OrderService {
@Resource
private OrderRepository orderRepository;
public void testAll() {
List<Long> orderIds = new ArrayList<>(10);
System.out.println("1.Insert--------------");
for (int i = 0; i < 10; i++) {
Order order = new Order();
order.setUserId(51);
order.setStatus("INSERT_TEST");
orderIds.add(orderRepository.insert(order));
}
System.out.println(orderRepository.selectAll());
System.out.println("2.Delete--------------");
for (Long each : orderIds) {
orderRepository.delete(each);
}
System.out.println(orderRepository.selectAll());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:sharding="http://shardingjdbc.io/schema/shardingjdbc/sharding"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://shardingjdbc.io/schema/shardingjdbc/sharding
http://shardingjdbc.io/schema/shardingjdbc/sharding/sharding.xsd">
http://shardingjdbc.io/schema/shardingjdbc/sharding/sharding.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config />
<context:component-scan base-package="io.shardingjdbc.example.jpa"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="shardingDataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:database="MYSQL" />
</property>
<property name="packagesToScan" value="io.shardingjdbc.example.jpa.entity" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory" />
<tx:annotation-driven/>
<bean id="demo_ds_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/demo_ds_0"/>
......
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<import resource="shardingContext.xml"/>
<context:annotation-config />
<context:component-scan base-package="io.shardingjdbc.example.jpa"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="shardingDataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:database="MYSQL" />
</property>
<property name="packagesToScan" value="io.shardingjdbc.example.jpa.entity" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.max_fetch_depth">10</prop>
<prop key="hibernate.jdbc.fetch_size">100</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory" />
<tx:annotation-driven/>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="log.context.name" value="sharding-jdbc-example-jpa" />
<property name="log.context.name" value="sharding-jdbc-spring-namespace-jpa-example" />
<property name="log.charset" value="UTF-8" />
<property name="log.pattern" value="[%-5level] %date --%thread-- [%logger] %msg %n" />
......
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="log.context.name" value="sharding-jdbc-example-mybatis" />
<property name="log.context.name" value="sharding-jdbc-spring-namespace-mybatis-example" />
<property name="log.charset" value="UTF-8" />
<property name="log.pattern" value="[%-5level] %date --%thread-- [%logger] %msg %n" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册