1

前言

Spring-data-jpa依赖于Hibernate,目的在于使数据访问更加简单


初次学习流程

  • 添加依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
  • 配置mysql数据源以及JPA
server.port=8088
#####
#配置数据源,依次为地址、用户名、密码、驱动、最大连接数
#####
spring.datasource.url=  jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.tomcat.max-active=20

######
####配置JPA
####从上往下依次数据源mysql、显示ddl语句、自动执行ddl语句(更新、创建或者销毁、创建)
####最后两句表示自动在数据库创建实体的表
######
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
#spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy  
#spring.jpa.database=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  • 3.布局包结构
    图片描述

编写实体

package com.dataaccess.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 * Created by Administrator on 2017/9/14.
 */
/*
* 使用@Entity实现实体的持久化,JPA检测到实体中有该注解时,会在数据库中生成对应的表
* 使用@Id指定表的主键
* 使用@GeneratedValue(strategy = GenerationType.AUTO)指定主键自动生成,mysql默认自增长
 */
@Entity
public class person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String name;
    private int age;
    public int getId(){
        return  id;
    }
    public void setId(int id){
        this.id=id;
    }
    public int getAge(){
        return age;
    }
    public void setAge(int age){
        this.age=age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

创建接口

package com.dataaccess.dao;

import org.springframework.data.repository.CrudRepository;
import com.dataaccess.model.person;
/**
 * Created by Administrator on 2017/9/14.
 */
public interface PersonRepository extends CrudRepository <person,Integer>  {

}

配置服务

package com.dataaccess.service;

import com.dataaccess.dao.PersonRepository;
import org.springframework.stereotype.Service;
import com.dataaccess.model.person;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by Administrator on 2017/9/14.
 */
/*
*@Transactional进行事物的绑定
 */
@Service
public class PersonService {
    @Resource
    private PersonRepository personRepository;
    @Transactional
    public void save( person p){
     personRepository.save(p);
    }
    @Transactional
    public void delete( int id){
personRepository.delete(id);
    }
    @Transactional
    public Iterable<person> getAll(){
        return personRepository.findAll();
    }



}


控制器

package com.dataaccess.controller;

import com.dataaccess.model.person;
import com.dataaccess.service.PersonService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/9/14.
 */
@RestController
@RequestMapping("/index")
public class HelloController {
    @Resource
    private PersonService personService;
    @RequestMapping("/save")
    public String save(){
        person p = new person();
        p.setAge(21);
        p.setName("DLP");
        personService.save(p);
        return "存入用户成功";
    }
    @RequestMapping("/delete")
    public String delete(){
personService.delete(1);
return "删除编号为1的用户";
    }
    @RequestMapping("/findAll")
    public Iterable<person> getAll(){
       return personService.getAll();
    }
}

运行

  • 添加用户到数据表
    图片描述图片描述
  • 从数据表中删除用户
    图片描述

图片描述



邓琳平
59 声望11 粉丝

这个人很懒