0

我已经使用 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);        
    }
}
4

1 回答 1

0

问题在于您的数据库entityManager配置,您需要按照以下步骤操作:

(1) 提供@EnableJpaRepositories给如下所示的应用程序类(将其从服务中删除)。

@SpringBootApplication
@EnableJpaRepositories(basePackages="com.duregesh.*")
public class TestSpringBootDataJpaApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestSpringBootDataJpaApplication.class, args);
}

application.properties(2) 提供你在(src/main/resourceseclipse中或server中文件夹下)中配置的数据库驱动和连接属性WEB-INF/classes,如下图:

spring.datasource.url: YOUR_DB_URL
spring.datasource.driverClassName: YOUR_DB_DRIVER_CLASS
spring.datasource.username: YOUR_DB_USER
spring.datasource.password: YOUR_DB_PWD
于 2016-11-24T11:46:06.343 回答