zoukankan      html  css  js  c++  java
  • springboot中jpa+lombok

    1.依赖

            <!--jpa相关-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>

    yml配置文件

    spring:
      datasource:
        url: jdbc:mysql://127.0.0.1:3306/wxdd?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false
        driver-class-name: com.mysql.cj.jdbc.Driver
        username: root
        password: 123456
      jpa:
        show-sql: true

    2.pojo

    @Entity
    @DynamicUpdate//若是数据库有默认的字段,会根据数据库默认字段进行修改
    @Data   //lombok 他可以创建set、get方法以及toString
    public class ProductInfo {
        @Id
        @GeneratedValue(strategy= GenerationType.IDENTITY)
        private Integer productId;
        private String productName;//名称
        private Integer productPrice;//加个
        private Integer productType;//类目
    }

    3.mapper文件

    public interface ProductInfoMapper extends JpaRepository<ProductInfo,Integer> {
      //方法名有格式的
      List<ProductInfo> findByProductIdIn(List<Integer> list);
    }

    4.测试类

    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class ProductInfoMapperTest {
        @Autowired
        private ProductInfoMapper product;
        @Test
        @Transactional
        public void getOne(){
            ProductInfo productInfo=product.getOne(1232);
            System.out.println(productInfo);
        }
    
        @Test
    //    @Transactional
        public void savMsg(){
            ProductInfo productInfo = new ProductInfo();
            productInfo.setProductName("张三");
            productInfo.setProductPrice(123456);
            productInfo.setProductType(2);
            ProductInfo pr = product.save(productInfo);
            Assert.assertNotNull(pr);
    //        Assert.assertNotEquals(null,pr);
        }
    
        @Test
        public void findByProductIdInTest(){
    
            List<Integer> list = Arrays.asList(1234,1235);
            List<ProductInfo> result = product.findByProductIdIn(list);
            System.out.println(result.toString());
            Assert.assertNotEquals(0,result.size());
        }
    
    
    }

    @Modifying:可以使用该注解来实现通过JPQL修改和删除(JPQL不支持添加)

    @Modifying
    @Query(value = "update Person p set p.name = ?1, p.age = ?2 where p.id = ?3")
    void updatePerson(String name, Integer age, Long id);
  • 相关阅读:
    正则表达式转换python2的print为python3风格
    译:Local Spectral Graph Convolution for Point Set Feature Learning-用于点集特征学习的局部谱图卷积
    Objectbox Box的getAll() 函数返回emptylist() 未判断导致崩溃
    关于定义顺序和内存分配的关系--记一道不严谨的C语言题
    iRecognizer号码扫描开发实录
    我的visual studio 配色方案 Rubik c++版
    Opencv4android的Android Studio环境配置及项目实例下载
    随便记录下
    linux下在先安装32位qt 链接
    python笔记
  • 原文地址:https://www.cnblogs.com/gxlaqj/p/11655116.html
Copyright © 2011-2022 走看看