Hibernate:如何为CRUD方法创建GenericDAO

时间:2017-05-20 21:51:07

标签: java mysql sql hibernate

我试图为基本的CRUD方法创建一个GenericDAO,以便我可以重用代码,但实际上并不知道如何开始

我已经为每个班级都有一个DAO,并且他们完美地工作

我阅读了很多教程,并下载了项目,但我不能将其添加(或理解)到我的程序

这是我的

public class Cliente {
private String nombre, direccion, telefono, cuit;
private int codigo, codigoPostal;
private double saldo, deuda;

public Cliente(String nombre, String direccion, int codigoPostal, String telefono, String cuit) {
    this.nombre = nombre;
    this.direccion = direccion;
    this.codigoPostal = codigoPostal;
    this.telefono = telefono;
    this.cuit = cuit;
    this.saldo = 0;
    this.deuda = 0;
}

public Cliente(){
}

//all the getters and setters

这是我的 GenericDAO 无效

public class GenericDAO {

@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;

public <T> T save(final T o){
  return (T) sessionFactory.getCurrentSession().save(o);
}


public void delete(final Object object){
  sessionFactory.getCurrentSession().delete(object);
}

/***/
public <T> T get(final Class<T> type, final long id){
  return (T) sessionFactory.getCurrentSession().get(type, id);
}

/***/
public <T> T merge(final T o)   {
  return (T) sessionFactory.getCurrentSession().merge(o);
}

/***/
public <T> void saveOrUpdate(final T o){
  sessionFactory.getCurrentSession().saveOrUpdate(o);
}

public <T> List<T> getAll(final Class<T> type) {
  final Session session = sessionFactory.getCurrentSession();
  final Criteria crit = session.createCriteria(type);
 return crit.list();
}

}

我的 Class.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="principal.Cliente" table="Cliente">
    <id name="codigo" column="codigo">
        <generator class="identity" />
    </id>
    <property name="nombre" type="string" column="nombre"/>
    <property name="direccion" type="string" column="direccion"/>
    <property name="telefono" type="string" column="telefono"/>
    <property name="cuit" type="string" column="cuit"/>
    <property name="codigoPostal" type="int" column="cp"/>
    <property name="saldo" type="double" column="saldo"/>
    <property name="deuda" type="double"  column="deuda"/> 
</class>
</hibernate-mapping>

这是我的 HibernateUtil

public class HibernateUtil {

private static SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory()
{
  try
  {
     if (sessionFactory == null)
     {
        Configuration configuration = new Configuration().configure(HibernateUtil.class.getResource("/hibernate.cfg.xml"));
        StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
        serviceRegistryBuilder.applySettings(configuration.getProperties());
        ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
     }
     return sessionFactory;
  } catch (Throwable ex)
  {
     System.err.println("Initial SessionFactory creation failed: " + ex);
     throw new ExceptionInInitializerError(ex);
  }
}

public static SessionFactory getSessionFactory()
{
  return sessionFactory;
}

public static void shutdown()
{
  getSessionFactory().close();
}

和我的 hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/basededatosprueba</property>
    <property name="connection.username">root</property>
    <property name="connection.password"></property>
    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>
    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>

    <mapping resource="mapeos/Cliente.hbm.xml"/>
</session-factory>
</hibernate-configuration>

如果有人能解释如何使其发挥作用,我将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

duffymo 的解决方案

<块引用>

使用 Spring Boot CrudRepository: spring.io/guides/gs/accessing-data-jpa