OA Project 4: basedao Design

Source: Internet
Author: User

Note: This tutorial isChuanzhi podcast Tang YangguangI use the text version of the Free OA project video published by the lecturer to enhance my development knowledge. If some netizens repost it, please note. Thank you.

1. Why design basedao?

To reduce the amount of code, use the least code to implement the most functions.

2. Create a CN. Clear. OA. Base package and write an interface class under the package. It has the basic addition, deletion, modification, and query functions. The details are as follows:

1 package CN. clear. oa. base; 2 3 Import Java. util. list; 4 5 public interface basedao <t> {6/** 7 * query object objects by ID 8 * @ Param ID 9 * @ return10 */11 Public t findbyid (long ID ); 12/** 13 * query object sets by IDS 14 * @ Param ids15 * @ return16 */17 public list <t> findbyids (long [] IDs ); 18/** 19 * query all 20 * @ return21 */22 public list <t> findall (); 23/** 24 * Save the object 25 * @ Param entity26 */27 public void save (T entity ); 28/** 29 * update object 30 * @ Param entity31 */32 public void Update (T entity ); 33/** 34 * delete an object by ID 35 * @ Param id36 */37 public void Delete (long ID); 38}
Basedao. Java

3. Write a basedao implementation class basedaoimpl. Java under the package to implement all the methods. The specific content is as follows:

1 package CN. clear. oa. base; 2 3 Import Java. lang. reflect. parameterizedtype; 4 Import Java. util. collections; 5 import Java. util. list; 6 7 Import javax. annotation. resource; 8 9 Import Org. hibernate. session; 10 Import Org. hibernate. sessionfactory; 11 12 13 @ suppresswarnings ("unchecked") 14 public abstract class basedaoimpl <t> implements basedao <t> {15 16 @ resource17 private sessionfactory; // Introduce sessionfactory18 private class <t> clazz; // The specific object 19 20 public basedaoimpl () {21 // obtain the generic parent class 22 parameterizedtype Pt = (parameterizedtype) This. getclass (). getgenericsuperclass (); 23 // obtain the final object 24 this. clazz = (class <t>) pt. getactualtypearguments () [0]; 25 // test whether the expected object 26 system. out. println ("clazz --->" + clazz); 27} 28/** 29 * Get the session object. With protected, the subclass can directly use 30 * @ return31 */32 protected session getsession () {33 return sessionfactory. getcurrentsession (); 34} 35 36 Public t findbyid (long ID) {37 If (ID = NULL) {38 return NULL; 39} 40 return (t) getsession (). get (clazz, ID); 41} 42 43 public list <t> findbyids (long [] IDs) {44 45 if (IDs = NULL & IDs. length = 0) {46 Return collections. emptylist (); 47} 48 49 return getsession (). createquery (// 50 "from" + clazz. getsimplename () + "where ID in (: IDS)") // 51. setparam Eter ("IDS", IDS) // 52. list (); 53} 54 55 public list <t> findall () {56 57 Return getsession (). createquery (// 58 "from" + clazz. getsimplename () // 59. list (); 60} 61 62 public void save (T entity) {63 64 getsession (). save (entity); 65} 66 67 public void Update (T entity) {68 getsession (). update (entity); 69} 70 71 public void Delete (long ID) {72 73 object OBJ = findbyid (ID); 74 if (OBJ! = NULL) {75 getsession (). Delete (OBJ); 76} 77 78} 79 80}
Basedaoimpl. Java

4. Use our existing entity classes to test the DaO function.

1. Write an interface class userdao. Java on the DaO layer and inherit from basedao. java. The content is as follows:

1 package cn.clear.oa.dao;2 3 import cn.clear.oa.base.BaseDao;4 import cn.clear.oa.domain.User;5 6 public interface UserDao extends BaseDao<User>{7 8 }
Userdao. Java

2. Write the userdao implementation class userdaoimpl. Java at the daoimpl layer. The content is as follows:

 1 package cn.clear.oa.dao.impl; 2  3 import org.springframework.stereotype.Repository; 4  5 import cn.clear.oa.base.BaseDaoImpl; 6 import cn.clear.oa.dao.UserDao; 7 import cn.clear.oa.domain.User; 8 @Repository 9 public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {10 11 }
Userdaoimpl. Java

5. We will write a testbasedao. Java class at the tset layer to test the DaO function:

 1 package cn.clear.oa.test; 2  3 import org.junit.Test; 4 import org.springframework.stereotype.Service; 5  6 import cn.clear.oa.dao.RoleDao; 7 import cn.clear.oa.dao.UserDao; 8 import cn.clear.oa.dao.impl.RoleDaoImpl; 9 import cn.clear.oa.dao.impl.UserDaoImpl;10 11 @Service12 public class TsetBaseDao {13     @Test14     public void test() throws Exception {15         UserDao userDao = new UserDaoImpl();16 17     }18 }
Tsetbasedao. Java

Perform a single-point test. If the following information is printed on the console, the test is successful:

Clazz ---> class CN. Clear. OA. domain. User

So far, we have completed the most basic Dao data operations.

      

OA Project 4: basedao Design

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.