1. 程式人生 > >Mybatis-plus多表關聯查詢,多表分頁查詢

Mybatis-plus多表關聯查詢,多表分頁查詢

學習plus真的覺得寫程式碼真的越來越舒服了。昨天開始接觸吧,只要學會了多表關聯查詢。plus就能隨意搭配使用了。

關於怎麼搭建的就自行了去研究了哦。這裡直接進入主題。

我用的是springboot+mybatis-plus。新專案我打算以後都是這樣了。

 

配置一個分頁外掛!

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();
	}
}

 

Controller

	@RequestMapping("syscal")
	public Object syscal() {
		
		Page<Map<String, Object>> page=userService.selectListPage(1, 2);
		
		return page.getRecords();

	}

 

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

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));
		
	}

}

 

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);

}

 

結果

  

 

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', '[email protected]');
INSERT INTO `user` VALUES ('2', '行政村v', '22', '[email protected]');
INSERT INTO `user` VALUES ('3', '查詢啊', '60', '[email protected]');
INSERT INTO `user` VALUES ('4', '才表報錯', '30', '32弟媳婦[email protected]');
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');

 

 只要mybatis基礎夠好這個很好理解的