Record the next DAO layer class writing, convenient for later use.
1 PackageCom.bupt.auth.dao.impl;2 3 Importjava.util.ArrayList;4 ImportJava.util.HashSet;5 Importjava.util.List;6 ImportJava.util.Set;7 ImportJava.util.concurrent.CopyOnWriteArraySet;8 9 Importorg.springframework.stereotype.Repository;Ten One ImportCom.bupt.auth.dao.RoleDao; A ImportCom.bupt.auth.dao.base.BaseDaoImpl; - Importcom.bupt.auth.entity.Permission; - ImportCom.bupt.auth.entity.Role; the Importcom.bupt.auth.exception.MyException; - -@Repository ("Roledao") - Public classRoledaoimplextendsBasedaoimpl<role>ImplementsRoledao { + - @Override + PublicRole Findroleadminbyuserid (Long id) { A //TODO auto-generated Method Stub atRole role = This. Findrolebyrolenameanduserid (ID, "Administrator"); - - returnrole; - } - -@SuppressWarnings ("Unchecked") in @Override - PublicList<role>Findrolebyuserid (Long id) { to //TODO auto-generated Method stub\ +String sql = "Select R from Role r INNER JOIN fetch r.owneruser u INNER JOIN fetch r.accesstokens LEFT JOIN fetch R.permis sions where u.id =: id "; -list<role> list = GetSession (). CreateQuery (SQL). Setlong ("id", id). list (); the * if(List = =NULL|| List.size () = = 0) $ return NULL;Panax Notoginseng -list<role> result =NewArraylist<role>(); the for(Role role:list) { + if(!result.contains (role)) A Result.add (role); the } + - returnresult; $ } $ -@SuppressWarnings ("Unchecked") - @Override the PublicRole Findrolebyrolenameanduserid (Long ID, String rolename) { - //TODO auto-generated Method StubWuyiString sql = "Select R from Role r INNER JOIN fetch r.owneruser u INNER JOIN fetch r.accesstokens WHERE u.id =: ID and R.N Ame =: rolename "; thelist<role> list = GetSession (). CreateQuery (SQL). Setlong ("id", id). setString ("RoleName", rolename). List (); - Wu if(List = =NULL|| List.size () = = 0) - return NULL; About $ returnList.get (0); - } - - A@SuppressWarnings ("Unchecked") + @Override the PublicList<role>Findrolebypermid (Long id) { - //TODO auto-generated Method Stub $String sql = "Select R from Role r INNER join fetch R.owneruser INNER JOIN fetch r.accesstokens INNER JOIN fetch R.permiss Ions p WHERE p.id =: id "; thelist<role> list = GetSession (). CreateQuery (SQL). Setlong ("id", id). list (); the the if(List = =NULL|| List.size () = = 0) the return NULL; - inlist<role> result =NewArraylist<role>(); the for(Role role:list) { the if(!result.contains (role)) About Result.add (role); the } the the returnresult; + } - the@SuppressWarnings ("Unchecked")Bayi @Override the Public BooleanDeleterolespermissionbyroleidandpermid (role role, theLong Permid)throwsMyException { - //TODO auto-generated Method Stub - //Role role = This.loadrolebyroleid (Roleid); the if(Role = =NULL) the Throw NewMyException ("Role not Found", "301"); the theSet<permission> PSet =role.getpermissions (); - the if(PSet = =NULL){ the Throw NewMyException ("Role has no Permissions", "" "); the }94 if(Pset.size ()! = 0) the { theSet<permission> psetcopy =NewHashset<permission>(PSet); the 98 for(Permission p:psetcopy) About { - if(P.getid (). Longvalue () = =Permid.longvalue ()) {101 Pset.remove (p);102 }103 }104 } the 106 107 This. Update (role);108 109 return true; the }111 the @Override113 PublicSet<string>Findaccesstokensbyroleid (Long roleid) { the //TODO auto-generated Method Stub theRole role = This. Loadrolebyroleid (Roleid); the 117 if(Role = =NULL)118 return NULL;119 - returnRole.getaccesstokens ();121 }122 123 @Override124 Public Booleandeleteaccesstokenbyroleidandtokenstring (Long Roleid, theString Accesstoken)throwsMyException {126 //TODO auto-generated Method Stub127Role role = This. Loadrolebyroleid (Roleid); - 129 if(Role = =NULL) the Throw NewMyException ("Role not found!", "301");131 theSet<string> Accesstokens =Role.getaccesstokens ();133 Accesstokens.remove (accesstoken);134 This. Update (role);135 136 return true;137 }138 139 @Override $ PublicRole Loadrolebyroleid (Long roleid) {141String sql = "Select R from Role r INNER join fetch R.owneruser INNER JOIN fetch r.permissions INNER JOIN fetch R.accessto Kens WHERE r.id =: id ";142list<role> list = This. GetSession (). CreateQuery (SQL). Setlong ("id", Roleid). List ();143 144 if(List = =NULL|| List.size () = = 0){145String sql2 = "Select R from Role r INNER join fetch R.owneruser INNER JOIN fetch r.accesstokens WHERE r.id =: id";146List<role> List2 = This. GetSession (). CreateQuery (SQL2). Setlong ("id", Roleid). List ();147 148 if(List2 = =NULL|| List2.size () = = 0){149 return NULL; Max }151 the returnList2.get (0);153 }154 155 returnList.get (0);156 }157}
Common wording of DAO layer implementation classes