0

我想了解如何在我的数据库上实现添加、编辑、删除和搜索等通用方法,我已经建立了连接(休眠)并且工作正常

我有这个方法,有效

类:通用DAO

public <T> T save(final T o){
        Session session=HibernateUtil.getSessionFactory().openSession();
        Transaction trans=session.beginTransaction();
        Object object = (T) session.save(o);
        trans.commit();
        return (T) object;
    }

并在主要

GenericDAO gen = new GenericDAO();
gen.save(object); 

我也有其他方法我不知道如何使用它们

类:通用DAO

public void delete(final Object object){
   Session session=HibernateUtil.getSessionFactory().openSession();
   Transaction trans=session.beginTransaction();
   session.delete(object);
   trans.commit();
}

/***/
public <T> T get(final Class<T> type, final int id){
    Session session=HibernateUtil.getSessionFactory().openSession();
    Transaction trans=session.beginTransaction();
    Object object = (T) session.get(type, id);
    trans.commit();
    return (T) object;
}

public <T> List<T> getAll(final Class<T> type) {
    Session session=HibernateUtil.getSessionFactory().openSession();
    Transaction trans=session.beginTransaction();
    final Criteria crit = session.createCriteria(type);
    List<T> list = crit.list();
    trans.commit();
    return list;
}

谢谢

4

2 回答 2

3

我认为GenericDAO类是基类。它不是用来直接使用的。你检查过这篇文章吗?我检查了这篇文章并创建了一个示例项目。

例子

GitHub - generic-dao-hibernate 示例

例如,您可能希望根据 MySQL 第一步示例创建一个 API 来检索所有员工列表。

雇员表模式如下:

基础 SQL

    CREATE TABLE employees (
        emp_no      INT             NOT NULL,  -- UNSIGNED AUTO_INCREMENT??
        birth_date  DATE            NOT NULL,
        first_name  VARCHAR(14)     NOT NULL,
        last_name   VARCHAR(16)     NOT NULL,
        gender      ENUM ('M','F')  NOT NULL,  -- Enumeration of either 'M' or 'F'  
        hire_date   DATE            NOT NULL,
        PRIMARY KEY (emp_no)                   -- Index built automatically on primary-key column
                                               -- INDEX (first_name)
                                               -- INDEX (last_name)
    );

O/R 映射

Hibernate 要求您配置映射对象关系设置。之后,您将享受将 object-to-sql 和 sql-to-object 的转换。

基于SQL的实体类

  • @Entity, @Table, @Id, @Column, @GeneratedValue来自休眠
  • @Data, @NoArgsConstructor来自 lombok,它减少了 getter/setter 代码
  • @XmlRootElement, @XmlAccessorType来自 jaxb,你可能不需要使用它

    @Entity
    @Data
    @NoArgsConstructor
    @Table(name = "employees")
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement
    public class Employees implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @Id
        @Column(name = "emp_no", unique = true)
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer empNo;
    
        @Column(name = "birth_date")
        private Date birthDate;
    
        @Column(name = "first_name")
        private String firstName;
    
        @Column(name = "last_name")
        private String lastName;
    
        @Column(name = "gender")
        @Enumerated(EnumType.STRING)
        private Gender gender;
    
        @Column(name = "hire_date")
        private Date hireDate;
    }
    

前端资源类

您总是需要编写 DAO(数据访问对象)来访问数据库。GenericDAO是一种减少样板源代码的方法。

雇员资源类

  • WEB API 上的 CRUD 操作
    • #create, #read,#update#delete

应该等同于

  • SQL
    • INSERT, SELECT,UPDATEDELETE

您需要使用密钥识别一条或多条记录。在这种情况下,id是示例主键。

    @Path("/employee")
    public class EmployeesResource {

        static Logger log = LoggerFactory.getLogger(EmployeesResource.class);

        @GET
        @Produces(MediaType.APPLICATION_JSON)
        public List<Employees> index(@BeanParam Employees paramBean) {
            EmployeesDao dao = (EmployeesDao) SpringApplicationContext.getBean("employeesDao");
            List<Employees> result = dao.read();
            System.out.println("Get all employees: size = " + result.size());
            return result;
        }

        @GET
        @Path("{id}")
        @Produces(MediaType.APPLICATION_JSON)
        public Employees show(@PathParam("id") Integer id) {
            EmployeesDao dao = (EmployeesDao) SpringApplicationContext.getBean("employeesDao");
            System.out.println("Get employees -> id = " + id);
            return dao.read(id);
        }

        @POST
        @Consumes(MediaType.APPLICATION_JSON)
        public Integer create(Employees obj) {
            EmployeesDao dao = (EmployeesDao) SpringApplicationContext.getBean("employeesDao");
            return dao.create(obj);
        }

        @PUT
        @Path("{id}")
        @Consumes(MediaType.APPLICATION_JSON)
        public void update(Employees obj, @PathParam("id") String id) {
            EmployeesDao dao = (EmployeesDao) SpringApplicationContext.getBean("employeesDao");
            dao.update(obj);
        }

        @DELETE
        @Path("{id}")
        public void destroy(@PathParam("id") Integer id) throws Exception {
            EmployeesDao dao = (EmployeesDao) SpringApplicationContext.getBean("EmployeesDao");
            dao.delete(id);
        }
    }

GenericDao 接口和实现

接口(来自 ibm 的帖子)

根据post,我们可以声明dao接口。然后我们应该实现该接口的方法。

    public interface GenericDao<T, PK extends Serializable> {

        /** Persist the newInstance object into database */
        PK create(T newInstance);

        /**
         * Retrieve an object that was previously persisted to the database using
         * the indicated id as primary key
         */
        T read(PK id);
        List<T> read();

        /** Save changes made to a persistent object. */
        void update(T transientObject);

        /** Remove an object from persistent storage in the database */
        void delete(PK id) throws Exception;
        void delete(T persistentObject) throws Exception;
    }

执行

    public class GenericDaoHibernateImpl<T, PK extends Serializable> implements GenericDao<T, PK> {

        private Class<T> type;

        @Autowired
        private SessionFactory sessionFactory;

        public SessionFactory getSessionFactory() {
            return sessionFactory;
        }

        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }

        public GenericDaoHibernateImpl(Class<T> type) {
            this.type = type;
        }

        // Not showing implementations of getSession() and setSessionFactory()
        private Session getSession() {
            Session session = sessionFactory.getCurrentSession();
            return session;
        }

        @Transactional(readOnly = false, rollbackFor = RuntimeException.class)
        public PK create(T o) {
            return (PK) getSession().save(o);
        }

        @Transactional(readOnly = false, rollbackFor = RuntimeException.class)
        public void update(T o) {
            getSession().update(o);
        }

        @Transactional(readOnly = true)
        public T read(PK id) {
            return (T) getSession().get(type, id);
        }

        @SuppressWarnings("unchecked")
        @Transactional(readOnly = true)
        public List<T> read() {
            return (List<T>) getSession().createCriteria(type).list();
        }

        @Transactional(readOnly = false, rollbackFor = RuntimeException.class)
        public void delete(PK id) {
            T o = getSession().load(type, id);
            getSession().delete(o);
        }

        @Transactional(readOnly = false, rollbackFor = RuntimeException.class)
        public void delete(T o) {
            getSession().delete(o);
        }

如果您在项目中只使用简单的 CRUD 操作,则无需为 SQL 操作附加任何代码。例如,您可以使用or创建另一个简单的 SQL 表,例如divisions_tableor 。personnel_tableextends GenericDao<Division, Integer>extends GenericDao<Personnel, Integer>

编辑

要实例化与每个表相关的真正的dao类,您需要配置applicationContext.xml和bean。

例子

<bean id="employeesDao" parent="abstractDao">
    <!-- You need to configure the interface for Dao -->
    <property name="proxyInterfaces">
        <value>jp.gr.java_conf.hangedman.dao.EmployeesDao</value>
    </property>
    <property name="target">
        <bean parent="abstractDaoTarget">
            <constructor-arg>
                <value>jp.gr.java_conf.hangedman.models.Employees</value>
            </constructor-arg>
        </bean>
    </property>
</bean>

附言

你需要记住这篇文章是十年前写的。而且,您应该认真考虑哪个 O/R 映射器真的很好。我认为 O/R 映射器现在略有下降。代替 Hibernate,你可以找到MyBatis , JOOQ

于 2017-08-04T02:34:12.630 回答
1

这是实现以休眠为中心的通用 DAO 的一种方法。它提供基本的 CRUD 操作以及简单的搜索,但可以扩展以包含其他通用功能。

IGenericDAO 接口

public interface IGenericDAO<T extends Serializable> {

T findOne(long id);

List<T> findAll();

void create(T entity);

void update(T entity);

void delete(T entity);

void deleteById(long entityId);

public void setClazz(Class<T> clazzToSet);

}

抽象模板DAO

import java.io.Serializable;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class AbstractHibernateDAO<T extends Serializable> implements IGenericDAO<T> {

private Class<T> clazz;

@Autowired
SessionFactory sessionFactory;

public final void setClazz(Class<T> clazzToSet) {
    this.clazz = clazzToSet;
}


@Override
public T findOne(long id) {
    return (T) getCurrentSession().get(clazz, id);
}


@Override
public List<T> findAll() {
    return getCurrentSession().createQuery("from " + clazz.getName(),clazz).getResultList();
}


@Override
public void create(T entity) {
    getCurrentSession().persist(entity);
}


@Override
public void update(T entity) {
    getCurrentSession().merge(entity);
}


@Override
public void delete(T entity) {
    getCurrentSession().delete(entity);
}


@Override
public void deleteById(long entityId) {
    T entity = findOne(entityId);
    delete(entity);
}

protected final Session getCurrentSession() {
    return sessionFactory.getCurrentSession();
    }
}

通用HiberateDAO

注意:这里使用范围原型。spring 容器在每个请求上创建一个新的 dao 实例。

@Repository
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class GenericHibernateDAO<T extends Serializable> extends AbstractHibernateDAO<T> 
implements IGenericDAO<T> {
   //
}

服务等级

展示如何在服务类中使用自动装配通用 dao 并为模型类传递参数。另外,请注意此实现使用@Transactional 注释进行春季事务管理。

@Service
public class TestService implements ITestService {

private IGenericDAO<TestModel> dao;

@Autowired
public void setDao(IGenericDAO<TestModel> daoToSet) {
    dao = daoToSet;
    dao.setClazz(TestModel.class);

}

@Override
@Transactional
public List<TestModel> findAll() {
    return dao.findAll();
}
}

应用配置

展示如何使用 @EnableTransactionManagement 为自动事务管理设置 spring

@Configuration
@ComponentScan("com.base-package")
@EnableTransactionManagement
public class AppConfig {

       // add hibernate configuration

      // add beans


}
于 2019-01-06T15:17:19.393 回答