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);
  • 相关阅读:
    缠中说禅 摘选
    laravel中不使用 remember_token时退出报错,如何解决?
    关于Ubuntu拒绝root用户ssh远程登录
    laravel中类似于thinkPHP中trace功能
    js原生语法实现表格操作
    使用clear来清除localStorage保存对象的全部数据
    JS设置CSS样式的集中方式
    thinkphp两表联查并且分页
    生于忧患,死于安乐 (先秦:孟子及其弟子)
    在对年轻人最不友好的环境中,刘裕起于阡陌,成就霸业
  • 原文地址:https://www.cnblogs.com/gxlaqj/p/11655116.html
Copyright © 2011-2022 走看看