diff --git a/pom.xml b/pom.xml index 2d68144a47..a1b9e3a57f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.springframework.data spring-data-jdbc - 1.0.0.BUILD-SNAPSHOT + 1.0.0.DATAJDBC-157-SNAPSHOT Spring Data JDBC Spring Data module for JDBC repositories. diff --git a/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java b/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java index d9e041bfca..ab1de4ee3b 100644 --- a/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java +++ b/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 the original author or authors. + * Copyright 2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,17 +25,12 @@ import org.springframework.data.mapping.PropertyPath; /** - * {@link DataAccessStrategy} implementation based on MyBatis. - * - * Each method gets mapped to a statement. The name of the statement gets constructed as follows: - * - * The namespace is based on the class of the entity plus the suffix "Mapper". This is then followed by the method name separated by a dot. - * - * For methods taking a {@link PropertyPath} as argument, the relevant entity is that of the root of the path, and the path itself gets as dot separated String appended to the statement name. - * - * Each statement gets an instance of {@link MyBatisContext}, which at least has the entityType set. - * - * For methods taking a {@link PropertyPath} the entityTyoe if the context is set to the class of the leaf type. + * {@link DataAccessStrategy} implementation based on MyBatis. Each method gets mapped to a statement. The name of the + * statement gets constructed as follows: The namespace is based on the class of the entity plus the suffix "Mapper". + * This is then followed by the method name separated by a dot. For methods taking a {@link PropertyPath} as argument, + * the relevant entity is that of the root of the path, and the path itself gets as dot separated String appended to the + * statement name. Each statement gets an instance of {@link MyBatisContext}, which at least has the entityType set. For + * methods taking a {@link PropertyPath} the entityTyoe if the context is set to the class of the leaf type. * * @author Jens Schauder */ @@ -73,7 +68,7 @@ public void delete(Object id, Class domainType) { @Override public void delete(Object rootId, PropertyPath propertyPath) { - sqlSession().delete(mapper(propertyPath.getOwningType().getType()) + ".delete." + propertyPath.toDotPath(), + sqlSession().delete(mapper(propertyPath.getOwningType().getType()) + ".delete-" + toDashPath(propertyPath), new MyBatisContext(rootId, null, propertyPath.getLeafProperty().getTypeInformation().getType(), Collections.emptyMap())); } @@ -94,7 +89,7 @@ public void deleteAll(PropertyPath propertyPath) { Class leaveType = propertyPath.getLeafProperty().getTypeInformation().getType(); sqlSession().delete( // - mapper(baseType) + ".deleteAll." + propertyPath.toDotPath(), // + mapper(baseType) + ".deleteAll-" + toDashPath(propertyPath), // new MyBatisContext(null, null, leaveType, Collections.emptyMap()) // ); } @@ -119,7 +114,7 @@ public Iterable findAllById(Iterable ids, Class domainType) { @Override public Iterable findAllByProperty(Object rootId, JdbcPersistentProperty property) { - return sqlSession().selectList(mapper(property.getOwner().getType()) + ".findAllByProperty." + property.getName(), + return sqlSession().selectList(mapper(property.getOwner().getType()) + ".findAllByProperty-" + property.getName(), new MyBatisContext(rootId, null, property.getType(), Collections.emptyMap())); } @@ -131,7 +126,8 @@ public boolean existsById(Object id, Class domainType) { @Override public long count(Class domainType) { - return sqlSession().selectOne(mapper(domainType) + ".count"); + return sqlSession().selectOne(mapper(domainType) + ".count", + new MyBatisContext(null, null, domainType, Collections.emptyMap())); } private String mapper(Class domainType) { @@ -141,4 +137,8 @@ private String mapper(Class domainType) { private SqlSession sqlSession() { return sqlSessionFactory.openSession(); } + + private String toDashPath(PropertyPath propertyPath) { + return propertyPath.toDotPath().replaceAll("\\.", "-"); + } } diff --git a/src/test/java/org/springframework/data/jdbc/core/MyBatisDataAccessStrategyUnitTests.java b/src/test/java/org/springframework/data/jdbc/core/MyBatisDataAccessStrategyUnitTests.java index b169b13ee0..0b8534432f 100644 --- a/src/test/java/org/springframework/data/jdbc/core/MyBatisDataAccessStrategyUnitTests.java +++ b/src/test/java/org/springframework/data/jdbc/core/MyBatisDataAccessStrategyUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 the original author or authors. + * Copyright 2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -123,7 +123,7 @@ public void deleteAllByPath() { accessStrategy.deleteAll(PropertyPath.from("class.name.bytes", String.class)); - verify(session).delete(eq("java.lang.StringMapper.deleteAll.class.name.bytes"), captor.capture()); + verify(session).delete(eq("java.lang.StringMapper.deleteAll-class-name-bytes"), captor.capture()); assertThat(captor.getValue()) // .isNotNull() // @@ -167,7 +167,7 @@ public void deleteByPath() { accessStrategy.delete("rootid", PropertyPath.from("class.name.bytes", String.class)); - verify(session).delete(eq("java.lang.StringMapper.delete.class.name.bytes"), captor.capture()); + verify(session).delete(eq("java.lang.StringMapper.delete-class-name-bytes"), captor.capture()); assertThat(captor.getValue()) // .isNotNull() // @@ -248,6 +248,7 @@ public void findAllById() { ); } + @SuppressWarnings("unchecked") @Test // DATAJDBC-123 public void findAllByProperty() { @@ -259,7 +260,7 @@ public void findAllByProperty() { accessStrategy.findAllByProperty("id", property); - verify(session).selectList(eq("java.lang.StringMapper.findAllByProperty.propertyName"), captor.capture()); + verify(session).selectList(eq("java.lang.StringMapper.findAllByProperty-propertyName"), captor.capture()); assertThat(captor.getValue()) // .isNotNull() // @@ -298,4 +299,29 @@ public void existsById() { ); } + @Test // DATAJDBC-157 + public void count() { + + doReturn(0L).when(session).selectOne(anyString(), any()); + + accessStrategy.count(String.class); + + + verify(session).selectOne(eq("java.lang.StringMapper.count"), captor.capture()); + + assertThat(captor.getValue()) // + .isNotNull() // + .extracting( // + MyBatisContext::getInstance, // + MyBatisContext::getId, // + MyBatisContext::getDomainType, // + c -> c.get("key") // + ).containsExactly( // + null, // + null, // + String.class, // + null // + ); + } + }