Spring整合Hibernate过程中一个Service只能处理一个跟数据库连接的dao吗????-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

Spring整合Hibernate过程中一个Service只能处理一个跟数据库连接的dao吗????

爵霸 2016-03-03 09:44:11 1687

具体情况:使用@Service @Transactional处理跟数据库连接的dao时,通过@Autowired自动引入的dao只能有一个吗?我已实现了对TeacherDao的自动引入并可以完成相关增删改操作,可是当我想用同一个labservice再次引用AnnouncementDao去连接相应的数据库表进行查询操作时发现报错:

Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private a.service.LabService a.controller.HomeController.labService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'labServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private a.dao.AnnouncementDao a.service.LabServiceImpl.announcementDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [a.dao.AnnouncementDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Spring-servlet.xml配置文件相关内容:

    <!-- 指定Hibernate配置文件的位置-->
        <property name="configLocations">  
            <list>  
                <value>  
                    classpath*:hibernate.cfg.xml  
                </value>  
            </list>  
        </property> 


    </bean>


    <!-- 配置Spring事务注解-->
    <tx:annotation-driven transaction-manager="transactionManager"/>


    <!-- 配置Spring事务管理-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
hibernate.cfg.xml  :

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC  
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 


<hibernate-configuration>
    <session-factory>
        <mapping class="a.model.Manager" />
        <mapping class="a.model.Teacher" />
        <mapping class="a.model.Message" />
        <mapping class="a.model.Announcement" />
      
    </session-factory>          
</hibernate-configuration>
a.model.Manager:实体类,映射数据库属性。

package a.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity 
@Table(name="Manager")
public class Manager {

@Id
@Column(name="mno")
private String mno;
@Column(name="mname")
private String mname;
@Column(name="mpassword")
private String mpassword;
@Column(name="mgender")
private String mgender;
@Column(name="mbirthday")
//@DateTimeFormat(pattern="yyy-mm-dd")
private String mbirthday;
@Column(name="mtel")
private String mtel;
@Column(name="mschool")
private String mschool;
....set(),get()


a.service.labservice:

package a.service;
import a.dao.AnnouncementDao;
import a.dao.ManagerDao;
import a.dao.MessageDao;
import a.dao.TeacherDao;
import a.model.Teacher;
import a.model.Announcement;
import a.model.Message;
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class LabServiceImpl implements LabService {


@Autowired(required=false)
private TeacherDao teacherDao;
public Teacher getTeacherDao(String no) {
return teacherDao.getTeacher(no);
}
public void setTeacherDao( TeacherDao teacherDao) {
this.teacherDao = teacherDao;
}

@Autowired
private ManagerDao managerDao;

public ManagerDao getManagerDao() {
return managerDao;
}
public void setManagerDao(ManagerDao managerDao) {
this.managerDao = managerDao;
}

@Autowired(required=false) 
private MessageDao messageDao; 
@Autowired 
private AnnouncementDao announcementDao; 
XML Java 数据库连接 数据库 数据格式 Spring
分享到
取消 提交回答
全部回答(1)
  • 被纵养的懒猫
    2019-10-09 16:16:46

    继承HibernateDaoSupport是可以的,但这种方式还是不太好,还是用到了继承,也就是没有降低所谓的耦合度。 spring为我们提供了一个hibernateTemplate,你只需在配置文件中配置一个bean,并传入一个sessionFactory。 <bean id="hibernateTemplate" class="具体的类是有spring提供的,我忘了,你可以查一下"> <property name="sessionFactory" ref="sessionFactory"> </bean> 然后在使用dao的时候注入该hibernateTemplate的bean即可。 <bean id="userDao" class="com.wang.dao.user.UserDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"> <bean>

    答案来源于网络

    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题
推荐课程