zoukankan      html  css  js  c++  java
  • SpringBoot JPA(实现查询多值)

    JPA是java Persistence API简称,中文名:java持久层API,JPA是JCP组织发布的J2EE标准之一

    1.创建DataSource连接池对象

     1 <dependency>
     2             <groupId>org.springframework.boot</groupId>
     3             <artifactId>spring-boot-starter-jdbc</artifactId>
     4         </dependency>
     5         <!-- 数据库驱动 -->
     6         <dependency>
     7             <groupId>com.oracle</groupId>
     8             <artifactId>ojdbc6</artifactId>
     9             <version>11.2.0.3</version>
    10         </dependency>
    View Code

    2.在pom.xml中定义spring-boot-starter-data-jpa

    1 <!-- 定义spring-boot-starter-data-jpa -->
    2           <dependency>
    3               <groupId>org.springframework.boot</groupId>
    4               <artifactId>spring-boot-starter-data-jpa</artifactId>
    5           </dependency>
    View Code

    3.根据数据库表定义实体类

     1 package cn.xdl.entity;
     2 
     3 import java.io.Serializable;
     4 
     5 import javax.persistence.Column;
     6 import javax.persistence.Entity;
     7 import javax.persistence.Id;
     8 import javax.persistence.Table;
     9 
    10 @Entity
    11 @Table(name="EMP")  //通常和@Entity配合使用,只能标注在实体的class定义处,表示实体对应的数据库表的信息
    12 public class Emp implements Serializable{
    13     /**
    14      * 
    15      */
    16     private static final long serialVersionUID = 1L;
    17     @Id  //定义了映射到数据库表的主键的属性,一个实体只能有一个属性被映射为主键置于getXxxx()前
    18     @Column(name="EMPNO")  //name表示表的名称默认地,表名和实体名称一致,只有在不一致的情况下才需要指定表名
    19     private Integer empno;
    20     @Column(name="ENAME")
    21     private String ename;
    22     @Column(name="JOB")
    23     private String job;
    24     @Column(name="MGR")
    25     private int mgr;
    26     public Integer getEmpno() {
    27         return empno;
    28     }
    29     public void setEmpno(Integer empno) {
    30         this.empno = empno;
    31     }
    32     public String getEname() {
    33         return ename;
    34     }
    35     public void setEname(String ename) {
    36         this.ename = ename;
    37     }
    38     public String getJob() {
    39         return job;
    40     }
    41     public void setJob(String job) {
    42         this.job = job;
    43     }
    44     public int getMgr() {
    45         return mgr;
    46     }
    47     public void setMgr(int mgr) {
    48         this.mgr = mgr;
    49     }
    50     @Override
    51     public String toString() {
    52         return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + "]";
    53     }
    54 }
    View Code

    4.定义Dao接口,继承JPA功能接口

     1 package cn.xdl.jpa;
     2 
     3 import org.springframework.data.jpa.repository.JpaRepository;
     4 
     5 import cn.xdl.entity.Emp;
     6 //JpaRepository:JPA资源库
     7 /**
     8  * 1.所有继承该接口的都被spring所管理,改接口作为标识接口,功能就是用来控制domain模型的
     9  * 2.Spring Data可以让我们只定义接口,只要遵循spring data的规范,无需写实现类。 
    10  *
    11  */
    12 public interface EmpDao extends JpaRepository<Emp, Integer>{
    13 
    14 }
    View Code

    5.获取Dao接口对象操作数据库

     1 @SpringBootApplication
     2 public class MyBootApplication {
     3     public static void main(String[] args) throws SQLException {
     4         ApplicationContext ioc = SpringApplication.run(MyBootApplication.class, args);
     5         // 自动配置创建DataSource,id名为dataSource
     6         DataSource ds = ioc.getBean("dataSource", DataSource.class);
     7         System.out.println(ds);
     8         System.out.println("=================");
     9         System.out.println("=================");
    10         System.out.println("=================");
    11         EmpDao empDao = ioc.getBean("empDao", EmpDao.class);
    12         /**
    13          * 遍历
    14          */
    15         List<Emp> empdatas = empDao.findAll();
    16         for (Emp emp : empdatas) {
    17             System.out.println(emp);
    18         }
    19     }
    20 }
    View Code
  • 相关阅读:
    ubuntu12.04 死机 卡屏 画面冻结解决方案
    Install Firefox 20 in Ubuntu 13.04, Ubuntu 12.10, Ubuntu 12.04, Linux Mint 14 and Linux Mint 13 by PPA
    ListView1.SelectedItems.Clear()
    android studio 下载地址
    jquery.slider.js jquery幻灯片测试
    jquery.hovermenu.js
    jquery.tab.js选项卡效果
    适配 placeholder,jquery版
    jquery.autoscroll.js jquery自动滚动效果
    将 Google Earth 地图集成到自己的窗体上的 简单控件
  • 原文地址:https://www.cnblogs.com/resultset/p/9551593.html
Copyright © 2011-2022 走看看