springboot_jpa

包结构
在这里插入图片描述
导入依赖

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.2.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>

创建实体

@Entity
public class Dog {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private int dogid;
    private String dogname;
    private String kind;
    private int age;
    private int sex;
    }

创建DogDao

public interface DogDao extends CrudRepository<Dog, Integer> {
}

创建DogService

public interface DogService {
    //新增数据
    public Dog insertGrade(Dog dog);

    //修改数据
    public Dog updataGrade(Dog dog);

    //删除数据
    public void deleteGrade(Integer id);
    //查询数据
    public Iterable<Dog> getAllGrade();
}

创建DogSetviceimpl

@Service
public class DogSetviceimpl implements DogService {
    @Resource
    private DogDao dogDao;

    @Override
    public Dog insertGrade(Dog dog) {
        return dogDao.save(dog);
    }

    @Override
    public Dog updataGrade(Dog dog) {
        return dogDao.save(dog);
    }

    @Override
    public void deleteGrade(Integer id) {
        dogDao.deleteById(id);
    }

    @Override
    public Iterable<Dog> getAllGrade() {
        return dogDao.findAll();
    }
}

创建JpaController

@RestController
@RequestMapping("/DaoController")
public class JpaController {
    @Resource
    private DogService dogService;
    //添加
        @RequestMapping("/insertDog")
    public Dog insertGrade(){
        Dog dog=new Dog();
        dog.setDogname("海绵宝宝");
        dog.setAge(1);
        return dogService.insertGrade(dog);
    }
    //修改
    @RequestMapping("/updateDog")
    public Dog updateDao(){
        Dog dog=new Dog();
        dog.setDogid(1);
        dog.setDogname("海绵宝宝puls");
        return dogService.updataGrade(dog);
    }
    //删除
    @RequestMapping("/deleteDog")
    public void deleteDog(){
        dogService.deleteGrade(1);
    }
    //查询
    @RequestMapping("/getAllDog")
    public Iterable<Dog> getAllDog(){
        return dogService.getAllGrade();
    }

创建application.yml

##数据库四大连接参数
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///springbootjpa
    username: root
    password: root
  ##jpa表的生成策略
  jpa:
    hibernate:
      ddl-auto: update
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值