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.
- 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) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; return id == user.id && Objects.equals(name, user.name); } @Override public int hashCode() { return Objects.hash(id, name); } public User(int id, String name) { this.id = id; this.name = name; }
- 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) { map.put(id,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 map.put(id,entity); } //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 list.add(map.get(s)); } return list; } //delete public void delete(String id) { map.remove(id); }
- 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"); //System.out.println(u); //To update userDao.update("3",new User(5,"Durant")); // System.out.println(userDao.getT("3")); //Return List object collection List list = userDao.getList(); //System.out.println(list); //Delete object with id 3 userDao.delete("5"); System.out.println(list); }
Make a little progress every day. Come on, for the one you love. Fight every day!