mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
Merge remote-tracking branch 'origin/1.3' into 1.3
This commit is contained in:
commit
1a1b2041d4
@ -44,6 +44,14 @@
|
||||
<artifactId>mybatis-plus-join-core</artifactId>
|
||||
<version>1.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
<version>2.7.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
@ -28,6 +28,10 @@ import org.springframework.core.annotation.Order;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author yulichang
|
||||
* @since 1.3.2
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
|
||||
@ -47,19 +51,26 @@ public class MybatisPlusJoinAutoConfiguration {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* mybatis plus join 拦截器
|
||||
*/
|
||||
@Bean
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public MPJInterceptor mpjInterceptor() {
|
||||
return new MPJInterceptor();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* mybatis plus 拦截器配置
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnBean(SqlSessionFactory.class)
|
||||
public InterceptorConfig interceptorConfig(List<SqlSessionFactory> sqlSessionFactoryList) {
|
||||
return new InterceptorConfig(sqlSessionFactoryList);
|
||||
return new InterceptorConfig(sqlSessionFactoryList, properties.getBanner());
|
||||
}
|
||||
|
||||
/**
|
||||
* mybatis plus join 自定义方法
|
||||
*/
|
||||
@Bean
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
@ConditionalOnMissingBean({DefaultSqlInjector.class, AbstractSqlInjector.class, ISqlInjector.class})
|
||||
@ -67,12 +78,20 @@ public class MybatisPlusJoinAutoConfiguration {
|
||||
return new MPJSqlInjector();
|
||||
}
|
||||
|
||||
/**
|
||||
* 多表查询注解
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
@Bean
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public MappingConfig mappingConfig() {
|
||||
return new MappingConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* springboot content 工具类
|
||||
*/
|
||||
@Bean
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public SpringContentUtils springContentUtils() {
|
||||
|
@ -5,15 +5,16 @@ import lombok.experimental.Accessors;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Configuration properties for MyBatis.
|
||||
*
|
||||
* @author Eddú Meléndez
|
||||
* @author Kazuki Shimizu
|
||||
* @author yulichang
|
||||
* @since 1.3.2
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@ConfigurationProperties(prefix = "mybatis-plus-join")
|
||||
public class MybatisPlusJoinProperties {
|
||||
|
||||
/**
|
||||
* 打印banner
|
||||
*/
|
||||
private Boolean banner = false;
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
{
|
||||
"groups": [
|
||||
{
|
||||
"sourceType": "com.github.yulichang.autoconfigure.MybatisPlusJoinProperties",
|
||||
"name": "mybatis-plus-join",
|
||||
"type": "com.github.yulichang.autoconfigure.MybatisPlusJoinProperties"
|
||||
}
|
||||
],
|
||||
"properties": [
|
||||
{
|
||||
"name": "mybatis-plus-join.banner",
|
||||
"defaultValue": true,
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "打印 banner."
|
||||
}
|
||||
]
|
||||
}
|
@ -17,16 +17,19 @@ import java.util.List;
|
||||
*
|
||||
* @author yulichang
|
||||
*/
|
||||
//@Configuration
|
||||
//@ConditionalOnBean(SqlSessionFactory.class)
|
||||
//@SuppressWarnings("unused")
|
||||
public class InterceptorConfig {
|
||||
|
||||
|
||||
private static final Log logger = LogFactory.getLog(InterceptorConfig.class);
|
||||
|
||||
public InterceptorConfig(List<SqlSessionFactory> sqlSessionFactoryList) {
|
||||
public InterceptorConfig(List<SqlSessionFactory> sqlSessionFactoryList, Boolean banner) {
|
||||
replaceInterceptorChain(sqlSessionFactoryList);
|
||||
if (banner) {
|
||||
//打印banner
|
||||
System.out.println(" _ _ |_ _ _|_. ___ _ | _ . _ . _ \n" +
|
||||
"| | |\\/|_)(_| | |_\\ |_)||_|_\\ | (_) | | | \n" +
|
||||
" / | / 1.3.0");
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
@ -12,7 +12,6 @@ import org.springframework.context.ApplicationListener;
|
||||
* @author yulichang
|
||||
* @since 1.2.0
|
||||
*/
|
||||
//@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class MappingConfig implements ApplicationListener<ApplicationReadyEvent> {
|
||||
|
||||
@Override
|
||||
|
@ -26,8 +26,6 @@ import static java.util.stream.Collectors.toList;
|
||||
* @author yulichang
|
||||
* @see DefaultSqlInjector
|
||||
*/
|
||||
//@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
//@ConditionalOnMissingBean({DefaultSqlInjector.class, AbstractSqlInjector.class, ISqlInjector.class})
|
||||
public class MPJSqlInjector extends DefaultSqlInjector {
|
||||
|
||||
private static final List<String> METHOD_LIST = Arrays.asList("SelectOne", "SelectCount",
|
||||
|
@ -1,4 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--suppress ALL -->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
@ -33,9 +34,47 @@
|
||||
</scm>
|
||||
|
||||
<properties>
|
||||
<springboot.version>2.5.4</springboot.version>
|
||||
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>${springboot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>${springboot.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>2.1.214</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.24</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.yulichang</groupId>
|
||||
<artifactId>mybatis-plus-join-boot-starter</artifactId>
|
||||
<version>1.3.0</version>
|
||||
</dependency>
|
||||
<!-- PageHelper 兼容性测试 -->
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<version>1.4.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -1,7 +0,0 @@
|
||||
package com.github.yulichang;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.github.yulichang.test;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.github.yulichang.test;
|
||||
|
||||
import com.github.yulichang.test.mapper.UserMapper;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ApplicationContextEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Component
|
||||
@SuppressWarnings("unused")
|
||||
public class ListenerEvent implements ApplicationListener<ApplicationContextEvent> {
|
||||
@Resource
|
||||
private HikariDataSource hikariDataSource;
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
private SqlSessionFactory sqlSessionFactory;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
System.out.println("1");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(@NotNull ApplicationContextEvent applicationContextEvent) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.github.yulichang.test.config;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* mybatis-plus配置
|
||||
*/
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
/**
|
||||
* 分页插件
|
||||
*/
|
||||
@Bean
|
||||
public MybatisPlusInterceptor paginationInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
PaginationInnerInterceptor page = new PaginationInnerInterceptor();
|
||||
page.setOptimizeJoin(false);
|
||||
interceptor.addInnerInterceptor(page);
|
||||
return interceptor;
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.github.yulichang.test.dto;
|
||||
|
||||
import com.github.yulichang.test.entity.AddressDO;
|
||||
import com.github.yulichang.test.enums.Sex;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* userDTO
|
||||
*/
|
||||
@Data
|
||||
@ToString
|
||||
public class UserDTO {
|
||||
/** user */
|
||||
private Integer id;
|
||||
/** user */
|
||||
private String nameName;
|
||||
/** user */
|
||||
private Sex sex;
|
||||
/** user */
|
||||
private String headImg;
|
||||
/** user */
|
||||
private String userHeadImg;//同 headImg 别名测试
|
||||
/** user_address */
|
||||
private String tel;
|
||||
/** user_address */
|
||||
private String address;
|
||||
/** user_address */
|
||||
private String userAddress;
|
||||
/** area */
|
||||
private String province;
|
||||
/** area */
|
||||
private String city;
|
||||
/** area */
|
||||
private String area;
|
||||
|
||||
private List<AddressDO> addressList;
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.github.yulichang.test.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
@Accessors(chain = true)
|
||||
@EqualsAndHashCode
|
||||
@TableName("address")
|
||||
public class AddressDO {
|
||||
|
||||
@TableId
|
||||
private Integer id;
|
||||
|
||||
private Integer userId;
|
||||
|
||||
private Integer areaId;
|
||||
|
||||
private String tel;
|
||||
|
||||
private String address;
|
||||
|
||||
@TableLogic
|
||||
private Boolean del;
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.github.yulichang.test.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
@Accessors(chain = true)
|
||||
@EqualsAndHashCode
|
||||
@TableName("area")
|
||||
public class AreaDO {
|
||||
|
||||
@TableId
|
||||
private Integer id;
|
||||
|
||||
private String province;
|
||||
|
||||
private String city;
|
||||
|
||||
private String area;
|
||||
|
||||
private String postcode;
|
||||
|
||||
@TableLogic
|
||||
private Boolean del;
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.github.yulichang.test.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.github.yulichang.test.enums.Sex;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
@Accessors(chain = true)
|
||||
@EqualsAndHashCode
|
||||
@TableName("`user`")
|
||||
public class UserDO {
|
||||
|
||||
@TableId
|
||||
private Integer id;
|
||||
|
||||
@TableField("name")
|
||||
private String sdafrrvnbioiure;
|
||||
|
||||
private Sex sex;
|
||||
|
||||
private String headImg;
|
||||
|
||||
private Integer addressId;
|
||||
|
||||
@TableLogic
|
||||
private Boolean del;
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.github.yulichang.test.enums;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.EnumValue;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
public enum Sex {
|
||||
|
||||
MAN(0, "男"),
|
||||
|
||||
WOMAN(1, "女");
|
||||
|
||||
@EnumValue
|
||||
private final int code;
|
||||
private final String des;
|
||||
|
||||
Sex(int code, String des) {
|
||||
this.code = code;
|
||||
this.des = des;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.github.yulichang.test.mapper;
|
||||
|
||||
import com.github.yulichang.base.MPJBaseMapper;
|
||||
import com.github.yulichang.test.entity.AddressDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
@SuppressWarnings("unused")
|
||||
public interface AddressMapper extends MPJBaseMapper<AddressDO> {
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.github.yulichang.test.mapper;
|
||||
|
||||
import com.github.yulichang.base.MPJBaseMapper;
|
||||
import com.github.yulichang.test.entity.AreaDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
@SuppressWarnings("unused")
|
||||
public interface AreaMapper extends MPJBaseMapper<AreaDO> {
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.github.yulichang.test.mapper;
|
||||
|
||||
import com.github.yulichang.base.MPJBaseMapper;
|
||||
import com.github.yulichang.test.entity.UserDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper extends MPJBaseMapper<UserDO> {
|
||||
|
||||
}
|
25
mybatis-plus-join-test/src/main/resources/application.yml
Normal file
25
mybatis-plus-join-test/src/main/resources/application.yml
Normal file
@ -0,0 +1,25 @@
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: org.h2.Driver
|
||||
url: jdbc:h2:mem:test
|
||||
username: root
|
||||
password: test
|
||||
sql:
|
||||
init:
|
||||
schema-locations: classpath:db/schema.sql
|
||||
data-locations: classpath:db/data.sql
|
||||
|
||||
|
||||
mybatis-plus:
|
||||
global-config:
|
||||
db-config:
|
||||
logic-delete-field: del
|
||||
logic-delete-value: true
|
||||
logic-not-delete-value: false
|
||||
banner: true
|
||||
configuration:
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
|
||||
# 打印 mybatis plus join banner
|
||||
mybatis-plus-join:
|
||||
banner: true
|
79
mybatis-plus-join-test/src/main/resources/db/data.sql
Normal file
79
mybatis-plus-join-test/src/main/resources/db/data.sql
Normal file
@ -0,0 +1,79 @@
|
||||
-- noinspection SqlDialectInspectionForFile
|
||||
|
||||
-- noinspection SqlNoDataSourceInspectionForFile
|
||||
|
||||
DELETE FROM area;
|
||||
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10001, '北京市01', '北京01', '朝阳01', '80001', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10002, '北京市02', '北京02', '朝阳02', '80002', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10003, '北京市03', '北京03', '朝阳03', '80003', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10004, '北京市04', '北京04', '朝阳04', '80004', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10005, '北京市05', '北京05', '朝阳05', '80005', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10006, '北京市06', '北京06', '朝阳06', '80006', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10007, '北京市07', '北京07', '朝阳07', '80007', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10008, '北京市08', '北京08', '朝阳08', '80008', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10009, '北京市09', '北京09', '朝阳09', '80009', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10010, '北京市10', '北京10', '朝阳10', '80010', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10011, '北京市11', '北京11', '朝阳11', '80011', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10012, '北京市12', '北京12', '朝阳12', '80012', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10013, '北京市13', '北京13', '朝阳13', '80013', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10014, '北京市14', '北京14', '朝阳14', '80014', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10015, '北京市15', '北京15', '朝阳15', '80015', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10016, '北京市16', '北京16', '朝阳16', '80016', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10017, '北京市17', '北京17', '朝阳17', '80017', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10018, '北京市18', '北京18', '朝阳18', '80018', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10019, '北京市19', '北京19', '朝阳19', '80019', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10020, '北京市20', '北京20', '朝阳20', '80020', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10021, '北京市21', '北京21', '朝阳21', '80021', false);
|
||||
INSERT INTO area (id, province, city, area, postcode, del) VALUES (10022, '北京市22', '北京22', '朝阳22', '80022', false);
|
||||
|
||||
DELETE FROM `user`;
|
||||
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 1, '张三01', 1, 1, 'https://url-01', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 2, '李四02', 1, 0, 'https://url-02', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 3, '李四02', 1, 0, 'https://url-03', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 4, '李四04', 1, 0, 'https://url-04', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 5, '李四05', 1, 0, 'https://url-05', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 6, '李四06', 1, 0, 'https://url-06', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 7, '李四07', 1, 0, 'https://url-07', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 8, '李四08', 1, 0, 'https://url-08', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 9, '李四09', 1, 0, 'https://url-09', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (10, '李四10', 1, 0, 'https://url-10', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (11, '李四11', 1, 0, 'https://url-11', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (12, '李四12', 1, 0, 'https://url-12', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (13, '李四13', 1, 0, 'https://url-13', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (14, '李四14', 1, 0, 'https://url-14', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (15, '李四15', 1, 0, 'https://url-15', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (16, '李四16', 1, 0, 'https://url-16', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (17, '李四17', 1, 0, 'https://url-17', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (18, '李四18', 1, 0, 'https://url-18', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (19, '李四19', 1, 0, 'https://url-19', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (20, '李四20', 1, 0, 'https://url-20', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (21, '李四21', 1, 0, 'https://url-21', false);
|
||||
INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (22, '李四22', 1, 0, 'https://url-22', false);
|
||||
|
||||
|
||||
DELETE FROM address;
|
||||
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 1, 1, 10001, '10000000001', '曹县01', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 2, 1, 10002, '10000000002', '曹县02', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 3, 1, 10003, '10000000003', '曹县03', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 4, 1, 10004, '10000000004', '曹县04', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 5, 1, 10005, '10000000005', '曹县05', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 6, 1, 10006, '10000000006', '曹县06', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 7, 1, 10007, '10000000007', '曹县07', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 8, 1, 10008, '10000000008', '曹县08', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 9, 1, 10009, '10000000009', '曹县09', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (10,10, 10010, '10000000010', '曹县10', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (11,11, 10011, '10000000011', '曹县11', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (12,12, 10012, '10000000012', '曹县12', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (13,13, 10013, '10000000013', '曹县13', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (14,14, 10014, '10000000014', '曹县14', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (15,15, 10015, '10000000015', '曹县15', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (16,16, 10016, '10000000016', '曹县16', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (17,17, 10017, '10000000017', '曹县17', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (18,18, 10018, '10000000018', '曹县18', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (19,19, 10019, '10000000019', '曹县19', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (20,20, 10020, '10000000020', '曹县20', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (21,21, 10021, '10000000021', '曹县21', false);
|
||||
INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (22,22, 10022, '10000000022', '曹县22', false);
|
42
mybatis-plus-join-test/src/main/resources/db/schema.sql
Normal file
42
mybatis-plus-join-test/src/main/resources/db/schema.sql
Normal file
@ -0,0 +1,42 @@
|
||||
-- noinspection SqlDialectInspectionForFile
|
||||
|
||||
-- noinspection SqlNoDataSourceInspectionForFile
|
||||
|
||||
DROP TABLE IF EXISTS area;
|
||||
|
||||
create table area
|
||||
(
|
||||
id int auto_increment
|
||||
primary key,
|
||||
province varchar(255) null,
|
||||
city varchar(255) null,
|
||||
area varchar(255) null,
|
||||
postcode varchar(255) null,
|
||||
del bit
|
||||
);
|
||||
|
||||
DROP TABLE IF EXISTS `user`;
|
||||
|
||||
create table `user`
|
||||
(
|
||||
id int auto_increment
|
||||
primary key,
|
||||
`name` varchar(255) not null,
|
||||
`address_id` int not null,
|
||||
sex tinyint not null,
|
||||
head_img varchar(255) not null,
|
||||
del bit
|
||||
);
|
||||
|
||||
DROP TABLE IF EXISTS address;
|
||||
|
||||
create table address
|
||||
(
|
||||
id int auto_increment
|
||||
primary key,
|
||||
user_id int null,
|
||||
area_id int null,
|
||||
tel varchar(255) null,
|
||||
address varchar(255) null,
|
||||
del bit
|
||||
);
|
@ -0,0 +1,130 @@
|
||||
package com.github.yulichang.test;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.github.yulichang.test.dto.UserDTO;
|
||||
import com.github.yulichang.test.entity.AddressDO;
|
||||
import com.github.yulichang.test.entity.AreaDO;
|
||||
import com.github.yulichang.test.entity.UserDO;
|
||||
import com.github.yulichang.test.mapper.UserMapper;
|
||||
import com.github.yulichang.toolkit.MPJWrappers;
|
||||
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 连表测试类
|
||||
* <p>
|
||||
* 支持mybatis-plus 查询枚举字段
|
||||
* 支持mybatis-plus typeHandle功能
|
||||
* <p>
|
||||
* 移除了mybatis-plus 逻辑删除支持,逻辑删除需要在连表查询时自己添加对应的条件
|
||||
*/
|
||||
@SpringBootTest
|
||||
@SuppressWarnings("unused")
|
||||
class JoinTest {
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
@Resource
|
||||
private SqlSessionFactory sqlSessionFactory;
|
||||
|
||||
/**
|
||||
* 一对多
|
||||
*/
|
||||
@Test
|
||||
void testJoin() {
|
||||
MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
|
||||
.selectAll(UserDO.class)
|
||||
.selectCollection(AddressDO.class, UserDTO::getAddressList)
|
||||
.leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)
|
||||
.orderByDesc(UserDO::getId);
|
||||
|
||||
List<UserDTO> list = userMapper.selectJoinList(UserDTO.class, wrapper);
|
||||
list.forEach(System.out::println);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 简单的分页关联查询 lambda
|
||||
*/
|
||||
@Test
|
||||
void test1() {
|
||||
IPage<UserDTO> iPage = userMapper.selectJoinPage(new Page<>(1, 10), UserDTO.class,
|
||||
MPJWrappers.<UserDO>lambdaJoin()
|
||||
.selectAll(UserDO.class)
|
||||
.select(AddressDO::getAddress)
|
||||
.select(AreaDO::getProvince)
|
||||
.leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)
|
||||
.leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId));
|
||||
iPage.getRecords().forEach(System.out::println);
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单的分页关联查询 lambda
|
||||
* ON语句多条件
|
||||
*/
|
||||
@Test
|
||||
void test3() {
|
||||
IPage<UserDTO> page = userMapper.selectJoinPage(new Page<>(1, 10), UserDTO.class,
|
||||
MPJWrappers.<UserDO>lambdaJoin()
|
||||
.selectAll(UserDO.class)
|
||||
.select(AddressDO::getAddress)
|
||||
.leftJoin(AddressDO.class, on -> on
|
||||
.eq(UserDO::getId, AddressDO::getUserId)
|
||||
.eq(UserDO::getId, AddressDO::getUserId))
|
||||
.eq(UserDO::getId, 1)
|
||||
.and(i -> i.eq(UserDO::getHeadImg, "er")
|
||||
.or()
|
||||
.eq(AddressDO::getUserId, 1))
|
||||
.eq(UserDO::getId, 1));
|
||||
page.getRecords().forEach(System.out::println);
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单的函数使用
|
||||
*/
|
||||
@Test
|
||||
void test4() {
|
||||
UserDTO one = userMapper.selectJoinOne(UserDTO.class, MPJWrappers.<UserDO>lambdaJoin()
|
||||
.selectSum(UserDO::getId)
|
||||
.selectMax(UserDO::getId, UserDTO::getHeadImg)
|
||||
.leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)
|
||||
.eq(UserDO::getId, 1));
|
||||
System.out.println(one);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 忽略个别查询字段
|
||||
*/
|
||||
@Test
|
||||
void test6() {
|
||||
IPage<UserDTO> page = userMapper.selectJoinPage(new Page<>(1, 10), UserDTO.class,
|
||||
MPJWrappers.<UserDO>lambdaJoin()
|
||||
.selectAll(UserDO.class)
|
||||
.select(AddressDO.class, p -> true)
|
||||
.select(AddressDO::getAddress)
|
||||
.leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)
|
||||
.eq(UserDO::getId, 1));
|
||||
page.getRecords().forEach(System.out::println);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 关联查询返回map
|
||||
*/
|
||||
@Test
|
||||
void test7() {
|
||||
List<Map<String, Object>> list = userMapper.selectJoinMaps(MPJWrappers.<UserDO>lambdaJoin()
|
||||
.selectAll(UserDO.class)
|
||||
.select(AddressDO::getAddress)
|
||||
.leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)
|
||||
.eq(UserDO::getId, 1));
|
||||
list.forEach(System.out::println);
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.github.yulichang.test;
|
||||
|
||||
import com.github.yulichang.test.mapper.UserMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 测试
|
||||
*/
|
||||
@SpringBootTest
|
||||
@SuppressWarnings("unused")
|
||||
class MPJ131Test {
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
|
||||
/**
|
||||
* 先明确 你要干啥?
|
||||
* 在mapper中定义lambda形式的resultMap?
|
||||
* 要解决问题
|
||||
* 1、只初始化了部分数据库实体类? NPE
|
||||
* <p>
|
||||
* stater
|
||||
* core
|
||||
* anntion
|
||||
* test
|
||||
* <p>
|
||||
* 超高自由度的sql拼接器?
|
||||
* 解决了自连接和子查询和各种函数
|
||||
* 如果底层调用MPJLambdaWrapper那样还有什么意义
|
||||
*/
|
||||
@Test
|
||||
void testJoin() {
|
||||
System.out.println(1);
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
package com.github.yulichang.test;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.github.yulichang.query.MPJQueryWrapper;
|
||||
import com.github.yulichang.test.dto.UserDTO;
|
||||
import com.github.yulichang.test.entity.UserDO;
|
||||
import com.github.yulichang.test.mapper.UserMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@SpringBootTest
|
||||
class QueryWrapperTest {
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
|
||||
/**
|
||||
* 链表查询
|
||||
*/
|
||||
@Test
|
||||
void test1() {
|
||||
UserDO userDO = userMapper.selectJoinOne(UserDO.class, new MPJQueryWrapper<UserDO>()
|
||||
.selectAll(UserDO.class).last("LIMIT 1"));
|
||||
System.out.println(userDO);
|
||||
|
||||
UserDTO dto = userMapper.selectJoinOne(UserDTO.class, new MPJQueryWrapper<UserDO>()
|
||||
.selectAll(UserDO.class)
|
||||
.select("name AS nameName")
|
||||
.last("LIMIT 1"));
|
||||
System.out.println(dto);
|
||||
}
|
||||
|
||||
@Test
|
||||
void test2() {
|
||||
List<UserDO> userDO = userMapper.selectJoinList(UserDO.class, new MPJQueryWrapper<UserDO>()
|
||||
.selectAll(UserDO.class)
|
||||
.leftJoin("address t2 on t2.user_id = t.id")
|
||||
.le("t.id ", 10));
|
||||
System.out.println(userDO);
|
||||
|
||||
List<UserDTO> dto = userMapper.selectJoinList(UserDTO.class, new MPJQueryWrapper<UserDO>()
|
||||
.selectAll(UserDO.class)
|
||||
.select("t2.address AS userAddress")
|
||||
.leftJoin("address t2 on t2.user_id = t.id")
|
||||
.le("t.id ", 10));
|
||||
System.out.println(dto);
|
||||
}
|
||||
|
||||
@Test
|
||||
void test3() {
|
||||
IPage<UserDO> userDO = userMapper.selectJoinPage(new Page<>(1, 10), UserDO.class, new MPJQueryWrapper<UserDO>()
|
||||
.selectAll(UserDO.class)
|
||||
.leftJoin("address t2 on t2.user_id = t.id")
|
||||
.lt("t.id ", 5));
|
||||
System.out.println(userDO);
|
||||
|
||||
IPage<UserDTO> dto = userMapper.selectJoinPage(new Page<>(1, 10), UserDTO.class, new MPJQueryWrapper<UserDO>()
|
||||
.selectAll(UserDO.class)
|
||||
.select("t2.address AS userAddress")
|
||||
.leftJoin("address t2 on t2.user_id = t.id")
|
||||
.lt("t.id ", 5));
|
||||
System.out.println(dto);
|
||||
}
|
||||
|
||||
@Test
|
||||
void test4() {
|
||||
List<Map<String, Object>> maps = userMapper.selectJoinMaps(new MPJQueryWrapper<UserDO>()
|
||||
.selectAll(UserDO.class)
|
||||
.leftJoin("address t2 on t2.user_id = t.id")
|
||||
.lt("t.id ", 5));
|
||||
System.out.println(maps);
|
||||
|
||||
List<Map<String, Object>> joinMaps = userMapper.selectJoinMaps(new MPJQueryWrapper<UserDO>()
|
||||
.selectAll(UserDO.class)
|
||||
.select("t2.address AS userAddress")
|
||||
.leftJoin("address t2 on t2.user_id = t.id")
|
||||
.lt("t.id ", 5));
|
||||
System.out.println(joinMaps);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user