我在spring-boot上有spring + jpa + rest应用程序(尝试实现)
spring-boot申请
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import javax.persistence.PersistenceContext;
@SpringBootApplication
@ComponentScan("package")
@EnableJpaRepositories(basePackages = "package.dao", entityManagerFactoryRef = "emf")
@ImportResource("applicationContext.xml")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我休息了-控制器
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import package.myService;
@RestController
public class TestController {
@Autowired
private MyService myService;
@PostMapping("create")
public void test1(){
}
@PostMapping("{id}")
public void test2(@RequestBody Object object){
}
@GetMapping("{id}")
public void test3(@PathVariable Long id){
}
}
实体:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.math.BigDecimal;
@Entity
public class MyEntity {
@Id
@GeneratedValue
private Long id;
private String name;
public MyEntity() {
}
public MyEntity(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
存储库为:
MyEntityRepository:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface MyEntityRepositoryextends JpaRepository<myEntity, Long>{
public void findNameById(@Param("id") Long id);
}
资源中的应用上下文:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config />
<bean id="riskMetricService" class="ru.sbrf.risks.services.data.service.RiskMetricServiceImpl"/>
</beans>
如果需要,我可以添加pom.xml
尝试分层时出现错误消息:应用程序启动失败
说明:
构造函数的参数0 org.springframework.data.rest.webmvc.RepositorySearchController 需要一个找不到的名为“ emf”的bean。
操作:
在哪里需要使用dbconnection配置定位文件? 如何解决此错误消息?
答案 0 :(得分:1)
在您的应用程序中,您引用的是一个名为emf的实体管理器
entityManagerFactoryRef =“ emf”
您应该定义它。您可以在应用程序上下文中或在 @Configuration类。
例如,您可以这样定义它:
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
......
</bean>