SSH电力项目三

Posted panzer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSH电力项目三相关的知识,希望对你有一定的参考价值。

底层方法封装:模糊查询,姓张的人

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电力项目三的主要内容,如果未能解决你的问题,请参考以下文章

国家电力项目SSH搭建

C++项目三代码参考(改进版)

SSH电力项目一 搭建Hibernate框架

SSH电力项目九--运行监控首页显示

验证码逆向专栏极验三代四代点选类验证码逆向分析

SourceTree使用SSH克隆码云项目