使用hibernate实现增删改查

前言

最近开始在公司实习了,刚开始还没有跟项目,只是接到leader的指令看以往的代码,然后自己添加简单额增删改查功能。公司用的是springboot+hibernate的框架。

创建数据库

首先贴出数据库的代码,因为是测试,所以比较简单,只有三个字段:

CREATE TABLE `t_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `gender` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

后端代码实现

在这里插入图片描述
这个是代码目录,非常简单,因为只是很简单的例子,接下来直接贴出代码:
TestRepository:

public interface TestRepository extends JpaRepository<Test, Integer>, QuerydslPredicateExecutor<Test> {

    @Query(value = "select *  from t_test where id = ?1 ", nativeQuery = true)
    Test findByIds(Integer id);
    @Query(value = "select *  from t_test where name like %?1%", nativeQuery = true)
    List<Test>  findVauge(String name);
    @Query(value = "select *  from t_test limit ?1,?2 ", nativeQuery = true)
    List<Test> findTest(Integer start, Integer end);
    @Modifying
    @Query(value = "update t_test set name=?1 where id=?2 ", nativeQuery = true)
    int updateTest(String name,Integer id);
    @Modifying
    @Query(value = "delete from t_test  where id=? ", nativeQuery = true)
    int deleteTest(Integer id);
    @Modifying
    @Query(value = "insert into t_test (name,gender) values(?1,?2) ", nativeQuery = true)
    int insertTest
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值