首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

mybatis泛型Dao参照二

2013-07-08 
mybatis泛型Dao参考二? return o?}?/**? * 获取全部对象? * @throws SQLException?? */?public T List

mybatis泛型Dao参考二
? return o;
?}?/**
? * 获取全部对象
? * @throws SQLException?
? */
?public <T> List<T> getAll(Class<T> entityClass) throws SQLException {
? return getSqlMapClient().queryForList(entityClass.getName() + POSTFIX_SELECT, null);
?}?/**
? * 新增对象
? * @throws SQLException?
? */
?public void insert(Object o) throws SQLException {
? getSqlMapClient().insert(o.getClass().getName() + POSTFIX_INSERT, o);
?}?/**
? * 保存对象
? * @throws SQLException?
? */
?public int update(Object o) throws SQLException {
? return getSqlMapClient().update(o.getClass().getName() + POSTFIX_UPDATE, o);
?}?/**
? * 删除对象
? * @throws SQLException?
? */
?public int remove(Object o) throws SQLException {
? return getSqlMapClient().delete(o.getClass().getName() + POSTFIX_DELETE, o);
?}?/**
? * 根据ID删除对象
? * @throws SQLException?
? */
?public <T> int removeById(Class<T> entityClass, Serializable id) throws SQLException {
? return getSqlMapClient().delete(entityClass.getName() + POSTFIX_DELETE_PRIAMARYKEY, id);
?}?/**
? * map查询.
? *?
? * @param map
? *??????????? 包含各种属性的查询
? * @throws SQLException?
? */
?public <T> List<T> find(Class<T> entityClass, Map<String, Object> map) throws SQLException {
? if (map == null)
?? return this.getSqlMapClient().queryForList(entityClass.getName() + POSTFIX_SELECT, null);
? else {
?? map.put("findBy", "True");
?? return this.getSqlMapClient()
???? .queryForList(entityClass.getName() + POSTFIX_SELECTMAP, map);
? }
?}?/**
? * sql 查询.
? *?
? * @param sql
? *??????????? 直接sql的语句(需要防止注入式攻击)
? * @throws SQLException?
? */
?public <T> List<T> find(Class<T> entityClass, String sql) throws SQLException {
? Assert.hasText(sql);
? if (StringUtils.isEmpty(sql))
?? return this.getSqlMapClient().queryForList(entityClass.getName() + POSTFIX_SELECT, null);
? else
?? return this.getSqlMapClient()
???? .queryForList(entityClass.getName() + POSTFIX_SELECTSQL, sql);
?}?/**
? * 根据属性名和属性值查询对象.
? *?
? * @return 符合条件的对象列表
? * @throws SQLException?
? */
?public <T> List<T> findBy(Class<T> entityClass, String name, Object value) throws SQLException {
? Assert.hasText(name);
? Map<String, Object> map = new HashMap<String, Object>();
? map.put(name, value);
? return find(entityClass, map);
?}?/**
? * 根据属性名和属性值查询对象.
? *?
? * @return 符合条件的唯一对象
? */
?public <T> T findUniqueBy(Class<T> entityClass, String name, Object value) {
? Assert.hasText(name);
? Map<String, Object> map = new HashMap<String, Object>();
? try {
?? PropertyUtils.getProperty(entityClass.newInstance(), name);
?? map.put(name, value);
?? map.put("findUniqueBy", "True");
?? return (T) getSqlMapClient().queryForObject(entityClass.getName() + POSTFIX_SELECTMAP,
???? map);
? } catch (Exception e) {
?? logger.error("Error when propertie on entity," + e.getMessage(), e.getCause());
?? return null;
? }?}?/**
? * 根据属性名和属性值以Like AnyWhere方式查询对象.
? * @throws SQLException?
? */
?public <T> List<T> findByLike(Class<T> entityClass, String name, String value) throws SQLException {
? Assert.hasText(name);
? Map<String, Object> map = new HashMap<String, Object>();
? map.put(name, value);
? map.put("findLikeBy", "True");
? return getSqlMapClient().queryForList(entityClass.getName() + POSTFIX_SELECTMAP, map);?}?/**
? * 判断对象某些属性的值在数据库中不存在重复
? *?
? * @param tableName
? *??????????? 数据表名字
? * @param names
? *??????????? 在POJO里不能重复的属性列表,以逗号分割 如"name,loginid,password" <br>
? *??????????? FIXME how about in different schema?
? */
?public boolean isNotUnique(Object entity, String tableName, String names) {
? try {
?? String primarykey;
?? Connection con = getSqlMapClient().getCurrentConnection();
?? ResultSet dbMetaData = con.getMetaData().getPrimaryKeys(con.getCatalog(), null, tableName);
?? dbMetaData.next();
?? if (dbMetaData.getRow() > 0) {
??? primarykey = dbMetaData.getString(4);
??? if (names.indexOf(primarykey) > -1)
???? return false;
?? } else {
??? return true;
?? }? } catch (SQLException e) {
?? logger.error(e.getMessage(), e);
?? return false;
? }
? return false;
?}?/**
? * 分页查询函数,使用PaginatedList.
? *?
? * @param pageNo
? *??????????? 页号,从0开始.
? * @throws SQLException
? */
?@SuppressWarnings("rawtypes")
?public DataPage pagedQuery(String sqlName, HashMap<String, Object> hashMap, Integer pageNo, Integer pageSize)
?? throws SQLException {? if (pageNo == null || pageSize == null) {
?? List list = getSqlMapClient().queryForList(sqlName, hashMap);
?? if (list == null || list.size() == 0) {
??? return new DataPage();
?? } else {
??? return new DataPage(0, list.size(), list.size(), list);
?? }
? } else {
?? Assert.hasText(sqlName);
?? Assert.isTrue(pageNo >= 1, "pageNo should start from 1");
?? // Count查询
?? Integer totalCount = (Integer) getSqlMapClient().queryForObject(sqlName + ".Count", hashMap);?? if (totalCount < 1) {
??? return new DataPage();
?? }?? // 实际查询返回分页对象
?? int startIndex = DataPage.getStartOfPage(pageNo, pageSize);
?? hashMap.put("startIndex", startIndex);
?? hashMap.put("pageSize", pageSize);
?? List list = getSqlMapClient().queryForList(sqlName, hashMap);?? return new DataPage(startIndex, totalCount, pageSize, list);
? }
?}?public String getMappedSQL(String sqlName) {
? String sql = null;? SqlMapClientImpl sqlmap = (SqlMapClientImpl) getSqlMapClient();? MappedStatement stmt = sqlmap.getMappedStatement(sqlName);
? StaticSql staticSql = (StaticSql) stmt.getSql();
? sql = staticSql.getSql(null, null);
? return sql;
?}
}?以上两种方式可根据项目的需求自由选择,但是需要注意的是,无论是哪种方式,都要将配置文件和dao具体实现中所需参数保持一致

热点排行