• SpirngBoot整合Spring-data-JPA


    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.运行后结果如下

  • 相关阅读:
    今天开始学习WCF了开发环境的配置
    我的文章《DLL封装登录框架实现代码复用》终于要发表了
    python小练习给右键菜单添加用chrome打开
    手动创建oracle数据库
    Sublime Text 2中如何输入中文
    转 用Python画Mandelbrot集
    Win7 64位安装Python PIL库
    推荐一本有关设计的好书《写给大家看的设计书》
    坚持做自己
    更新专业版显卡驱动导致ubuntu无法进入桌面环境
  • 原文地址:https://www.cnblogs.com/godoforange/p/11533273.html
Copyright © 2020-2023  润新知