JPA是java Persistence API简称,中文名:java持久层API,JPA是JCP组织发布的J2EE标准之一
1.创建DataSource连接池对象

1 1 <dependency> 2 2 <groupId>org.springframework.boot</groupId> 3 3 <artifactId>spring-boot-starter-jdbc</artifactId> 4 4 </dependency> 5 5 <!-- 数据库驱动 --> 6 6 <dependency> 7 7 <groupId>com.oracle</groupId> 8 8 <artifactId>ojdbc6</artifactId> 9 9 <version>11.2.0.3</version> 1010 </dependency>
View Code
2.在pom.xml中定义spring-boot-starter-data-jpa

11 <!-- 定义spring-boot-starter-data-jpa --> 22 <dependency> 33 <groupId>org.springframework.boot</groupId> 44 <artifactId>spring-boot-starter-data-jpa</artifactId> 55 </dependency>
View Code
3.根据数据库表定义实体类

1 1 package cn.xdl.entity; 2 2 3 3 import java.io.Serializable; 4 4 5 5 import javax.persistence.Column; 6 6 import javax.persistence.Entity; 7 7 import javax.persistence.Id; 8 8 import javax.persistence.Table; 9 9 1010 @Entity 1111 @Table(name="EMP") //通常和@Entity配合使用,只能标注在实体的class定义处,表示实体对应的数据库表的信息 1212 public class Emp implements Serializable{ 1313 /** 1414 * 1515 */ 1616 private static final long serialVersionUID = 1L; 1717 @Id //定义了映射到数据库表的主键的属性,一个实体只能有一个属性被映射为主键置于getXxxx()前 1818 @Column(name="EMPNO") //name表示表的名称默认地,表名和实体名称一致,只有在不一致的情况下才需要指定表名 1919 private Integer empno; 2020 @Column(name="ENAME") 2121 private String ename; 2222 @Column(name="JOB") 2323 private String job; 2424 @Column(name="MGR") 2525 private int mgr; 2626 public Integer getEmpno() { 2727 return empno; 2828 } 2929 public void setEmpno(Integer empno) { 3030 this.empno = empno; 3131 } 3232 public String getEname() { 3333 return ename; 3434 } 3535 public void setEname(String ename) { 3636 this.ename = ename; 3737 } 3838 public String getJob() { 3939 return job; 4040 } 4141 public void setJob(String job) { 4242 this.job = job; 4343 } 4444 public int getMgr() { 4545 return mgr; 4646 } 4747 public void setMgr(int mgr) { 4848 this.mgr = mgr; 4949 } 5050 @Override 5151 public String toString() { 5252 return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + "]"; 5353 } 5454 }
View Code
4.定义Dao接口,继承JPA功能接口

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

1 1 @SpringBootApplication 2 2 public class MyBootApplication { 3 3 public static void main(String[] args) throws SQLException { 4 4 ApplicationContext ioc = SpringApplication.run(MyBootApplication.class, args); 5 5 // 自动配置创建DataSource,id名为dataSource 6 6 DataSource ds = ioc.getBean("dataSource", DataSource.class); 7 7 System.out.println(ds); 8 8 System.out.println("================="); 9 9 System.out.println("================="); 1010 System.out.println("================="); 1111 EmpDao empDao = ioc.getBean("empDao", EmpDao.class); 1212 /** 1313 * 遍历 1414 */ 1515 List<Emp> empdatas = empDao.findAll(); 1616 for (Emp emp : empdatas) { 1717 System.out.println(emp); 1818 } 1919 } 2020 }
View Code