SSH:查询

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值