分页查询,没法显示,查询不到结果
package com.itcast.controller;
import java.util.Date;
import java.util.List;
import org.n3r.idworker.Sid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.itcast.entity.SysUser;
import com.itcast.service.SysUserService;
import com.itcast.utils.ItCastJSONResult;
/**
* mybatis springboot整合CURDController
* 访问路径:http://localhost:8080/mybatis/saveUser
* 访问路径:http://localhost:8080/mybatis/updateUser
* 访问路径:http://localhost:8080/mybatis/deleteUser
* 访问路径:http://localhost:8080/mybatis/queryUserById?userId=1001
* 访问路径:http://localhost:8080/mybatis/queryUserList
* 访问路径:http://localhost:8080/mybatis/queryUserListPaged
* 访问路径:http://localhost:8080/mybatis/queryUserByIdCustom?userId=1001
* 访问路径:http://localhost:8080/mybatis/saveUserTransactional
* @author jack
*
*/
@RestController
@RequestMapping("mybatis")
public class MyBatisCURDController {
final static Logger log=LoggerFactory.getLogger(MyBatisCURDController.class);
@Autowired
private Sid sid;
@Autowired
private SysUserService userService;
@RequestMapping("/saveUser")
public ItCastJSONResult saveUser() throws Exception{
log.info("保存用户,当前时间:{},操作人:{}",new Date(),"jack");
String userid=sid.nextShort();
SysUser user=new SysUser();
user.setId(userid);
user.setUsername("轩辕花狐貂"+new Date());
user.setNickname("花狐貂"+new Date());
user.setPassword("abc123456");
user.setIsDelete(0);
user.setRegistTime(new Date());
userService.saveUser(user);
return ItCastJSONResult.ok("插入成功");
}
@RequestMapping("/updateUser")
public ItCastJSONResult updateUser(){
SysUser user=new SysUser();
user.setId("1001");
user.setUsername("mybatisUser"+new Date());
user.setNickname("mybatisUser"+new Date());
user.setPassword("mybatisUser");
user.setIsDelete(0);
user.setRegistTime(new Date());
userService.updateUser(user);
return ItCastJSONResult.ok("更新成功");
}
@RequestMapping("/deleteUser")
public ItCastJSONResult deleteUser(String userId) {
userService.deleteUser(userId);
return ItCastJSONResult.ok("删除成功");
}
@RequestMapping("/queryUserById")
public ItCastJSONResult queryUserById(String userId) {
log.info("userId="+userId);
log.info("queryUserById查询到的结果"+userService.queryUserById(userId));
return ItCastJSONResult.ok(userService.queryUserById(userId));
}
@RequestMapping("/queryUserList")
public ItCastJSONResult queryUserList() {
SysUser user = new SysUser();
/* user.setUsername("test");
user.setNickname("test");*/
List<SysUser> userList = userService.queryUserList(user);
return ItCastJSONResult.ok(userList);
}
@RequestMapping("/queryUserListPaged")
public ItCastJSONResult queryUserListPaged(Integer page) {
if (page == null) {
page = 1;
}
int pageSize = 3;
SysUser user = new SysUser();
// user.setNickname("lee");
List<SysUser> userList = userService.queryUserListPaged(user, page, pageSize);
log.info("userList的大小:"+userList.size());
log.info("userList:"+userList);
return ItCastJSONResult.ok(userList);
}
@RequestMapping("/queryUserByIdCustom")
public ItCastJSONResult queryUserByIdCustom(String userId) {
return ItCastJSONResult.ok(userService.queryUserByIdCustom(userId));
}
@RequestMapping("/saveUserTransactional")
public ItCastJSONResult saveUserTransactional() {
String userId = sid.nextShort();
SysUser user = new SysUser();
user.setId(userId);
user.setUsername("lee" + new Date());
user.setNickname("lee" + new Date());
user.setPassword("abc123");
user.setIsDelete(0);
user.setRegistTime(new Date());
userService.saveUserTransactional(user);
return ItCastJSONResult.ok("保存成功");
}
}
==================================================
package com.itcast.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.itcast.entity.SysUser;
import com.itcast.mapper.SysUserMapper;
import com.itcast.mapper.SysUserMapperCustom;
import com.itcast.service.SysUserService;
import tk.mybatis.mapper.entity.Example;
import org.thymeleaf.util.StringUtils;
@Service
public class SysUserServiceImpl implements SysUserService {
@Autowired
private SysUserMapper userMapper;
@Autowired
private SysUserMapperCustom userMapperCustom;
@Override
public void saveUser(SysUser user) throws Exception {
userMapper.insert(user);
}
@Override
public void updateUser(SysUser user) {
userMapper.updateByPrimaryKeySelective(user);
}
@Override
public void deleteUser(String userId) {
// TODO Auto-generated method stub
userMapper.deleteByPrimaryKey(userId);
}
@Override
public SysUser queryUserById(String userId) {
return userMapper.selectByPrimaryKey(userId);
}
@Override
public List<SysUser> queryUserList(SysUser user) {
try {
Example example = new Example(SysUser.class);
Example.Criteria criteria = example.createCriteria();
if (!StringUtils.isEmptyOrWhitespace(user.getUsername())) {
// criteria.andEqualTo("username", user.getUsername());
criteria.andLike("username", "%" + user.getUsername() + "%");
}
if (!StringUtils.isEmptyOrWhitespace(user.getNickname())) {
criteria.andLike("nickname", "%" + user.getNickname() + "%");
}
List<SysUser> userList = userMapper.selectByExample(example);
return userList;
} catch (Exception ex) {
System.out.println(ex.getMessage());
// TODO: handle exception
return null;
}
}
@Override
public List<SysUser> queryUserListPaged(SysUser user, Integer page, Integer pageSize) {
// 开始分页
PageHelper.startPage(page, pageSize);
Example example = new Example(SysUser.class);
Example.Criteria criteria = example.createCriteria();
if (StringUtils.isEmptyOrWhitespace(user.getNickname())) {
criteria.andLike("nickname", "%" + user.getNickname() + "%");
}
example.orderBy("registTime").desc();
List<SysUser> userList = userMapper.selectByExample(example);
return userList;
}
@Override
public SysUser queryUserByIdCustom(String userId) {
List<SysUser> userList = userMapperCustom.queryUserSimplyInfoById(userId);
if (userList != null && !userList.isEmpty()) {
return (SysUser) userList.get(0);
}
return null;
}
@Override
public void saveUserTransactional(SysUser user) {
userMapper.insert(user);
int a = 1 / 0;
user.setIsDelete(1);
userMapper.updateByPrimaryKeySelective(user);
}
}