1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 |
/** * (#)EntityDAO.java 1.0 2008-6-7 上午09:25:37 * */ package com.keyi.util.dao; import java.io.Serializable; import java.util.List; public interface EntityDAO<T> { /** * 根據主鍵查找對象 * * @param id * 主鍵值 * @return 對象實體 */ T findByPrimarykey(Serializable id); /** * 新增對象到數據庫 * * @param o * 對象實體 */ void insert(T o); /** * 更新對象實體到數據庫 * * @param o * 對象實體 */ void update(T o); /** * 根據主鍵刪除對象 * * @param id * 主鍵值 */ void deleteByPrimarykey(Serializable id); /** * 更新對象信息 * * @param statementId * sql語句名稱後綴 * @param parameters * sql參數 */ void update( String statementId, Object parameters); /** * sql查詢單個對象 * * @param statementId * sql語句名稱後綴 * @param parameters * sql參數 * @return 查詢結果 */ T queryForObject( String statementId, Object parameters); /** * sql查詢列表 * * @param statementId * sql語句名稱後綴 * @param parameters * sql參數 * @return 查詢結果 */ List<T> queryForList( String statementId, Object parameters); } IbatisEntityDao 類內容以下: /** * (#)IbatisEntityDao.java 1.0 2008-6-7 上午09:24:32 * */ package com.keyi.util.dao; import java.io.Serializable; import java.util.List; import java.util.Map; import org.apache.commons.lang.StringUtils; import com.keyi.util.page.Page; @SuppressWarnings( "unchecked") public class IbatisEntityDao<T> extends IbatisGenericDao implements EntityDAO<T> { /** * DAO所管理的Entity類型. */ protected Class<T> entityClass; protected String primaryKeyName; /** * 在構造函數中將泛型T.class賦給entityClass. */ @SuppressWarnings( "unchecked") public IbatisEntityDao() { entityClass = GenericsUtils.getSuperClassGenricType(getClass()); } /** * 根據ID獲取對象. */ public T findByPrimarykey(Serializable id) { return get(getEntityClass(), id); } /** * 取得entityClass. <p/> JDK1.4不支持泛型的子類能夠拋開Class<T> entityClass,重載此函數達到相同效果。 */ protected Class<T> getEntityClass() { return entityClass; } public String getIdName(Class clazz) { return "id"; } /** * 分頁查詢. */ public Page pagedQuery(Map parameterObject, int start, int limit) { return pagedQuery(getEntityClass(), parameterObject, start, limit); } /** * 分頁查詢. */ public Page pagedQuery(Map parameterObject, int start, int limit, String countSqlId, String pageQuerySqlId) { if (StringUtils.isNotBlank(pageQuerySqlId)) return pagedQuery(getEntityClass(), parameterObject, start, limit, countSqlId, pageQuerySqlId); else { return pagedQuery(getEntityClass(), parameterObject, start, limit); } } /** * 根據ID移除對象. */ public void deleteByPrimarykey(Serializable id) { removeById(getEntityClass(), id); } /** * 保存對象. 爲了實現IEntityDao 我在內部使用了insert和upate 2個方法. */ public void insert(T o) { super._insert(o); } public void setPrimaryKeyName( String primaryKeyName) { this.primaryKeyName = primaryKeyName; } public List<T> queryForList( String statementId, Object parameters) { return super.queryForList(getEntityClass(), statementId, parameters); } public T queryForObject( String statementId, Object parameters) { return super.queryForObject(getEntityClass(), statementId, parameters); } public void update( String statementId, Object parameters) { super.update(getEntityClass(), statementId, parameters); } public void update(T o) { super._update(o); } } IbatisGenericDao 類內容以下: /** * (#)IbatisGenericDao.java 1.0 2008-6-7 上午09:20:13 * */ package com.keyi.util.dao; import java.io.Serializable; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import org.springframework.util.Assert; import com.keyi.util.page.Page; @SuppressWarnings( "unchecked") public class IbatisGenericDao extends SqlMapClientDaoSupport { public static final String POSTFIX_INSERT = ".insert"; public static final String POSTFIX_UPDATE = ".update"; public static final String POSTFIX_DELETE = ".delete"; public static final String POSTFIX_DELETE_PRIAMARYKEY = ".deleteByPrimaryKey"; public static final String POSTFIX_SELECT = ".select"; public static final String POSTFIX_GETALL = ".getAll"; public static final String POSTFIX_SELECTMAP = ".selectByMap"; public static final String POSTFIX_SELECTSQL = ".selectBySql"; public static final String POSTFIX_COUNT = ".count"; public static final String POSTFIX_QUERY = ".query"; /** * 根據ID獲取對象 */ public <T> T get(Class<T> entityClass, Serializable id) { T o = (T) getSqlMapClientTemplate().queryForObject( getStatementId(entityClass, IbatisGenericDao.POSTFIX_SELECT), id); return o; } /** * 新增對象 */ public void _insert(Object o) { getSqlMapClientTemplate().insert( getStatementId(o.getClass(), IbatisGenericDao.POSTFIX_INSERT), o); } /** * 保存對象 */ public void _update(Object o) { getSqlMapClientTemplate().update( getStatementId(o.getClass(), IbatisGenericDao.POSTFIX_UPDATE), o); } /** * 根據ID刪除對象 */ public <T> void removeById(Class<T> entityClass, Serializable id) { getSqlMapClientTemplate().delete( getStatementId(entityClass, IbatisGenericDao.POSTFIX_DELETE_PRIAMARYKEY), id); } /** * 分頁查詢函數,使用PaginatedList. * * @param start *@param limit @return 含17117717記錄數和當前頁數據的Page對象. */ public Page pagedQuery(Class entityClass, Map parameterObject, int start, int limit) { Assert.isTrue(start >= 0, "pageNo should start from 0"); // 計算總數 Integer totalCount = (Integer) getSqlMapClientTemplate() .queryForObject( getStatementId(entityClass, IbatisGenericDao.POSTFIX_COUNT), parameterObject); // 若是沒有數據則返回Empty Page Assert.notNull(totalCount, "totalCount Error"); if (totalCount.intValue() == 0) { return new Page(); } List list; int totalPageCount = 0; int startIndex = 0; // 若是pageSize小於0,則返回全部數捄1177,等同於getAll if (limit > 0) { // 計算頁數 totalPageCount = (totalCount / limit); totalPageCount += ((totalCount % limit) > 0) ? 1 : 0; // 計算skip數量 if (totalCount > start) { startIndex = start; } else { startIndex = (totalPageCount - 1) * limit; } if (parameterObject == null) parameterObject = new HashMap(); parameterObject.put( "startIndex", startIndex); parameterObject.put( "endIndex", limit); list = getSqlMapClientTemplate() .queryForList( getStatementId(entityClass, IbatisGenericDao.POSTFIX_QUERY), parameterObject); } else { list = getSqlMapClientTemplate() .queryForList( getStatementId(entityClass, IbatisGenericDao.POSTFIX_QUERY), parameterObject); } return new Page(startIndex, totalCount, limit, list); } /** * 分頁查詢函數,使用PaginatedList. * * @param start *@param limit @return 含17117717記錄數和當前頁數據的Page對象. */ public Page pagedQuery(Class entityClass, Map parameterObject, int start, int limit, String countSqlId, String pageQuerySqlId) { Assert.isTrue(start >= 0, "pageNo should start from 0"); // 計算總數 Integer totalCount = (Integer) getSqlMapClientTemplate() .queryForObject( getStatementId(entityClass, countSqlId), parameterObject); // 若是沒有數據則返回Empty Page Assert.notNull(totalCount, "totalCount Error"); if (totalCount.intValue() == 0) { return new Page(); } List list; int totalPageCount = 0; int startIndex = 0; // 若是pageSize小於0,則返回全部數捄1177,等同於getAll if (limit > 0) { // 計算頁數 totalPageCount = (totalCount / limit); totalPageCount += ((totalCount % limit) > 0) ? 1 : 0; // 計算skip數量 if (totalCount >= start) { startIndex = start; } else { startIndex = (totalPageCount - 1) * limit; } if (parameterObject == null) parameterObject = new HashMap(); parameterObject.put( "startIndex", startIndex); parameterObject.put( "endIndex", limit); list = getSqlMapClientTemplate() .queryForList( getStatementId(entityClass, pageQuerySqlId), parameterObject); } else { list = getSqlMapClientTemplate() .queryForList( getStatementId(entityClass, pageQuerySqlId), parameterObject); } return new Page(startIndex, totalCount, limit, list); } /** * get statement id in SQL Map file * * @param entityClass * entity class * @param suffix * suffix * @return statement id */ private String getStatementId(Class entityClass, String suffix) { String className = entityClass.getName(); String shortName = className.replace(entityClass.getPackage().getName() + ".", ""); return shortName + suffix; } public <T> List<T> queryForList(Class<T> entityClass, String statementId, Object parameters) { return getSqlMapClientTemplate().queryForList( getStatementId(entityClass, statementId), parameters); } public <T> T queryForObject(Class<T> entityClass, String statementId, Object parameters) { return (T) getSqlMapClientTemplate().queryForObject( getStatementId(entityClass, statementId), parameters); } public void update(Class entityClass, String statementId, Object parameters) { getSqlMapClientTemplate().update( getStatementId(entityClass, statementId), parameters); } } GenericsUtils 類內容以下: /** * (#)GenericsUtils.java 1.0 2008-6-7 上午09:26:43 * */ package com.keyi.util.dao; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @SuppressWarnings( "unchecked") public class GenericsUtils { private static final Log log = LogFactory.getLog(GenericsUtils. class); private GenericsUtils() { } public static Class getSuperClassGenricType(Class clazz) { return getSuperClassGenricType(clazz, 0); } public static Class getSuperClassGenricType(Class clazz, int index) { Type genType = clazz.getGenericSuperclass(); if (!(genType instanceof ParameterizedType)) { log.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType"); return Object. class; } Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); if (index >= params.length || index < 0) { log.warn( "Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: " + params.length); return Object. class; } if (!(params[index] instanceof Class)) { log.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter"); return Object. class; } return (Class) params[index]; } } 具體應用以下: public interface UserDAO extends EntityDAO<User> { } public class IbatisUserDAO extends IbatisEntityDao<User> implements UserDAO { } public interface NewsDAO extends EntityDAO<News> { } public class IbatisNewsDAO extends IbatisEntityDao<News> implements NewsDAO { } 樓上的解釋一下。爲什麼不能用分頁呢? /** * (#)Page.java 1.0 2008-6-7 上午09:21:03 * */ import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; /** * 分頁對象. 包含當前頁數據及分頁信息如總記錄數. * * @author 狂狼爪哇 */ @SuppressWarnings( "serial") public class Page implements Serializable { public static int DEFAULT_PAGE_SIZE = 10; private int pageSize = Page.DEFAULT_PAGE_SIZE; // 每頁的記錄數 private int start; // 當前頁第一條數據在List中的位置,從0開始 private Collection data; // 當前頁中存放的記錄,類型通常爲List private int totalCount; // 總記錄數 /** * 構造方法,只構造空頁. */ public Page() { this( 0, 0, Page.DEFAULT_PAGE_SIZE, new ArrayList()); } /** * 默認構造方法. * * @param start * 本頁數據在數據庫中的起始位置 * @param totalSize * 數據庫中總記錄條數 * @param pageSize * 本頁容量 * @param data * 本頁包含的數據 */ public Page( int start, int totalSize, int pageSize, Collection data) { this.pageSize = pageSize; this.start = start; totalCount = totalSize; this.data = data; } /** * 取總記錄數. */ public int getTotalCount() { return totalCount; } /** * 取總頁數. */ public int getTotalPageCount() { if (totalCount % pageSize == 0) return totalCount / pageSize; else return totalCount / pageSize + 1; } /** * 取每頁數據容量. */ public int getPageSize() { return pageSize; } /** * 取當前頁中的記錄. */ public Collection getResult() { return data; } /** * 取該頁當前頁碼,頁碼從1開始. */ public int getCurrentPageNo() { return start / pageSize + 1; } /** * 該頁是否有下一頁. */ public boolean hasNextPage() { return getCurrentPageNo() < getTotalPageCount() - 1; } /** * 該頁是否有上一頁. */ public boolean hasPreviousPage() { return getCurrentPageNo() > 1; } /** * 獲取任一頁第一條數據在數據集的位置,每頁條數使用默認值. * * @see #getStartOfPage(int,int) */ protected static int getStartOfPage( int pageNo) { return Page.getStartOfPage(pageNo, Page.DEFAULT_PAGE_SIZE); } /** * 獲取任一頁第一條數據在數據集的位置. * * @param pageNo * 從1開始的頁號 * @param pageSize * 每頁記錄條數 * @return 該頁第一條數據 */ public static int getStartOfPage( int pageNo, int pageSize) { return (pageNo - 1) * pageSize; } public int getCurrentPage() { return this.getCurrentPageNo(); } public int getTotalPage() { return this.getTotalPageCount(); } public int getStart() { return this.start; } } domain 類: import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; import com.czuan.util.user.IUser; /** * 功能:用戶信息 * * @author 狂狼爪哇 * @version 1.0 * @JDK 5 */ public class User implements IUser { private Long id; /* * 用戶名 */ private String loginName; /* * 密碼 */ private String passWord; /* * 問題 */ private String question; /* * 答案 */ private String result; /* * 郵箱 */ private String email; /* * 電話號碼 */ private String phone; /* * 手機號碼 */ private String tel; /* * 祥細地扯 */ private String address; /** * 角色 */ private Long role; /* * 傳真 */ private String fax; /* * qq/MSN */ private String qqMsn; /* * 真實姓名 */ private String name; /* * 身份證號碼 */ private String card; /* * 郵證號碼 */ private String postalcode; /** * 註冊時間 */ private Date registerDate; /* * 備註 */ private String bak; public String getAddress() { return address; } public void setAddress( String address) { this.address = address; } public String getBak() { return bak; } public void setBak( String bak) { this.bak = bak; } public String getCard() { return card; } public void setCard( String card) { this.card = card; } public String getEmail() { return email; } public void setEmail( String email) { this.email = email; } public String getFax() { return fax; } public void setFax( String fax) { this.fax = fax; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getLoginName() { return loginName; } public void setLoginName( String loginName) { this.loginName = loginName; } public String getName() { return name; } public void setName( String name) { this.name = name; } public String getPassWord() { return passWord; } public void setPassWord( String passWord) { this.passWord = passWord; } public String getPhone() { return phone; } public void setPhone( String phone) { this.phone = phone; } public String getPostalcode() { return postalcode; } public void setPostalcode( String postalcode) { this.postalcode = postalcode; } public String getQqMsn() { return qqMsn; } public void setQqMsn( String qqMsn) { this.qqMsn = qqMsn; } public String getQuestion() { return question; } public void setQuestion( String question) { this.question = question; } public String getResult() { return result; } public void setResult( String result) { this.result = result; } public Long getRole() { return role; } public void setRole(Long role) { this.role = role; } public String getTel() { return tel; } public void setTel( String tel) { this.tel = tel; } @SuppressWarnings( "unchecked") public Map transfer2Map() { Map parameters = new HashMap(); if(StringUtils.isNotBlank( this.getLoginName())) parameters.put( "loginName", "%" + StringEscapeUtils.escapeSql( this.getLoginName().trim()) + "%"); if(StringUtils.isNotBlank( this.getPassWord())) parameters.put( "passWord", "%" + StringEscapeUtils.escapeSql( this.getPassWord().trim()) + "%"); if(StringUtils.isNotBlank( this.getQuestion())) parameters.put( "question", "%" + StringEscapeUtils.escapeSql( this.getQuestion().trim()) + "%"); if(StringUtils.isNotBlank( this.getResult())) parameters.put( "result", "%" + StringEscapeUtils.escapeSql( this.getResult().trim()) + "%"); if(StringUtils.isNotBlank( this.getEmail())) parameters.put( "email", "%" + StringEscapeUtils.escapeSql( this.getEmail().trim()) + "%"); if(StringUtils.isNotBlank( this.getPhone())) parameters.put( "phone", "%" + StringEscapeUtils.escapeSql( this.getPhone().trim()) + "%"); if(StringUtils.isNotBlank( this.getTel())) parameters.put( "tel", "%" + StringEscapeUtils.escapeSql( this.getTel().trim()) + "%"); if(StringUtils.isNotBlank( this.getFax())) parameters.put( "fax", "%" + StringEscapeUtils.escapeSql( this.getFax().trim()) + "%"); if(StringUtils.isNotBlank( this.getQqMsn())) parameters.put( "qqMsn", "%" + StringEscapeUtils.escapeSql( this.getQqMsn().trim()) + "%"); if(StringUtils.isNotBlank( this.getName())) parameters.put( "name", "%" + StringEscapeUtils.escapeSql( this.getName().trim()) + "%"); if(StringUtils.isNotBlank( this.getCard())) parameters.put( "card", "%" + StringEscapeUtils.escapeSql( this.getCard().trim()) + "%"); if(StringUtils.isNotBlank( this.getPostalcode())) parameters.put( "postalcode", "%" + StringEscapeUtils.escapeSql( this.getPostalcode()).trim() + "%"); parameters.put( "role", this.getRole()); return parameters; } public Date getRegisterDate() { return registerDate; } public void setRegisterDate(Date registerDate) { this.registerDate = registerDate; } } |