0.引言

使用SpringBoot data jpa技术相比mybatis是比较难的,这里只给出整合方法

1.引入SpringBoot data jpa

        <!--JPA依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.9</version>
        </dependency>
        <!--mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

2.配置文件

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mathproject?serverTimezone=UTC&useSSL=true&allowMultiQueries=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.show-sql=true
spring.jpa.database=mysql
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQL57InnoDBDialect
server.servlet.session.timeout=600m

3.建表

ORM框架不需要建表,只需要在Bean中加个@Entity注解就行

import javax.persistence.*;

/**
 * 广告表
 */
@Entity(name="Advertisement")
public class Advertisement {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id",columnDefinition="bigint COMMENT '主键,自动生成'")
    private Integer id;
    @Column(name = "img",columnDefinition="varchar(32)")
    private String img;
    @Column(name = "href",columnDefinition="varchar(32)")
    private String href;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }
}

4.DAO层

持久层的话直接继承下来就行,别的都不用做

import org.just.computer.mathproject.Entity.Advertisement;
import org.springframework.data.jpa.repository.JpaRepository;

public interface AdvertisementResp extends JpaRepository<Advertisement,Integer> {
}

5.service层

新手千万记得要加@Service,否则会在IOC时无法注入

import org.just.computer.mathproject.DAO.AdvertisementResp;
import org.just.computer.mathproject.Entity.Advertisement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdvertisementService {
    @Autowired
    AdvertisementResp advertisementResp;

    public List<Advertisement> getAllAdvertisement(){
        return advertisementResp.findAll();
    }
    public void addAdvertisement(String img,String href){
        Advertisement advertisement=new Advertisement();
        advertisement.setImg(img);
        advertisement.setHref(href);
        advertisementResp.save(advertisement);
    }
    public void deleteAdvertisementById(Integer id){
        advertisementResp.deleteById(id);
    }
}

6.Controller层

注意这里结合了swagger的注解

@RestController
@Api(tags ="广告")
@RequestMapping("/Advertisement/")
public class AdvertisementController {
    @Autowired
    AdvertisementService advertisementService;

    @ApiOperation(value ="获得所有广告")
    @GetMapping("/getAllAdvertisement")
    public List<Advertisement> getAllAdvertisement(){
        return advertisementService.getAllAdvertisement();
    }
    @ApiOperation(value = "添加广告")
    @GetMapping("/addAdvertisement")
    public Boolean getAllAdvertisement(@RequestParam String img, @RequestParam String href){
        try {
            advertisementService.addAdvertisement(img,href);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @ApiOperation(value = "删除广告")
    @GetMapping("/deleteAdvertisement")
    public Boolean deleteAdvertisementById(Integer id){
       try{
           advertisementService.deleteAdvertisementById(id);
           return true;
       }catch (Exception e){
           return false;
       }
    }
}

7.运行后结果如下

01-20 02:35