import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
@EnableTransactionManagement
@Configuration
@MapperScan("com.szpdc.Dao")//输入你的dao层的包
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
/**
* SQL执行效率插件
*/
@Bean
// @Profile({ "dev", "test" }) // 设置 dev test 环境开启
public PerformanceInterceptor performanceInterceptor() {
return new PerformanceInterceptor();
}
}
Dao
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Select;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
/**
*
*@Author By Yirs
*@Date 2018-12-01 22:27:13
*/
public interface UserMapper extends BaseMapper<User> {
/**
* 为了区别plus原来的查询select*的字段 ,自定义为dy
* @param 分页查询
* @return 多表关联查询
*/
@Select("SELECT a.id,a.`name`,a.age,b.`describe` FROM USER a LEFT JOIN userinfo b ON a.id = b.user_id where a.id=#{id}")
List<Map<String, Object>> dyGetUserList(Page<Map<String,Object>> page,Integer id);
}
service
import java.util.Map;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
public interface UserService {
Page<Map<String,Object>> selectListPage(int current, int number);
}
ServiceImpl
ServiceImpl
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
/**
*
* @Author By Yirs
* @Date 2018-12-01 21:18:06
* @Description mybatis-plus 在服务层继承了ServiceImpl 就有了 UserMapper的 就少了以前的@Autowired 注入dao层
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Override
public Page<Map<String,Object>> selectListPage(int current, int number) {
// 新建分页
Page<Map<String,Object>> page = new Page<Map<String,Object>>(current, number);
// 返回分页结果 1为id
return page.setRecords(this.baseMapper.dyGetUserList(page,1));
}
}
controller
@RequestMapping("syscal")
public Object syscal() {
Page<Map<String, Object>> page=userService.selectListPage(1, 2);
return page.getRecords();
}
测试sql
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`email` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '谢谢', '18', '32as1d564@qq.com');
INSERT INTO `user` VALUES ('2', '行政村v', '22', '3234564@qq.com');
INSERT INTO `user` VALUES ('3', '查询啊', '60', '35asd564@qq.com');
INSERT INTO `user` VALUES ('4', '才表报错', '30', '32弟媳妇64@qq.com');
INSERT INTO `user` VALUES ('5', 'asdasd', '22', 'sdfsad');
INSERT INTO `user` VALUES ('6', 'dsfds', '32', 'sdafsdf');
INSERT INTO `user` VALUES ('7', 'sdfxczv', '33', 'xcvbvchrwt');
INSERT INTO `user` VALUES ('8', 'dfsalkcxjv', '99', 'jklsdhcfvlkjsda');
INSERT INTO `user` VALUES ('9', 'jskdchvc,m', '34', 'dvclkjh');
-- ------------------------------ ------------------------------ ----------------------------
DROP TABLE IF EXISTS `userinfo`;
CREATE TABLE `userinfo` (
`id` int(11) NOT NULL,
`user_id` int(11) DEFAULT NULL,
`describe` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of userinfo
-- ----------------------------
INSERT INTO `userinfo` VALUES ('1', '1', '用户1');