Spring data 学习日记(二)接口Repository

2018-02-06  本文已影响0人  noddle

准备工作

数据库emp表(oracle里scott/tiger自带的表)

-- Create table
create table EMP
(
  empno    NUMBER(4) not null,
  ename    VARCHAR2(10),
  job      VARCHAR2(9),
  mgr      NUMBER(4),
  hiredate DATE,
  sal      NUMBER(7,2),
  comm     NUMBER(7,2),
  deptno   NUMBER(2)
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Create/Recreate primary, unique and foreign key constraints 
alter table EMP
  add constraint PK_EMP primary key (EMPNO)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
alter table EMP
  add constraint FK_DEPTNO foreign key (DEPTNO)
  references DEPT (DEPTNO);

-------------------------------------------
1   7369    SMITH   CLERK           7902    1980/12/17 星期三  800.00      20
2   7499    ALLEN   SALESMAN    7698    1981/2/20 星期五   1600.00 300.00  30
3   7521    WARD    SALESMAN    7698    1981/2/22 星期日   1250.00 500.00  30
4   7566    JONES   MANAGER 7839    1981/4/2 星期四    2975.00     20
5   7654    MARTIN  SALESMAN    7698    1981/9/28 星期一   1250.00 1400.00 30
6   7698    BLAKE   MANAGER 7839    1981/5/1 星期五    2850.00     30
7   7782    CLARK   MANAGER 7839    1981/6/9 星期二    2450.00     10
8   7788    SCOTT   ANALYST 7566    1987/4/19 星期日   3000.00     20
9   7839    KING    PRESIDENT              1981/11/17 星期二   5000.00     10
10  7844    TURNER  SALESMAN    7698    1981/9/8 星期二    1500.00 0.00    30
11  7876    ADAMS   CLERK         7788      1987/5/23 星期六       1100.00     20
12  7900    JAMES   CLERK        7698   1981/12/3 星期四   950.00      30
13  7902    FORD    ANALYST 7566    1981/12/3 星期四   3000.00     20
14  7934    MILLER  CLERK   7782    1982/1/23 星期六   1300.00     10


创建对应的类

@Entity(name="EMP")
public class Employee {
    @Id
    private  Integer empno;
    private String ename;
    private String job;
    private Integer mgr;
    private Date hiredate;
    private Double sal;
    private Double comm;
    private Integer deptno;
    public Integer getEmpno() {
        return empno;
    }
    public void setEmpno(Integer empno) {
        this.empno = empno;
    }
    public String getEname() {
        return ename;
    }
   public void setEname(String ename) {
        this.ename = ename;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public Integer getMgr() {
        return mgr;
    }
    public void setMgr(Integer mgr) {
        this.mgr = mgr;
    }
    public Date getHiredate() {
        return hiredate;
    }
    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }
    public Double getSal() {
        return sal;
    }
    public void setSal(Double sal) {
        this.sal = sal;
    }
    public Double getComm() {
        return comm;
    }
    public void setComm(Double comm) {
        this.comm = comm;
    }
    public Integer getDeptno() {
        return deptno;
    }
    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }
    @Override
    public String toString() {
        return "Employee{" +
                "empno=" + empno +
                ", ename='" + ename + '\'' +
                ", job='" + job + '\'' +
                ", mgr=" + mgr +
                ", hiredate=" + hiredate +
                ", sal=" + sal +
                ", comm=" + comm +
                ", deptno=" + deptno +
                '}';
    }
}

定义接口

public interface EmployeeReposity extends Repository<Employee,Integer> {
    //定义根据工资sal查询员工信息
    List<Employee>findAllBySal(Double sal);
}

测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class TestJPARepositoty {
    @Autowired
    private EmployeeReposity employeeReposity;
    @Test
    public void test() {
        //查询工资三千的员工信息
        List<Employee> employeeList = employeeReposity.findAllBySal(3000.0);
        for (Employee employee : employeeList) {
            System.out.println(employee);
        }
    }
}
---------------------------------------
运行结果:
Employee{empno=7788, ename='SCOTT', job='ANALYST', mgr=7566, hiredate=1987-04-19, sal=3000.0, comm=null, deptno=20}
Employee{empno=7902, ename='FORD', job='ANALYST', mgr=7566, hiredate=1981-12-03, sal=3000.0, comm=null, deptno=20}
上一篇下一篇

猜你喜欢

热点阅读