4.0.0org.springframework.bootspring-boot-starter-parent2.1.6.RELEASEcom.xmlxyseasgame0.0.1-SNAPSHOTseasgameDemo project for Spring Boot1.8org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-starter-data-jpaMySQLmysql-connector-javaruntimeorg.springframework.bootspring-boot-devtoolsruntimetrueorg.projectlomboklomboktrueorg.springframework.bootspring-boot-starter-data-jpaio.springfoxspringfox-swagger22.8.0io.springfoxspringfox-swagger-ui2.8.0org.springframework.bootspring-boot-configuration-processortrueorg.springframework.bootspring-boot-starter-securitynet.sf.json-libjson-lib2.2.2jdk15com.belerwebpinyin4j2.5.1org.springframework.bootspring-boot-starter-thymeleafjavax.servletjavax.servlet-api3.1.0providedwarorg.springframework.bootspring-boot-maven-pluginorg.apache.maven.pluginsmaven-compiler-plugin1.81.8seasgameorg.apache.maven.pluginsmaven-compiler-plugin2.3.2${project.build.sourceEncoding}1.71.7org.apache.maven.pluginsmaven-surefire-plugintrue
@Query
当一个SQL较为复杂时,第一个想到的就是原生的SQL语句。如果只是简单的查询,那情况还没这么糟糕
@Query(value = " SELECT IFNULL(sum(right_num),0) sumRight FROM t_record WHERE record_owner_id = ?1 AND responder_no = ?2 ",nativeQuery = true)
Map sumRightNum(int studentId,int responderNo);
但如果需要进行动态查询,或更改,那这个value就变得复杂了。
package com.xmlxy.seasgame.dao;
import com.xmlxy.seasgame.entity.ScoreEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
*
* Description:
* @author hwc
* @date 2019/9/5
* @return
*/
public interface ScoreDao extends CrudRepository
{
/**
*
* Description:
*@param scoreEntity
* @author hwc
* @date 2019/9/6
*/
@Transactional(rollbackFor = Exception.class)
@Modifying
@Query(value = "UPDATE t_score t SET " +
"t.responder_no = CASE WHEN :#{#scoreEntity.responderNo} IS NULL THEN t.responder_no ELSE :#{#scoreEntity.responderNo} END," +
"t.max_level = CASE WHEN :#{#scoreEntity.maxLevel} IS NULL THEN t.max_level ELSE :#{#scoreEntity.maxLevel} END," +
"t.right_num = CASE WHEN :#{#scoreEntity.rightNum} IS NULL THEN t.right_num ELSE :#{#scoreEntity.rightNum} END," +
"t.use_time = CASE WHEN :#{#scoreEntity.userTime} IS NULL THEN t.use_time ELSE :#{#scoreEntity.userTime} END WHERE student_id = :#{#scoreEntity.getStudentId()}",nativeQuery = true)
void updateScore(@Param("scoreEntity") ScoreEntity scoreEntity);
}