[log]spring data jpa 实现动态的更新

sql 的动态更新插入

mybatis 中可以在sql语句中使用isNull来判断

jap中的实现方法

@Entity
@Data
@Table(name = "channel")
// 需要在实体类中加入
@EntityListeners(AuditingEntityListener.class)
public class Channel {
	 @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long channelId;
	 @CreationTimestamp
    private Timestamp createTime;
    @CreatedBy
    private String createUserId;
    @UpdateTimestamp
    private Timestamp modifyTime;
    @LastModifiedBy
    private String modifyUserId;
    }
@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class JpaAuditingConfiguration {

    @Bean
    public AuditorAware<String> auditorProvider() {
    //获取当前用户 在create的时候使用
        return () -> Optional.ofNullable( SecurityContextHolder.getContext().getAuthentication().getName());
    }


}

    @PostMapping(value = "/create")
    protected ResponseEntity create(@RequestBody Channel resources) {

        if (!ObjectUtils.isEmpty(resources.getChannelId())) {
            Channel channel = phoneChannelService.getInfo(resources.getChannelId()).get();
            resources.setCreateTime(channel.getCreateTime());
            resources.setCreateUserId(channel.getCreateUserId());
            BeanUtils.copyProperties(resources, channel, ResultUtil.getNullPropertyNames(resources));
            resources = channel;
        }
        return ResultUtil.wrapper(phoneChannelService.save(resources), HttpStatus.OK);
    }

   /**
     * 获取属性为null的字段列表
     * @date: 2019-06-10 liu
    **/
    public static String[] getNullPropertyNames(Object source) {
        final BeanWrapper wrappedSource = new BeanWrapperImpl(source);
        return Stream.of(wrappedSource.getPropertyDescriptors())
                .map(FeatureDescriptor::getName)
                .filter(propertyName -> wrappedSource.getPropertyValue(propertyName) == null)
                .toArray(String[]::new);
    }
}
实现公告发布的基本思路是,前端页面提供一个表单,用户输入公告内容,点击发布按钮,然后将公告信息通过Ajax请求发送到后端,后端接收到请求后将公告信息保存到数据库中,再返回一个响应告诉前端保存成功。 下面是一个简单的实现过程: 1. 创建公告表 首先,在数据库中创建一个公告表,包含id、标题、内容、发布时间等字段。 2. 创建后端接口 使用Spring Boot创建一个后端接口,接收前端发送的公告信息,将公告信息保存到数据库中。可以使用Spring Data JPA实现对数据库的操作。 例如,创建一个名为AnnouncementController的控制器类,包含一个POST方法用于接收并保存公告信息: ```java @RestController @RequestMapping("/announcement") public class AnnouncementController { @Autowired private AnnouncementRepository announcementRepository; @PostMapping("/add") public String addAnnouncement(@RequestBody Announcement announcement) { announcementRepository.save(announcement); return "success"; } } ``` 3. 创建前端页面 使用Vue.js创建一个前端页面,包含一个表单,用户输入公告信息并点击发布按钮后将公告信息发送到后端接口。 例如,创建一个名为AnnouncementForm的Vue组件,包含一个表单和一个发布按钮: ```html <template> <form> <div> <label for="title">标题:</label> <input type="text" id="title" v-model="title" /> </div> <div> <label for="content">内容:</label> <textarea id="content" v-model="content"></textarea> </div> <button type="button" @click="addAnnouncement">发布</button> </form> </template> ``` 在组件中定义一个addAnnouncement方法,用于将公告信息发送到后端接口: ```javascript <script> import axios from 'axios'; export default { data() { return { title: '', content: '' }; }, methods: { addAnnouncement() { const announcement = { title: this.title, content: this.content }; axios.post('/announcement/add', announcement) .then(response => { console.log(response.data); }) .catch(error => { console.log(error); }); } } }; </script> ``` 4. 集成前后端 将上面的前后端代码集成到一起,启动Spring Boot应用和Vue.js应用,访问Vue.js应用中的公告发布页面,输入公告信息并点击发布按钮,就可以将公告信息保存到数据库中了。 以上就是一个简单的Vue.js和Spring Boot实现公告发布的示例。当然,实际开发中还需要考虑一些安全性和可用性方面的问题,例如防止恶意攻击、数据校验、分页查询等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值