因为导错包, 我把JPQL换成了Querydsl, 我佛了
前言
{ "success": false, "message": "For queries with named parameters you need to use provide names for method parameters. Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters.; nested exception is java.lang.IllegalStateException: For queries with named parameters you need to use provide names for method parameters. Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters.", "payload": { "data": null } }
正确导入包
需要导入
import org.springframework.data.repository.query.Param;
导入
import org.apache.ibatis.annotations.Param;
时否则会报错
java.lang.IllegalStateException: For queries with named parameters you need to use provide names for method parameters. Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters.
或者不用@Param注解采用占位符的方式也是ok的
@Modifying @Query("delete from UserRolePo u where u.roleId = ?1") void deleteRelationByRoleId(Integer roleId);
另外, 在用Querydsl查询过程中又因为导错包发现predicates add异常, 正确导包
querydsl查询service层代码如下
list()方法功能是查询code或name like keyword的user信息
freeze方法用于修改用户status
package com.cebbank.api.service.impl; import com.cebbank.api.model.po.QUserPo; import com.cebbank.api.model.po.UserPo; import com.cebbank.api.model.qo.UserQo; import com.cebbank.api.model.vo.SysUserVo; import com.cebbank.api.model.vo.UserVo; import com.cebbank.api.repository.RoleRepository; import com.cebbank.api.repository.UserRepository; import com.cebbank.api.service.UserService; import com.cebbank.common.exception.UnexpectedStatusException; import com.google.common.collect.Lists; import com.querydsl.core.types.Predicate; import com.querydsl.core.types.Projections; import com.querydsl.jpa.impl.JPAQuery; import com.querydsl.jpa.impl.JPAQueryFactory; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; import static com.cebbank.api.common.CebStatus.SECTION_NOT_EXISTS; @Service public class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; @Autowired private RoleRepository roleRepository; @Autowired private JPAQueryFactory jpaQueryFactory; @Override public SysUserVo list(UserQo qo) { QUserPo userPo = QUserPo.userPo; List<Predicate> predicates = Lists.newArrayListWithExpectedSize(10); if (StringUtils.isNotBlank(qo.getKeyword())) { String likeStr = "%" + qo.getKeyword() + "%"; predicates.add(userPo.name.like(likeStr).or(userPo.code.like(likeStr))); } JPAQuery<UserVo> selectService = jpaQueryFactory.select(Projections.bean(UserVo.class, userPo.id.as("userId"), userPo.code.as("code"), userPo.name.as("name"), userPo.status.as("status") )).from(userPo); JPAQuery<UserVo> finalQuery = selectService .where(predicates.toArray(new Predicate[predicates.size()])) .offset((qo.getPageNo() - 1) * qo.getPageSize()) .limit(qo.getPageSize()); List<UserVo> userVos = finalQuery.fetch(); userVos.forEach(userVo -> { userVo.setRolePo(roleRepository.findRolesByUserId(userVo.getUserId())); }); long total = finalQuery.fetchCount(); SysUserVo sysUserVo = SysUserVo.builder() .list(userVos) .total(total) .build(); return sysUserVo; } @Override @Transactional public void freeze(UserQo qo) { UserPo userPo = userRepository.findById(qo.getUserId()) .orElseThrow(() -> new UnexpectedStatusException(SECTION_NOT_EXISTS, qo.getUserId())); userPo.setStatus(qo.getStatus()); userRepository.save(userPo); } }
import org.springframework.data.repository.query.Param;
作者:习惯沉淀
如果文中有误或对本文有不同的见解,欢迎在评论区留言。
如果觉得文章对你有帮助,请点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
扫码关注一线码农的学习见闻与思考。
回复"大数据","微服务","架构师","面试总结",获取更多学习资源!