具有控制器,服务层,DAO层的Spring Boot项目

时间:2018-10-01 09:24:22

标签: hibernate spring-boot servlets

  

在Spring Boot中实现COntroller,服务层,DAO层

     

创建了耐心的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;
}

}

  

在服务层中创建接口。 (PatientService)

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
└─────┘

1 个答案:

答案 0 :(得分:0)

由于自动配置,通常在春季启动时无需配置HibernateJpaSessionFactoryBean。

@SpringBootApplication
public class HospitalSystemApplication {

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

@SpringBootApplication已具有@EnableAutoConfiguration。另外,它将扫描HospitalSystemApplication所在的软件包。