SSH電力項目三

來源:互聯網
上載者:User

標籤:comm   hashmap   ima   let   update   code   type   div   find   

底層方法封裝:模糊查詢,姓張的人

TestService.java

package junit;public class TestService {    @Test    public void save(){    ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");    IElecTextService elecTextService = (IElecTextService) ac.getBean(IElecTextService.SERVICE_NAME);        ElecText e = new ElecText();    e.setTextName("abbbc");    e.setTextDate(new Date());    e.setTextRemark("deeef");    elecTextService.saveElecText(e);        }
// @Test
public void findCollectionByConditionNopage(){ ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); IElecTextService elecTextService = (IElecTextService) ac.getBean(IElecTextService.SERVICE_NAME); ElecText elecText = new ElecText(); elecText.setTextName("張"); elecText.setTextRemark("張"); List<ElecText> list = elecTextService.findCollectionByConditionNopage(elecText); if(list != null && list.size() > 0){ for(ElecText text:list){ System.out.println(text.getTextName() + "+"+ text.getTextRemark()); } } }}

ElecTextService.java  介面:

package com.itheima.elec.service;public interface IElecTextService {    public static final String SERVICE_NAME="com.itheima.elec.service.impl.ElecTextServiceImpl";    void saveElecText(ElecText elecText);    List<ElecText> findCollectionByConditionNopage(ElecText elecText);}

 

ElecTextServiceImpl.java 介面

package com.itheima.elec.service.impl;//事務控制:spring的聲明交易處理,在service層添加@Transactional@Service(IElecTextService.SERVICE_NAME)@Transactional(readOnly=true)public class ElecTextServiceImpl implements IElecTextService {    @Resource(name=IElecTextDao.SERVICE_NAME)    IElecTextDao elecTextDao;        @Transactional(readOnly=false)    public void saveElecText(ElecText elecText) {        // TODO Auto-generated method stub            elecTextDao.save(elecText);    }    @Override    public List<ElecText> findCollectionByConditionNopage(ElecText elecText) {        //查詢條件        String condition = "";        //查詢條件對應的參數        List<Object> paramsList = new ArrayList<Object>();//        if(elecText.getTextName() != null && !elecText.getTextName().equals("")){//                condition += " and o.textName like ?";//        }        if(StringUtils.isNotBlank(elecText.getTextName())){            condition += "  and o.textName like  ?";            paramsList.add("%" + elecText.getTextName() + "%");        }                if(StringUtils.isNotBlank(elecText.getTextRemark())){            condition += "  and o.textRemark like  ?";            paramsList.add("%" + elecText.getTextRemark() + "%");        }        //傳遞可變參數        Object [] params = paramsList.toArray();        //排序        Map<String, String> orderby = new LinkedHashMap<String,String>();                orderby.put("o.textDate", "asc");                orderby.put("o.textName", "desc");      //查詢      List<ElecText> list = elecTextDao.findCollectionByConditionNoPage(condition,params,orderby);      return list;    }}

 

ICommonDao.java

package com.itheima.elec.dao;public interface ICommonDao<T> {    void save(T entity);    void update(T entity);    T findObjectById(Serializable id);    void deleteObjectByIds(Serializable... ids);    void deleteObjectByCollection(List<T> list);    List<T> findCollectionByConditionNoPage(String condition, Object[] params, Map<String, String> orderby    );}

 

ICommonDaoImpl.java

package com.itheima.elec.dao.impl;public class CommonDaoImpl<T>  extends HibernateDaoSupport implements ICommonDao<T> {    //泛型轉化    Class entityClass = TUtils.getActualType(this.getClass());    /**     * 如何來實現這個save方法:通過HibernateDaoSupport 來實現,需要注入sessionFactory     */    @Resource    public void setDi(SessionFactory sessionFactory){        this.setSessionFactory(sessionFactory);    }    @Override    public void save(T entity) {        this.getHibernateTemplate().save(entity);    }    public void update(T entity){        this.getHibernateTemplate().update(entity);    }    @Override    public T findObjectById(Serializable id) {//        Class entityClass = TUtils.getActualType(this.getClass());        return (T) this.getHibernateTemplate().get(entityClass, id);   //entityClass 此處需要類型    }    @Override    public void deleteObjectByIds(Serializable... ids) {        if(ids != null && ids.length > 0){            for(Serializable id:ids){                Object entity = this.findObjectById(id);                this.getHibernateTemplate().delete(entity);            }        }//        this.getHibernateTemplate().delete(entity);    }    @Override    public void deleteObjectByCollection(List<T> list) {            this.getHibernateTemplate().deleteAll(list);    }    @Override    /**    這裡1=1的目的是方便在Service層拼裝sql或者hql語句,串連統一使用and     * SELECT o FROM ElecText o WHERE 1=1             #Dao層填寫            AND o.textName LIKE ‘%張%‘                  #Service拼裝            AND o.textRemark LIKE ‘%張%‘                #Service拼裝            ORDER BY o.textDate ASC,o.textName desc   #Service拼裝     */    public List<T> findCollectionByConditionNoPage(String condition, Object[] params, Map<String, String> orderby) {        //hql語句        String hql = "from " + entityClass.getSimpleName() + "  o where 1 = 1";        //將Map集合中存放的欄位排序,組織成ORDER BY o.textDate ASC, o.textName Desc        String orderByCondition = this.orderByHql(orderby);        //添加查詢條件        String finalHql = hql + condition + orderByCondition;        //查詢,執行sql語句        //方法一:        List<T> list = this.getHibernateTemplate().find(finalHql, params);                return list;    }    private String orderByHql(Map<String, String> orderby) {        StringBuffer buffer = new StringBuffer("");        if(orderby != null && orderby.size() > 0){            buffer.append(" ORDER BY ");            for(Map.Entry<String, String> map:orderby.entrySet()){                buffer.append(map.getKey() + " " + map.getValue() + ",");            }            //刪除最後一個逗號            buffer.deleteCharAt(buffer.length() - 1);        }        return buffer.toString();    }}

 

2017.1.11  11:58

 

SSH電力項目三

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.