一、使用单元测试

单元测试在每个项目环境中必不可少,springboot中如何使用单元测试

在src/test/java中新建测试类DemoApplicationTest.java

项目结构:

DemoApplicaytionTest.java内容
package springboot_jpa_jsp;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.king.app.App;
import com.king.entity.User;
import com.king.service.UserService; @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class)
public class DemoApplicaytionTest { @Autowired
private UserService userService; @Test
public void testUser() {
User u = userService.findOne("1");
System.out.println(u.toString());
}
}

此时,直接右键运行Junit测试即可

二、查询方法

1. 使用jpa的命名查询

当使用findBy/readBy/getBy + 某个字段时,比如:

User findById(String id);

也可以多字段查询,如:

List<User> findByCodeAndUsername(String code,String username);

spring jpa的相关命名规则如下:

Keyword Sample JPQL snippet

And

findByLastnameAndFirstname

… where x.lastname = ?1 and x.firstname = ?2

Or

findByLastnameOrFirstname

… where x.lastname = ?1 or x.firstname = ?2

Is,Equals

findByFirstname,findByFirstnameIs,findByFirstnameEquals

… where x.firstname = ?1

Between

findByStartDateBetween

… where x.startDate between ?1 and ?2

LessThan

findByAgeLessThan

… where x.age < ?1

LessThanEqual

findByAgeLessThanEqual

… where x.age <= ?1

GreaterThan

findByAgeGreaterThan

… where x.age > ?1

GreaterThanEqual

findByAgeGreaterThanEqual

… where x.age >= ?1

After

findByStartDateAfter

… where x.startDate > ?1

Before

findByStartDateBefore

… where x.startDate < ?1

IsNull

findByAgeIsNull

… where x.age is null

IsNotNull,NotNull

findByAge(Is)NotNull

… where x.age not null

Like

findByFirstnameLike

… where x.firstname like ?1

NotLike

findByFirstnameNotLike

… where x.firstname not like ?1

StartingWith

findByFirstnameStartingWith

… where x.firstname like ?1 (parameter bound with appended %)

EndingWith

findByFirstnameEndingWith

… where x.firstname like ?1 (parameter bound with prepended %)

Containing

findByFirstnameContaining

… where x.firstname like ?1 (parameter bound wrapped in %)

OrderBy

findByAgeOrderByLastnameDesc

… where x.age = ?1 order by x.lastname desc

Not

findByLastnameNot

… where x.lastname <> ?1

In

findByAgeIn(Collection<Age> ages)

… where x.age in ?1

NotIn

findByAgeNotIn(Collection<Age> age)

… where x.age not in ?1

True

findByActiveTrue()

… where x.active = true

False

findByActiveFalse()

… where x.active = false

IgnoreCase

findByFirstnameIgnoreCase

… where UPPER(x.firstame) = UPPER(?1)

2. 自定义注解查询

除了继承JpaRepository中的命名方法,有时候不可避免的要自定义查询方法。上面的单元测试中findOne就是自定义的查询方法。

使用@Query注解来查询,注解查询本质上仍然使用的是HQL语法,所以下面的是针对对象查询的。(我在测试时由于粗心大意将User写成user被坑了不少时间)

    @Query("select u from User u where u.id = :id")
User findOne(@Param("id")String id);

最新文章

  1. [(ngModel)]的实现原理
  2. ES5基础01:正则表达式
  3. 查看Linux硬件配置信息
  4. Android项目——传感器的使用
  5. hdu 1044 Collect More Jewels(bfs+状态压缩)
  6. USB挂起与唤醒.
  7. Linux学习1——首次登录
  8. Swift - 协议(protocol)
  9. bigdata之hadoop and spark
  10. 如何解决找不到方法HttpServletRequest.getServletContext() ---- NoSuchMethodError
  11. flex stage.width 与stage.stageWidth的区别
  12. python note 08 文件操作
  13. django xadmin拓展User模型
  14. MEF 插件式开发之 WPF 初体验
  15. VUE路由转场特效,WebAPP的前进与后退
  16. HiKey软硬件开发环境及其调试
  17. 数据分析--降维--LDA和PCA
  18. 微博推荐算法学习(Weibo Recommend Algolrithm)
  19. 如何修改opencart的模版适合为mycncart系统使用
  20. java项目学习

热门文章

  1. iOS CPU占有率达到了100%甚至更多,然后导致App闪退
  2. 分析一帧基于UDP的TFTP协议帧
  3. C#--readlyonly关键字
  4. 在linq to entities中无法使用自定义方法
  5. JavaScript之正則表達式入门
  6. vim打造简易C语言编辑器(在用2016.7.10)
  7. iptables的自定义链--子链
  8. 【Android】16.5 Android内置的系统服务
  9. 【Anroid】9.1 ListView相关类及其适配器
  10. date 增加一个小时 减少一个小时