The application of generics and the thought of Dao layer business

Keywords: Database

The application of generics and the thought of Dao layer business

generic paradigm

Generic, that is, parameterized type. When referring to parameters, the most familiar thing is that there is a formal parameter when defining the method, and then the arguments are passed when the method is called. How to understand parameterized type? As the name implies, the type is parameterized from the original concrete type, similar to the variable parameter in the method. At this time, the type is also defined as the parameter form (which can be called type parameter), and then the concrete type (type parameter) is passed in when using / calling. Generally speaking, that is to say, the type of an uncertain class can be changed according to the business needs to meet the needs of business development.

Thinking method of Dao layer business

Data access object is called database access object for short. It is used to add, delete, modify and query database data.
The following example is to use a collection to access data for data addition, deletion, modification and query.

  1. Create three classes - POJO, Dao and Test. This is the process of this Test. First, create the User class:
  private int id;
  private String name;

  public int getId() {
      return id;

  public void setId(int id) { = id;

  public String getName() {
      return name;

  public void setName(String name) { = name;

  public String toString() {
      return "User{" +
              "id=" + id +
              ", name='" + name + '\'' +

  public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;
      User user = (User) o;
      return id == &&

  public int hashCode() {
      return Objects.hash(id, name);

  public User(int id, String name) { = id; = name;
  1. In the design of UserDao layer, the parameter key is String and the Value is generic (T). Add, delete, modify and query the Map set.
Map<String ,T> map = new HashMap<String, T>();

       //Increase operation
       public void add(String id,T t) {

       //Get the value of an object
       public T getT(String id) {
           T t = map.get(id);
           return t;
       //To update
       public void update(String id,T entity) {
           //Using the put method of map to overwrite the old id with the new one
       //Returns a List collection
       public List<T> getList() {
           List<T> list = new ArrayList<T>();
           //Take the key value and receive it by String type
           for(String s : map.keySet()) {
               //Get the key value first
               return list;
       public void delete(String id) {
  1. Test class is used to add, delete, modify and query the newly created userDao.
       UserDao<User> userDao = new UserDao<User>();
       userDao.add("2",new User(1,"Xiao Ming"));
       userDao.add("3",new User(2,"Xiao Hu"));
       User u = userDao.getT("2");
       //To update
       userDao.update("3",new User(5,"Durant"));
      // System.out.println(userDao.getT("3"));
       //Return List object collection
       List list = userDao.getList();
       //Delete object with id 3


Make a little progress every day. Come on, for the one you love. Fight every day!

Posted by matthew1 on Sun, 01 Dec 2019 07:43:42 -0800