在java spring mvc app中。 以下是代码的Git repository。
我有以下实体:
package com.example.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import lombok.Data;
@Data
@Entity
public class Profile {
@Id
@GeneratedValue
Long id;
String name;
String address;
String description;
String img;
}
,存储库如下:
package com.example.respository;
public interface ProfileRepository extends JpaRepository<Profile,Long> {}
然后我尝试使用jackson将以下json存储到应用程序中:
[
{
"_class":"com.example.domain.Profile"
,"id":1
,"name":"anon_S4"
,"address":"No 122,narva mnt 234"
,"description":"bala bbbb"
,"img":"aaaaaa"
}
]
但它抱怨:
java.lang.IllegalArgumentException: No repository found for domain type: class com.example.domain.Profile
更新
我使用jackson将json填充到数据库中:
@Configuration
public class DatabasePopulator {
@Autowired
ObjectMapper mapper;
@Bean
public Jackson2RepositoryPopulatorFactoryBean repositoryPopulator() throws IOException {
Resource sourceData = new ClassPathResource("sample-data.json");
Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean();
mapper.registerModule(new JavaTimeModule());
factory.setMapper(mapper);
factory.setResources(new Resource[] { sourceData });
return factory;
}
}
答案 0 :(得分:0)
您应该使用
为MallApplication
课程添加注释
@EntityScan("com.example.domain")
@EnableJpaRepositories(basePackages = "com.example.repository")
默认情况下会扫描包中的Spring Data存储库。
此外,最好从存储库中删除@Repository
,因为此注释是多余的