SSH:查询

来源:互联网 发布:瑞典隐身战斗机知乎 编辑:程序博客网 时间:2024/04/30 00:56

1.dao/EmpDao类

package dao;import java.util.List;import entity.Emp;public interface EmpDao {    public List<Emp> findAllEmp();    public List<Emp> findEmpByCondition(Emp e);    public List<Emp> findEmpByNameParam(Emp e);    public List<Emp> findEmpByExemple(Emp e);    public Emp findEmpById(int id);}

2.dao.impl/EmpDaoImpl

package dao.impl;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import entity.Emp;import dao.*;public class EmpDaoImpl extends HibernateDaoSupport implements EmpDao {//参数数组    @SuppressWarnings("unchecked")    @Override    public List<Emp> findAllEmp() {        List<Emp> elist = this.getHibernateTemplate().find(                "from Emp where empno=? and ename=?",                new Object[] { 7788, "SCOTT" });        return elist;    }//对象作为参数数组    @Override    public List<Emp> findEmpByCondition(Emp e) {        @SuppressWarnings("unchecked")        List<Emp> elist = this.getHibernateTemplate().find(                "from Emp where empno=? and ename=?",                new Object[] {e.getEmpno(), e.getEname() });        return elist;    }//命名查询    @SuppressWarnings("unchecked")    @Override    public List<Emp> findEmpByNameParam(Emp e) {        @SuppressWarnings("unchecked")        String param[]={"empno","ename"};        Object value[]={e.getEmpno(),e.getEname()};        List<Emp> elist = this.getHibernateTemplate().findByNamedParam("from Emp where empno=:empno and ename=:ename", param, value);               return elist;    }    //通过对象    @Override    public List<Emp> findEmpByExemple(Emp e) {        List<Emp> elist = this.getHibernateTemplate().findByExample(e);             return elist;    }    //通过对象        @Override        public Emp findEmpById(int id) {            Emp e = this.getHibernateTemplate().get(Emp.class, id);                 return e;        }}

3.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">    <bean id="sessionFactory"        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="configLocation" value="classpath:hibernate.cfg.xml">        </property>    </bean>    <bean id="edi" class="dao.impl.EmpDaoImpl">    <property name="sessionFactory" ref="sessionFactory"></property>    </bean></beans>

4.Test类

package test;import java.util.ArrayList;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import dao.impl.EmpDaoImpl;import entity.Emp;public class Test {    /**     * @param args     */    public static void main(String[] args) {        // 加载spring容器,解析配置文件        ApplicationContext ac = new ClassPathXmlApplicationContext(                "applicationContext.xml");        EmpDaoImpl edi = (EmpDaoImpl) ac.getBean("edi");        // 数组条件        List<Emp> elist = new ArrayList<Emp>();        elist = edi.findAllEmp();        for (Emp e : elist) {            System.out.println(e.getEmpno() + " : " + e.getEname() + " : "                    + e.getMgr());        }        // 对象作为参数        Emp e = new Emp();           e.setEname("SCOTT");        e.setEmpno(7788);        e.setMgr(7566);        List<Emp> elist2 = edi.findEmpByCondition(e);        for (Emp e2 : elist2) {            System.out.println(e2.getEmpno() + " : " + e2.getEname() + " : "                    + e2.getMgr());        }        //命名查询        List<Emp> elist3 = edi.findEmpByNameParam(e);        for (Emp e3 : elist3) {            System.out.println(e3.getEmpno() + " : " + e3.getEname() + " : "                    + e3.getMgr());        }        //通过对象查询(非主键值)        List<Emp> elist4=edi.findEmpByExemple(e);        for (Emp e4 : elist4) {            System.out.println(e4.getEmpno() + " : " + e4.getEname() + " : "                    + e4.getMgr());        }        //通过ID查询        Emp e5=edi.findEmpById(7788);        System.out.println(e5.getEname());    }}
0 0
原创粉丝点击