SpringBoot+SpringDataJpa后台常用注解




SpringDataJpa中sqlite的时间格式必须为' 2017-03-23 09:10:29.100'  否则报错


com.controller
@RestController  表示该类是controller层 //@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
@RequestMapping("/")  请求路径
@Autowired spring自动创建该对象的bean,生成对象
@ResponseBody  返回结果放入body数据区,返回的不是html页面,一般为json


com.service
@Service  表示该类是service层


com.repository
@Query("select t from UsersEntity t where t.userName=?1 and t.from = ?2") 
UsersEntity findByUserNameAndFrom(String userName,String from);
//执行findByUserNameAndFrom的方法的时候不会去调用SpringDataJpa的匹配规则,直接调用query后面的hql语句


@Query("select * from tb_task t where t.task_name = ?1", nativeQuery = true)
  Task findByTaskName(String taskName);
  //执行findByTaskName的方法的时候不会去调用SpringDataJpa的匹配规则,直接调用query后面的sql语句


com.entity
@Entity  表示该类是entity
@Table(name = "users") //表名
@Id  表示该属性对应的字段是主键
@GeneratedValue(strategy = GenerationType.AUTO) //主键生成策略
@Column(name = "u_id") //对应的字段名




@RequestMapping("/findByUserNameAndFrom/{userName}/{from}")
@ResponseBody
public String findByUserNameAndFrom(@PathVariable String userName, @PathVariable String from) {
return userService.findByUserNameAndFrom(userName, from);
}
//@PathVariable 使用RequestMapping路径中的参数

下面是SpringDataJpa方法名命名规则:

关键字方法命名sql where字句
AndfindByNameAndPwdwhere name= ? and pwd =?
OrfindByNameOrSexwhere name= ? or sex=?
Is,EqualsfindById,findByIdEqualswhere id= ?
BetweenfindByIdBetweenwhere id between ? and ?
LessThanfindByIdLessThanwhere id < ?
LessThanEqualsfindByIdLessThanEqualswhere id <= ?
GreaterThanfindByIdGreaterThanwhere id > ?
GreaterThanEqualsfindByIdGreaterThanEqualswhere id > = ?
AfterfindByIdAfterwhere id > ?
BeforefindByIdBeforewhere id < ?
IsNullfindByNameIsNullwhere name is null
isNotNull,NotNullfindByNameNotNullwhere name is not null
LikefindByNameLikewhere name like ?
NotLikefindByNameNotLikewhere name not like ?

StartingWith

findByNameStartingWithwhere name like '?%'
EndingWithfindByNameEndingWithwhere name like '%?'
ContainingfindByNameContainingwhere name like '%?%'
OrderByfindByIdOrderByXDescwhere id=? order by x desc
NotfindByNameNotwhere name <> ?
InfindByIdIn(Collection<?> c)where id in (?)
NotInfindByIdNotIn(Collection<?> c)where id not  in (?)
True

findByAaaTue

where aaa = true
FalsefindByAaaFalsewhere aaa = false
IgnoreCasefindByNameIgnoreCasewhere UPPER(name)=UPPER(?)



springboot测试:
@RunWith(SpringJUnit4ClassRunner.class)
// 指定我们SpringBoot工程的Application启动类
@SpringBootTest(classes = { Application.class })
// @SpringApplicationConfiguration(classes = Application.class)
// 由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。
@WebAppConfiguration







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值