我已经使用 gradle 创建了 spring boot 数据 jpa 应用程序,我的项目结构如下所示。
com.duregesh
--TestSpringBootDataJpaApplication.java
com.durgesh.controller
--UserController.java
com.durgesh.model
--User.java
com.durgesh.repositories
--UserJpaRepository.java
com.durgesh.services
--UserServiceImpl.java
当我在下面运行spring boot时,异常正在触发描述:
com.durgesh.services.UserServiceImpl 中的字段 userJpaRepository 需要一个名为“emf”的 bean,但无法找到。
行动:考虑在您的配置中定义一个名为“emf”的 bean。
@SpringBootApplication
public class TestSpringBootDataJpaApplication {
public static void main(String[] args) {
SpringApplication.run(TestSpringBootDataJpaApplication.class, args);
}
-------------
public interface UserJpaRepository extends JpaRepository<User, Long> {}
-------------
@Entity
@Table(name = "USER")
public class User implements Serializable{
@Id
private Long id;
@Column(unique = true)
private String uid;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
----------------
@Service
@EnableJpaRepositories(basePackages="com.construction.de.*", entityManagerFactoryRef="emf")
public class UserServiceImpl implements UserService {
@Autowired
private UserJpaRepository userJpaRepository;
@Override
public User add(final User user) {
return userJpaRepository.save(user);
}
@Override
public User findById(final Long id) {
final User user = userJpaRepository.findOne(id);
return user;
}
}
----
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value ="/",method = RequestMethod.POST)
public User add( @RequestBody final User user){
return userService.add(user);
}
@RequestMapping(value ="/{id}",method = RequestMethod.GET)
public User findById(@PathVariable("id") final Long id){
return userService.findById(id);
}
}