package com.liu.user.dao;
import com.liu.user.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
/**
* Created by Administrator on 2019/8/15 0015.
*
* concat() 字符串拼接很好用。记得这个是mysql的函数
* 在param 导包的过程中,记得导包别倒错,要不然会查不到数据,或者报错
* 这里还支持原生的sql。视情况而选择相应技术类型
* 在使用原生的sql实例:
*
*
*/
public interface UserDao extends JpaRepository<User,String>,JpaSpecificationExecutor<User> {
// 这个是hibernate的方式
// @Query(value = " select u from User u where u.loginName like concat('%',:query,'%') or u.cardId like concat('%',:query,'%') ")
// 这种方式代表使用原生的sql查询方式
// @Query(value = "select u.* from boot_user u where u.login_name like concat('%',:query,'%') or u.card_id like concat('%',:query,'%')",nativeQuery = true)
// 占位符的使用
@Query(value = " select u from User u where u.loginName like concat('%',?1,'%') or u.cardId like concat('%',?1,'%') ")
Page<User> findSearch(@Param("query") String query, Pageable pageable);
}
2.4 拓展service类
package com.liu.user.service;
import com.liu.common.entity.PageResult;
import com.liu.user.dao.UserDao;
import com.liu.user.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by Administrator on 2019/8/15 0015.
* @service 用来标准他是一个service层的实现类
* @Slf4j 用来生成一个log的内置对象,这样我们就可以使用log 来记录日志
*
*/
@Service
@Slf4j
public class UserService {
@Autowired
private RedisTemplate redisTemplate ;
@Autowired
private UserDao userDao;
public PageResult findAllByPage(int page, int rows, String query){
Pageable pageable= PageRequest.of(page-1,rows);
log.info("query:{}",query);
Page<User> users = userDao.findSearch(query,pageable);
log.info("{}",users.getContent());
return new PageResult(users.getTotalElements(),users.getContent());
}
}
2.5 配套controller 类
package com.liu.user.controller;
import com.liu.common.entity.PageResult;
import com.liu.common.entity.Result;
import com.liu.user.entity.User;
import com.liu.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Administrator on 2019/8/15 0015.
* @RestController 表示该控制器所有的返回均为josn格式的对象
* @RequestMapping("/user") 表示该控制器作用域为/user
* @Slf4j 生成log的内置对象,用于记录日志
*/
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
@Autowired
private UserService userService;
/**
* @GetMapping(value = "/{id}") 表示书写接口的风格为restful风格 ,调用的方式为get,同时,使用路径参数
* @param id
* @return
*/
@GetMapping(value = "/{id}")
public Result findById(@PathVariable("id") String id){
User user = userService.findById(id);
return new Result(true,1,"查询成功",user);
}
/**
* @GetMapping(value = "/{id}") 表示书写接口的风格为restful风格 ,调用的方式为get,同时,使用路径参数
* @param page rows query
* @return
*/
@GetMapping(value = "/{page}/{rows}/{query}")
public Result findByList(@PathVariable(value = "page" ,required = false) int page , @PathVariable("rows") int rows, @PathVariable("query") String query){
log.info(" page{}",page);
log.info("rows{}",rows);
log.info("query{}",query);
PageResult pageResult= userService.findAllByPage(page,rows,query);
return new Result(true,1,"查询成功",pageResult);
}
}