Encapsulation Implementation of add, delete, modify, and query in SSH
When we use SSH, such as User, Admin, and other object, there are common addition, deletion, modification, and query methods. How can we implement this? See the following example:
This type of structure chart may be frequently used during the opening process. For example, the following project instance:
Where:
(1) BaseDao is the interface definition for adding, deleting, modifying, and querying. It is a parent interface and the following UserDao and RoleDao will inherit this interface;
(2) BaseDaoImpl is the implementation class of the add, delete, modify, and query interface, and is a parent class. Both the following UserDaoImpl and RoleDaoImpl inherit this interface;
(3) UserDao and RoleDao are interfaces defined by their own methods;
The Code is as follows:
BaseDao. java
import java.util.List;public interface BaseDao
{ void save(T entity); void delete(long id); void update(T entity); T getById(long id); List
findAll(); List
getByIdS(Long[] ids);}
BaseDaoImpl. java
Import java. lang. reflect. ParameterizedType; import java. util. List; import javax. annotation. Resource; import org. hibernate. Session; import org. hibernate. SessionFactory; public class BaseDaoImpl
Implements BaseDao
{@ Resource private SessionFactory sessionFactory; // gets the private Class of the sessionFactory object.
Clazz; // indicates the current type. * ** obtain the real value of subclass T through reflection ** because the parent class constructor */public BaseDaoImpl () is called first in the subclass () {ParameterizedType pt = (ParameterizedType) this. getClass (). getGenericSuperclass (); // obtain the generic parent Class type of the current new object. clazz = (Class
) Pt. getActualTypeArguments () [0]; // obtain the actual type of the first type parameter}/*** protected: You can directly use */protected Session getSession () in the subclass () {return sessionFactory. getCurrentSession ();} public void save (T entity) {getSession (). save (entity);} public void delete (long id) {Object object = getById (id); // obtain this object first if (Object! = Null) {getSession (). delete (object); // delete this object when the object is not empty} public void update (T entity) {getSession (). update (entity) ;}@ SuppressWarnings ("unchecked") public T getById (long id) {return (T) getSession (). get (clazz, id) ;}@ SuppressWarnings ("unchecked") public List
FindAll () {return getSession (). createQuery ("FROM" + clazz. getSimpleName (). list () ;}@ SuppressWarnings ("unchecked") public List
GetByIdS (Long [] ids) {return getSession (). createQuery (// "FROM" + clazz. getSimpleName () + "WHERE id IN (: ids )")//. setParameterList ("ids", ids )//. list ();}}
One of the key technologies here is how to obtain the deterministic type of T in the generic model, which is obtained through the reflection mechanism.
Then the following several basic classes determine the inheritance and implementation relationships, and nothing else is done,
public class RoleDaoImpl extends BaseDaoImpl
implements RoleDao {}
public class UserDaoImpl extends BaseDaoImpl
implements UserDao {}
public interface RoleDao extends BaseDao
{}
public interface UserDao extends BaseDao
{}