【问题标题】:Spring Boot project with Controller ,Service Layer,DAO Layer带有控制器、服务层、DAO 层的 Spring Boot 项目
【发布时间】:2018-10-01 09:24:21
【问题描述】:

在Spring Boot中实现控制器、服务层、DAO层

创建了 Patient pojo 类,

package com.hospital.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="Patient")
public class Patient {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int tokenNumber;

private String patientName;

private  int phoneNumber;

private int patientAge;

public int getTokenNumber() {
    return tokenNumber;
}

public void setTokenNumber(int tokenNumber) {
    this.tokenNumber = tokenNumber;
}

public String getPatientName() {
    return patientName;
}

public void setPatientName(String patientName) {
    this.patientName = patientName;
}

public int getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(int phoneNumber) {
    this.phoneNumber = phoneNumber;
}

public int getPatientAge() {
    return patientAge;
}

public void setPatientAge(int patientAge) {
    this.patientAge = patientAge;
}

@Override
public String toString() {
    return "Patient [tokenNumber=" + tokenNumber + ", patientName=" + patientName + ", phoneNumber=" + phoneNumber
            + ", patientAge=" + patientAge + ", getTokenNumber()=" + getTokenNumber() + ", getPatientName()="
            + getPatientName() + ", getPhoneNumber()=" + getPhoneNumber() + ", getPatientAge()=" + getPatientAge()
            + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString()
            + "]";
}

public Patient(int tokenNumber, String patientName, int phoneNumber, int patientAge) {
    this.tokenNumber = tokenNumber;
    this.patientName = patientName;
    this.phoneNumber = phoneNumber;
    this.patientAge = patientAge;
}

public Patient()
{
    
}

public Patient(String patientName, int phoneNumber, int patientAge) {
    this.patientName = patientName;
    this.phoneNumber = phoneNumber;
    this.patientAge = patientAge;
}

}

使用抽象方法创建 PatientDAO 接口。

package com.hospital.dao;

import java.util.List;

import com.hospital.entity.Patient;

public interface PatientDAO {

    public Patient patientVerifyByTokenumber(int tokenNumber);

    public List<Patient> listOfAllPatients();

}

实现 PatientDAOImpl(Repository) 中的所有方法

    package com.hospital.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.hospital.entity.Patient;



@Repository
@Transactional
public class PatientDAOImpl implements PatientDAO {

    
    

@Autowired
    SessionFactory sessionFactory;



@Override
@Transactional
public Patient patientVerifyByTokenumber(int tokenNumber) {
    Patient patient =this.sessionFactory.getCurrentSession().get(Patient.class, new Integer(tokenNumber));
    return patient;
}

@Override
@Transactional
public List<Patient> listOfAllPatients() {
    List<Patient> patientList=this.sessionFactory.getCurrentSession().createQuery("from Patient").list();
    return patientList;
}

}

在服务层创建接口。 (患者服务)

package com.hospital.service;

import java.util.List;

import com.hospital.entity.Patient;


public interface PatientService {
    
    public Patient patientVerifyByTokenumber(int tokenNumber);
    public List<Patient> listOfAllPatients();
}

在patientServiceimpl中实现Patientservice接口(业务逻辑代码)

package com.hospital.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.hospital.dao.PatientDAO;
import com.hospital.dao.PatientDAOImpl;
import com.hospital.entity.Patient;


@Service
public class PatientServiceImpl implements PatientService{
    
    @Autowired
    PatientDAO patientDAO;
    
    
    
    @Override
    public Patient patientVerifyByTokenumber(int tokenNumber) {
        
        return patientDAO.patientVerifyByTokenumber(tokenNumber);
    }

    @Override
    
    public List<Patient> listOfAllPatients() {
        return patientDAO.listOfAllPatients();
    }
}

创建了一个控制器

package com.hospital.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hospital.entity.Patient;
import com.hospital.service.PatientService;

@Controller
@RequestMapping("patient/")
public class RestController {

    @Autowired
    private PatientService service;


    @RequestMapping(value="verify",method=RequestMethod.POST)
    public Patient patientVerifyByTokenumber(@RequestParam int tokenNumber) {

        return service.patientVerifyByTokenumber(tokenNumber);
    }

    @RequestMapping(value="fetchall",method=RequestMethod.GET)
    public List<Patient> listOfAllPatients() {
        return service.listOfAllPatients();
    }

}

Spring boot 主类

package com.hospital.HospitalSystem;

import javax.persistence.EntityManagerFactory;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean;

@SpringBootApplication
@ComponentScan({"com.hospital"})
@EnableAutoConfiguration
public class HospitalSystemApplication {

    public static void main(String[] args) {
        SpringApplication.run(HospitalSystemApplication.class, args);
    }
    
    @Bean
    public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
        HibernateJpaSessionFactoryBean fact = new HibernateJpaSessionFactoryBean();
        fact.setEntityManagerFactory(emf);
        return fact;
    }
}

应用程序属性代码

spring.datasource.url = jdbc:mysql://localhost:3306/hospital?useSSL=false
    spring.datasource.username = root
    spring.datasource.password = mysql
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
    spring.jpa.properties.hibernate.id.new_generator_mappings = true
    spring.jpa.properties.hibernate.format_sql = true
    spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext

最后我从整个代码中得到了这个错误,找不到确切的位置

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-10-01 14:51:35.778 ERROR 5336 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

   restController (field private com.hospital.service.PatientService com.hospital.controller.RestController.service)
      ↓
   patientServiceImpl (field com.hospital.dao.PatientDAO com.hospital.service.PatientServiceImpl.patientDAO)
      ↓
   patientDAOImpl (field org.hibernate.SessionFactory com.hospital.dao.PatientDAOImpl.sessionFactory)
┌─────┐
|  sessionFactory defined in com.hospital.HospitalSystem.HospitalSystemApplication
└─────┘

【问题讨论】:

  • 看起来 HospitalSystemApplication 引用了自己
  • ya.. 我用 main 方法创建了 HospitalSystemApplication
  • 你需要更多帮助显示相关代码
  • 我上传了整个代码,

标签: hibernate spring-boot servlets


【解决方案1】:

通常在 spring boot 中不需要配置 HibernateJpaSessionFactoryBean,这要归功于 auto-configuracion。

@SpringBootApplication
public class HospitalSystemApplication {

    public static void main(String[] args) {
        SpringApplication.run(HospitalSystemApplication.class, args);
    }
}

@SpringBootApplication 已经有@EnableAutoConfiguration。此外,它还会扫描 HospitalSystemApplication 所在的包。

【讨论】:

  • PD:使用 Spring Data Jpa ;)
猜你喜欢
  • 2021-03-19
  • 1970-01-01
  • 2013-05-16
  • 2011-05-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 2012-01-25
  • 1970-01-01
相关资源
最近更新 更多