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
- SSH:查询
- SSH查询-lazy机制
- ssh实现查询分页
- ssh实现分页查询
- SSH 多关键字查询
- 使用ssh注解查询
- ssh分页+模糊查询
- SSH-------分页查询
- ssh查询及其安装
- SSH-CRUD-查询
- 多条件查询---ssh版本
- ssh分页查询和显示
- SSH分页(Hibernate分页查询)
- SSH应用总结–查询
- JAVA SSH ----Hibernate索引查询
- 【SSH】使用HQL查询数据
- ssh中的分页查询商品
- SSH+JPA查询两个数据库
- C语言问题:这个值不是100么,怎么变成15
- f0909的3个问题
- Expression: _CrtIsValidHeapPointer(pUserData)
- _BLOCK_TYPE_IS_VALID(pHead->nBlockUse
- 字节对齐(转)
- SSH:查询
- c/c++转义字符
- Linux-3.x离线安装openssh
- 问题三:键盘缓冲区残余信息问题
- 简单的 学生管理系统
- abnormal program termination
- C++学习点点点滴滴---std::setw(n)的用法
- NOIP2014八校联考第3场第1试10.4】反抗希碧拉系统续(regex)
- Codeforces Round #251 (Div. 2)B