jclagache / spring-data-mybatis

Simplifies the development of creating a MyBatis-based data access layer. http://www.springsource.org/spring-data
122 stars 32 forks source link

Spring-hateoas #10

Closed kopax closed 7 years ago

kopax commented 7 years ago

Thanks for updating the plugin on the latest version.

I am trying to make spring-data-rest, spring-hateoas to work with my mybatis.

It work fine with the example you provide, if I try to implement it using spring-data-rest, the plugin has some unwanted behavior for example:

I have a ManagerRepository.java :

public interface ManagerRepository extends PagingAndSortingRepository<Manager, Integer> {
}

I create this test controller to confirm it work:

    @RequestMapping("/manager")
    public ResponseEntity<Iterable<Manager>> dataSource(){
        Manager manager = managerRepository.findOne(1);
        Iterable<Manager> managerList = managerRepository.findAll();
        return new ResponseEntity<Iterable<Manager>>(managerList, HttpStatus.OK);
    }

and it work!:

dka@dev-01:[~]: curl http://localhost:8090/manager
[{"id":1,"login":"dka"},{"id":2,"login":"test"}]

Now I activate spring-data-rest by setting a base-path for my API, if I add the following to my application.yml :

spring:
  data:
    rest:
      base-path: /api

I'm expecting repositories to be exposed, I can verify it using curl :

curl localhost:8090/api
{
  "_links" : {
    "managers" : {
      "href" : "http://localhost:8090/api/managers{?page,size,sort}",
      "templated" : true
    },
    "profile" : {
      "href" : "http://localhost:8090/api/profile"
    }
  }
}

Great so far, but if I try to curl the managers link :

dka@dev-01:[~]: curl http://localhost:8090/api/managers
{"timestamp":1480919540647,"status":500,"error":"Internal Server Error","exception":"org.apache.ibatis.binding.BindingException","message":"Invalid bound statement (not found): com.repository.ManagerRepository.findAll","path":"/api/managers"

Same probleme with the managers profile : http://localhost:8090/api/profile/managers

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Dec 05 13:36:21 ICT 2016
There was an unexpected error (type=Internal Server Error, status=500).
Ambiguous search mapping detected. Both public abstract void org.springframework.data.repository.CrudRepository.delete(java.lang.Object) and public abstract void org.springframework.data.repository.CrudRepository.delete(java.io.Serializable) are mapped to /delete! Tweak configuration to get to unambiguous paths!

Is this a bug or me using it wrong ?

Also, the ClassPathMapperScanner now wait for a very long scan twice before allowing me to start my application. Is it because we do not use the @Repository in spring-data-mybatis ?

2016-12-05 13:41:57.020 DEBUG 27112 --- [           main] o.s.b.a.AutoConfigurationPackages        : @EnableAutoConfiguration was declared on a class in the package 'com'. Automatic @Repository and @Entity scanning is enabled.
2016-12-05 13:41:57.611  WARN 27112 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com]' package. Please check your configuration.
2016-12-05 13:42:56.143  WARN 27112 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[${mybatis.mapper.base.package:*}]' package. Please check your configuration.

It is not very clear what's inside, are theses supposed to work ?

Does your plugin cover or will cover the same scope of usage as hatunet's plugin ?

https://github.com/hatunet/spring-data-mybatis (Doc : http://www.ifrabbit.com/)

Thanks!

Edit:

I forgot, this is my ManagerMapper.xml

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.repository.ManagerRepository">

    <resultMap id="managerResultMap" type="com.domain.Manager">
        <id property="id" column="id"/>
        <result property="login" column="login"/>
    </resultMap>

    <sql id="getManagerSelect">
        SELECT
        manager.`ID` as 'id',
        manager.`LOGIN` as 'login'
    </sql>

    <sql id="getManagerFrom">
        FROM
        `MANAGER` manager
    </sql>

    <select id="count" resultType="java.lang.Long" parameterType="Integer">
        SELECT COUNT(ID)
        <include refid="getManagerFrom"/>
    </select>

    <select id="exists" resultType="java.lang.Boolean" parameterType="Integer">
        SELECT COUNT(ID)
        <include refid="getManagerFrom"/>
        WHERE
        ID = #{id};
    </select>

    <select id="find" resultMap="managerResultMap" parameterType="Integer">
        <include refid="getManagerSelect"/>
        <include refid="getManagerFrom"/>
        <if test="pk">
            WHERE manager.`ID` = #{pk.value}
        </if>
    </select>
</mapper>
jclagache commented 7 years ago

Hi @kopax, MyBatisRepository extends Repository , not PagingAndSortingRepository. All the errors you've got derive from this.

kopax commented 7 years ago

@jclagache I have found this plugin : https://github.com/hatunet/spring-data-mybatis

It take care of all sort of repository and is hateoas compatible. I've just tested the 1.0.2 and it work very fine.

jclagache commented 7 years ago

https://github.com/hatunet/spring-data-mybatis looks brillant ! Nice to see that you finally found what you were strongly looking for.