DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
company_id BIGINT(20) NOT NULL COMMENT '公司ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',PRIMARY KEY (id));
DROP TABLE IF EXISTS company;
CREATE TABLE company
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '公司名称',PRIMARY KEY (id));
DELETE FROM company;
INSERT INTO company (id, name) VALUES
(1,'HuaWei'),(2,'BYD');
DELETE FROM user;
INSERT INTO user (id, company_id,name, age, email) VALUES
(1,1,'Jone',18,'test1@baomidou.com'),(2,1,'Jack',20,'test2@baomidou.com'),(3,1,'Tom',28,'test3@baomidou.com'),(4,2,'Sandy',21,'test4@baomidou.com'),(5,2,'Billie',24,'test5@baomidou.com');
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.zs.testmybatisplus.mapper.UserMapper"><sql id="Base_Column_List">
t.id, t.name, t.age, t.email,
t.company_id as "company.id",
c.name as "company.name"</sql><select id="selectUserPage" resultType="com.zs.testmybatisplus.entity.User">
SELECT
<include refid="Base_Column_List"/>
FROM user t
LEFT JOIN company c on t.company_id=c.id
<where>
${ew.sqlSegment}</where></select></mapper>
3测试类
packagecom.zs.testmybatisplus;importjava.util.ArrayList;importjava.util.Date;importjava.util.List;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;importcom.baomidou.mybatisplus.core.conditions.query.QueryWrapper;importcom.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;importcom.baomidou.mybatisplus.core.metadata.IPage;importcom.baomidou.mybatisplus.extension.plugins.pagination.Page;importcom.zs.testmybatisplus.entity.Company;importcom.zs.testmybatisplus.entity.User;importcom.zs.testmybatisplus.service.IUserService;@RunWith(SpringRunner.class)@SpringBootTestpublicclassSampleTest{@AutowiredIUserService userService;@TestpublicvoidtestSelectList(){/**
* ==> Preparing: SELECT id,company_id AS "company.id",name,age,email FROM user
*/
userService.list().forEach(t ->System.out.println(t.getCompany()));}@TestpublicvoidtestInsert(){List<User> userList =newArrayList<>();for(int i =0; i <100;++i){Company cmp =newCompany();
cmp.setId(1L);User user =newUser();
user.setId(100L+ i);
user.setCompany(cmp);
user.setName("Han Meimei"+ i);
user.setEmail(user.getName()+"@baomidou.com");
user.setAge(18);
userList.add(user);}// User(id=100, company=Company(id=1, name=null), name=Han Meimei0, age=18, email=Han Meimei0@baomidou.com)/**
* ==> Preparing: INSERT INTO user ( id, company_id, name, age, email ) VALUES ( ?, ?, ?, ?, ? )
* ==> Parameters: 100(Long), 1(Long), Han Meimei0(String), 18(Integer), Han Meimei0@baomidou.com(String)
*/
userService.saveBatch(userList);/**
* ==> Preparing: SELECT id,company_id AS "company.id",name,age,email FROM user
* ==> Parameters:
*///userService.list().forEach(t -> System.out.println(t));testSelect();testUpdate();}@TestpublicvoidtestSelect(){QueryWrapper<User> wrapper =newQueryWrapper<>();
wrapper.eq("t.company_id",1);int pageSize =5;IPage<User> page =newPage<User>(1, pageSize);/**
* ==> Preparing: SELECT t.id, t.name, t.age, t.email, t.company_id as "company.id", c.name as "company
* .name" FROM user t LEFT JOIN company c on t.company_id=c.id WHERE (t.company_id = ?) LIMIT ?
* ==> Parameters: 1(Integer), 5(Long)
* <== Columns: id, name, age, email, company.id, company.name
* <== Row: 1, 1625798644898, 18, test1@baomidou.com, 1, HuaWei
*/List<User> userList = userService.selectUserPage(page, wrapper);for(int i =1; i <= page.getPages();++i){
page =newPage<User>(i, pageSize);
userList = userService.selectUserPage(page, wrapper);System.out.printf("==========================>共%d条数据,当前显示第%d页,每页%d条,共%d页====================================>\n", page.getTotal(), page.getCurrent(), page.getSize(), page.getPages());
userList.forEach(System.out::println);}}@TestpublicvoidtestUpdate(){System.out.println("=======================开始更新==================================================>");UpdateWrapper<User> wrapper =newUpdateWrapper<>();
wrapper.eq("company_id",1);User user =newUser();
user.setName(newDate().getTime()+"");/**
* ==> Preparing: UPDATE user SET name=? WHERE (company_id = ?)
* ==> Parameters: 1625881322081(String), 1(Integer)
*/
userService.update(user, wrapper);testSelect();}}