SpringBoot开发实战

@DynamicUpdate注解

@Entity
public class ProductCategory {

    /** 类目id. */
    @Id
    @GeneratedValue
    private Integer categoryId;

    /** 类目名字. */
    private String categoryName;

    /** 类目编号. */
    private Integer categoryType;
}

@Test
    public void saveTest() {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setCategoryId(2);
        productCategory.setCategoryName("男生最爱");
        productCategory.setCategoryType(3);
        repository.save(productCategory);
    }

update_time会进行更新。

@Entity
public class ProductCategory {

    /** 类目id. */
    @Id
    @GeneratedValue
    private Integer categoryId;

    /** 类目名字. */
    private String categoryName;

    /** 类目编号. */
    private Integer categoryType;

    private Date createTime;

    private Date updateTime;
}
@Test
    public void saveTest() {
        ProductCategory productCategory = repository.findOne(1);
        productCategory.setCategoryType(1);
        repository.save(productCategory);
    }

update_time不会更新。

在ProductCategory添加@DynamicUpdate,再次进行修改,update_time就会进行修改(需要修改为其他categoryType的值)。
@DynamicUpdate表示update对象的时候,生成动态的update语句,如果这个字段的值是null就不会被加入到update语句中。

快捷getter and setter

添加依赖

<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

以及插件lombok plugins

在ProductCategory添加@Date注解,就不许在生成getter和setter方法,toString()等方法。
如果只需要getter方法,只需添加@Getter,setter同理。

javax.transaction.Transactional

测试成功后,测试数据不影响数据库数据。与service中的事务不同,这里的Transactional在测试完成后,无论是否发生异常,都会将所有数据库操作回滚。

    @Test
    @Transactional
    public void saveTest() {
        ProductCategory productCategory = new ProductCategory("男生最爱", 4);
        ProductCategory result = repository.save(productCategory);
        // 断言方法
        Assert.assertNotNull(result);
        //Assert.assertNotEquals(null, result);
    }

@JsonProperty

@JsonProperty("id")
private String productId;

productId为了让我们看清楚字段的含义,id为借口要求的返回值。

BeanUtils

Spring提供的一个工具类:
BeanUtils.copyProperties(productInfo, productInofVO);
把productInfo对象的属性值copy到productInofVO中去。

分页

org.springframework.data.domain.Page

public interface OrderMasterRepository extends JpaRepository<OrderMaster, String> {

    Page<OrderMaster> findByBuyerOpenid(String buyerOpenid, Pageable pageable);

}

测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderMasterRepositoryTest {

    @Autowired
    private OrderMasterRepository repository;

    private final String OPENID = "110110";

    @Test
    public void findByBuyerOpenid() {
        // 第一个参数表示第几页,第二个表示一页几个
        PageRequest request = new PageRequest(0,1);

        Page<OrderMaster> result = repository.findByBuyerOpenid(OPENID, request);
        Assert.assertNotEquals(0, result.getTotalElements());
    }
}

@Transient

javax.persistence.Transient
在与数据库对照的实体类中,某个字段添加这个注解,会忽略掉这个字段。

日期转换之JsonSerialize

public class Date2LongSerializer extends JsonSerializer<Date> {

    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        jsonGenerator.writeNumber(date.getTime() / 1000);
    }
}

@Data
public class OrderDTO {

    /** ... */

    @JsonSerialize(using = Date2LongSerializer.class)
    private Date createTime;

    @JsonSerialize(using = Date2LongSerializer.class)
    private Date updateTime;
}

在返回结果OrderDTO时,会处理器时间。

实体类的参数查询到的为null的不显示

单个实体类配置,类头添加注解@JsonInclude(JsonInclude.Include.NON_NULL)

全局配置:

spring:
  jackson:
    default-property-inclusion: non_null
忽略方法

@JsonIgnore

RestTemplate

freemarker

依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
@Controller
@RequestMapping("/seller/order")
public class SellerOrderController {


    @GetMapping("/list")
    public ModelAndView list() {
        return new ModelAndView("order/list");
    }

}

建立文件夹以及文件:resources/templates/order/list.ftl

http://www.ibootstrap.cn/

异常捕获

@ControllerAdvice
public class SellexceptionHandler {

    @ExceptionHandler(value = SellException.class)
    @ResponseBody
    public ResultVO handlerSellerException(SellException e) {
        return ResultVOUtil.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = SellerAuthorizeException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public void handlerSellerAuthorizeException(SellerAuthorizeException e) {

    }
}
当前课程中博客项目的实战源码是我在 GitHub上开源项目 My-Blog,目前已有 3000 多个 star:本课程是一个 Spring Boot 技术栈的实战类课程,课程共分为 3 大部分,前面两个部分为基础环境准备和相关概念介绍,第三个部分是 Spring Boot 个人博客项目功能的讲解,通过本课程的学习,不仅仅让你掌握基本的 Spring Boot 开发能力以及 Spring Boot 项目的大部分开发使用场景,同时帮你提前甄别和处理掉将要遇到的技术难点,认真学完这个课程后,你将会对 Spring Boot 有更加深入而全面的了解,同时你也会得到一个大家都在使用的博客系统源码,你可以根据自己的需求和想法进行改造,也可以直接使用它来作为自己的个人网站,这个课程一定会给你带来巨大的收获。作者寄语本课程录制于 2020 年,代码基于 Spring Boot 2.x 版本。到目前为止,Spring Boot 技术栈也有一些版本升级,比如 Spring Boot 2.7 发版、Spring Boot 3.x 版本发布正式版本。对于这些情况,笔者会在本课程实战项目的开源仓库中创建不同的代码分支,保持实战项目的源码更新,保证读者朋友们不会学习过气的知识点。课程特色 课程内容紧贴 Spring Boot 技术栈,涵盖大部分 Spring Boot 使用场景。开发教程详细完整、文档资源齐全、实验过程循序渐进简单明了。实践项目页面美观且实用,交互效果完美。包含从零搭建项目、以及完整的后台管理系统和博客展示系统两个系统的功能开发流程。技术栈新颖且知识点丰富,学习后可以提升大家对于知识的理解和掌握,对于提升你的市场竞争力有一定的帮助。实战项目预览    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值