SSH:查詢

北京Java青年發表於2019-06-13

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());
	}

}

在這裡插入圖片描述

相關文章