我无法运行弹簧靴

时间:2016-11-24 11:29:39

标签: spring spring-mvc spring-boot spring-data spring-boot-gradle-plugin

我使用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

当我在弹出启动时,异常正在触发 说明:

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);        
    }
}

1 个答案:

答案 0 :(得分:0)

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

(1)向Application类提供@EnableJpaRepositories,如下所示(从服务中删除)。

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

(2)提供在application.properties(在eclipse中的src/main/resources下或服务器中的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