|
| 1 | +package com.nbs.biz.dao; |
| 2 | + |
| 3 | +import com.nbs.biz.model.BasicModel; |
| 4 | +import org.apache.ibatis.exceptions.PersistenceException; |
| 5 | +import org.apache.ibatis.session.SqlSession; |
| 6 | + |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +/** |
| 12 | + * @Package : com.nbs.biz.dao |
| 13 | + * @Description : <p></p> |
| 14 | + * @Author : lambor.c |
| 15 | + * @Date : 2018/6/23-21:08 |
| 16 | + * Copyright (c) 2018, NBS , lambor.c<[email protected]>. |
| 17 | + * All rights reserved. |
| 18 | + */ |
| 19 | +public class BasicDao { |
| 20 | + protected SqlSession session; |
| 21 | + private String className; |
| 22 | + |
| 23 | + |
| 24 | + public BasicDao(SqlSession session, Class clazz) |
| 25 | + { |
| 26 | + this.session = session; |
| 27 | + this.className = clazz.getName(); |
| 28 | + } |
| 29 | + |
| 30 | + public int insert(BasicModel model) |
| 31 | + { |
| 32 | + return session.insert(className + ".insert", model); |
| 33 | + } |
| 34 | + |
| 35 | + public List findAll() |
| 36 | + { |
| 37 | + //return session.selectList(className + ".findAll"); |
| 38 | + return _findAll(0); |
| 39 | + } |
| 40 | + |
| 41 | + private List _findAll(int time) |
| 42 | + { |
| 43 | + if (time > 10) |
| 44 | + { |
| 45 | + System.out.println("查询到 BasicModelList 对象失败次数>10,放弃查询"); |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + try |
| 50 | + { |
| 51 | + return session.selectList(className + ".findAll"); |
| 52 | + } catch (PersistenceException exception) |
| 53 | + { |
| 54 | + System.out.println("没有查询到 BasicModelList 对象,继续查询"); |
| 55 | + return _findAll(++time); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public BasicModel findById(String id) |
| 60 | + { |
| 61 | + return _findById(id, 0); |
| 62 | + } |
| 63 | + |
| 64 | + private BasicModel _findById(String id, int time) |
| 65 | + { |
| 66 | + if (time > 10) |
| 67 | + { |
| 68 | + System.out.println("查询到 BasicModel 对象失败次数>10,放弃查询"); |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + try |
| 73 | + { |
| 74 | + return (BasicModel) session.selectOne(className + ".findById", id); |
| 75 | + } catch (PersistenceException exception) |
| 76 | + { |
| 77 | + System.out.println("没有查询到 BasicModel 对象,继续查询"); |
| 78 | + return _findById(id, ++time); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public List find(String field, Object val) |
| 83 | + { |
| 84 | + Map map = new HashMap(); |
| 85 | + map.put("field", field); |
| 86 | + |
| 87 | + if (val instanceof String) |
| 88 | + { |
| 89 | + map.put("val", "'" + val + "'"); |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + map.put("val", val); |
| 94 | + } |
| 95 | + |
| 96 | + return session.selectList(className + ".find", map); |
| 97 | + } |
| 98 | + |
| 99 | + public int delete(String id) |
| 100 | + { |
| 101 | + return session.delete(className + ".delete", id); |
| 102 | + } |
| 103 | + |
| 104 | + public int deleteAll() |
| 105 | + { |
| 106 | + return session.delete(className + ".deleteAll"); |
| 107 | + } |
| 108 | + |
| 109 | + public int update(BasicModel model) |
| 110 | + { |
| 111 | + return session.update(className + ".update", model); |
| 112 | + } |
| 113 | + |
| 114 | + public int updateIgnoreNull(BasicModel model) |
| 115 | + { |
| 116 | + return session.update(className + ".updateIgnoreNull", model); |
| 117 | + } |
| 118 | + |
| 119 | + public List updateField(String field, Object val) |
| 120 | + { |
| 121 | + Map map = new HashMap(); |
| 122 | + map.put("field", field); |
| 123 | + |
| 124 | + if (val instanceof String || val instanceof Boolean) |
| 125 | + { |
| 126 | + map.put("val", "'" + val + "'"); |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + map.put("val", val); |
| 131 | + } |
| 132 | + |
| 133 | + return session.selectList(className + ".updateField", map); |
| 134 | + } |
| 135 | + |
| 136 | + public int count() |
| 137 | + { |
| 138 | + return (int) session.selectOne(className + ".count"); |
| 139 | + } |
| 140 | + |
| 141 | + public boolean exist(String id) |
| 142 | + { |
| 143 | + return ((int) (session.selectOne(className + ".exist", id))) > 0; |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments