簡體   English   中英

聲明createEntityManagerFactory的最佳方法

[英]best way to declare createEntityManagerFactory

我實際上在JPA的springboot項目上工作。 我正在尋找一個更好的實現,目前它的工作原理,但我的印象是它不是最好的方式

    @RestController
public class inscription {

EntityManagerFactory objFactory = Persistence.createEntityManagerFactory("com.myapplication_jar_0.0.1-SNAPSHOTPU");

 UserJpaController userCtrl = new UserJpaController(objFactory);
 SerialsJpaController licenseCtrl = new SerialsJpaController(objFactory);


   @CrossOrigin(origins = CURRENT_IP)
    @RequestMapping(value = "/createaccount", method = RequestMethod.GET)
    public CreatAccountResponseTemplate createAccount(
            @RequestParam(value = "login") String login,
            @RequestParam(value = "password") String password,
         ) 
    {
        EntityManager manager = objFactory.createEntityManager();

        CreatAccountResponseTemplate responseTemplate = new CreatAccountResponseTemplate();

...}

Spring JPA有助於減少配置數據存儲庫所需的樣板代碼。

也許使用EntityManagerFactory作為RestController的成員可能是一個不必要的依賴項。 這是另一種選擇:

  1. 創建您的域名

實體

@Entity
public class DataCenter {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;

    private String name;

    private String location;

   .......

}
  1. 創建接口存儲庫以處理與您的實體相關的數據庫操作。

知識庫

public interface DataCenterRepository extends JpaRepository<DataCenter,String> {}
  1. Repository到控制器,此示例適用於標准控制器,但它也適用於RestController

控制器

@Controller
@RequestMapping("/datacenters")
public class DataCenterController {

    private final DataCenterRepository dataCentersRepository;

    @Autowired
    public DataCenterController(DataCenterRepository dataCentersRepository){
        this.dataCentersRepository=dataCentersRepository;
    }

@RequestMapping(value = "/save", method=RequestMethod.POST)
public ModelAndView  save(@RequestParam(value="name") String datacenterName,
                          @RequestParam(value="age") String datacenterLocation, ModelAndView  modelAndView ) {
    DataCenter dataCenter = new DataCenter(datacenterName, datacenterLocation);
    dataCentersRepository.save(dataCenter);
    modelAndView.addObject("datacenter", dataCenter);
    modelAndView.setViewName("success");
    return modelAndView;
}

    @RequestMapping(value="/all", method=RequestMethod.GET)
    public String getAll(Model model){
        model.addAttribute("datacenters", dataCentersRepository.findAll());
        return "datacenters";
    }

如果你被迫@Autowired你的EntityManagerFactory然后

@Autowired
private EntityManagerFactory entityManagerFactory;

在spring boot中創建EntityManagerFactory的最佳方法是在application.properties文件中編寫以下配置。

spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=false
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform = org.hibernate.dialect.PostgreSQLDialect

spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.username=postgres
spring.datasource.password=root
spring.datasource.driver-class-name=org.postgresql.Driver

以上配置使用postgreSQL數據庫。 此配置將自動創建DataSourceEntityManagerFactoryJpaTransactionManager bean,從而簡化數據庫連接。 您還可以使用以下代碼訪問entityManager對象:

   @PersistenceContext
   private EntityManager entityManager;

有用的鏈接:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM