前言
最近开始在公司实习了,刚开始还没有跟项目,只是接到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