Mybatis-plus之映射篇(部分注解使用)

1年前 (2022) 程序员胖胖胖虎阿
127 0 0

1. pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.atguigu</groupId>
    <artifactId>mybatis_plus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis_plus</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1</version>
        </dependency>
        <!--mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--lombok用来简化实体类-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- JSON ↔ Object -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>
        <!-- IOC装配,池化技术 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.7.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2. 注解分析

前言,Mybatis-plus中默认开启驼峰式下划线转换映射。即库中 top_id 查到数据后—》会映射到实体类中 topId 该属性上。
可以手动关闭:

mybatis-plus.configuration.map-underscore-to-camel-case=false

关闭后 top_id —》top_id

2.1 @TableName

属性 类型 必须 默认 描述
value String “” 表名,不加则默认 类名
schema String “” schema(@since 3.1.1)
keepGlobalPrefix String false 使用全局的 tablePrefix 值?
resultMap String “” xml 中 resultMap 的id
autoResultMap String false 是否自动构建 resultMap (设置了resultMap则无效)
excludeProperty String[] [ ] 需要排除的属性名

schema 在数据库中,schema是数据库的组织和结构。不同的数据库schema的作用有差异。例如:

 Mysql(一般不使用):  在MySQL中基本认为schema和数据库相同,也就是说schema的名称和数据库的实例的名称相同,一个数据库有一个schema。

PostgreSQL(同下):  而在PostgreSQL中,可以创建一个数据库,然后在数据库中,创建不同的schema,每个schema又有着一些各自的表,索引等。

Oracle(一个库中不同空间有两个同名的表): 类似pgsql,可以理解为表空间。

resultMap 需要手动在xml写映射。
例如:

@TableName(resultMap = "userRole")  //采用xml中 id = userRole 的映射。
public class User  {
    private Integer id;
    private String name;
    private String address;
    private String sex;

    //一个用户对应多个账户,也就是一个User有多个Account对象。
    private List<Account> account; //对应collection中的 property="account"

    //一个用户有多个角色
    private List<Role> roles;
<!--定义封装Role和user的resultmap-->
    <resultMap id="userRole" type="user">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sex" column="sex"/>
        <result property="address" column="address"/>
        <collection property="roles"  ofType="role">
            <id property="roleId" column="role_id"/>
            <result property="roleName" column="role_name"/>
            <result property="roleDesc" column="role_desc"/>
        </collection>
    </resultMap>

autoResultMap 自动映射,此处别有洞天
通常配合 @TableField() 一起使用,确定映射规范性。

  • 情况一:不开启autoResultMap,即autoResultMap=false
    • 开启下划线转换(默认开启)
      库中查询结果列名 top_id —》topId
    • 关闭下划线转换
      库中查询结果列名 top_id —》top_id
  • 情况二: 开启autoResultMap,即autoResultMap=true
    此时映射,下划线转换 开不开都不受影响。默认会使用属性名作为映射。
    private Long top_id; 即映射规则 库top_id —》top_id
  • 情况三:若库中属性和字段名不同。则可以使用@TableField()固定。name_ip —》name_ip

    @TableField(value = “name_ip”) //不设定查询列的时候也默认该值。
    private String name;

excludeProperty 干啥都排除某属性。

@TableName(excludeProperty = "address,sex") //排除address和sex
public class User  {
    private Integer id;
    private String name;
    private String address;
    private String sex;
}

2.2 @TableId

Mybatis-plus之映射篇(部分注解使用)

2.3 @TableField

补充一下,exist = false 的时候,和 excludeProperty 干啥都排除某属性。是同一种效果。
Mybatis-plus之映射篇(部分注解使用)

以下注解不常用

2.4 @Version

乐观锁注解、标记 @Verison 在字段上

2.5 @EnumValue

通枚举类注解(注解在枚举字段上)

2.6 @TableLogic

表字段逻辑处理注解(逻辑删除)

2.7 @SqlParser

租户注解

2.8 @KeySequence

序列主键策略

3. 问题箱

3.1 什么情况下会出现查询结果为null呢?

结论
若查询结果有数据Total: 2,但是一条字段都映射不上,就会出现null
解决方案

  • 1. 关闭Mybatis-plus的驼峰下划线转换。
    mybatis-plus.configuration.map-underscore-to-camel-case= false。但是这个只能解决top_id的映射。
  • 2. 自定义映射,详情上看resultMap
  • 3. 自动映射,@TableName(autoResultMap = true)
    此时激活@TableField(value = "name_ip")name_ip能正确映射。
    top_id没有使用@TableField,所以默认属性名,top_id。对应,能正确映射。

若代码过多字段命名不规范,建议使用第三种。如图所示:
Mybatis-plus之映射篇(部分注解使用)

情景复现

@Date //一定要有lombok(不然自己写),因为映射是通过set方法放值的。
public class User1 {
    @TableId(type = IdType.AUTO)
    private Long top_id;
    @TableField(value = "name_ip")
    private String name;
    @TableField(value = "address")
    private String address;
    @TableField(value = "sex")
    private String sex;
}
@Repository
public interface UserGateway extends BaseMapper<User1> {
}
   @Autowired
    UserGateway userGateway;
    @Test
    void jpaTset(){
        QueryWrapper<User1> qu = new QueryWrapper<>();
        qu.select("top_id,name_ip");
        qu.in("name_ip", "牛一","牛二");
        List<User1> user1s=userGateway.selectList(qu);
        System.out.println(JSON.toJSONString(user1s));
    }

Mybatis-plus之映射篇(部分注解使用)

版权声明:程序员胖胖胖虎阿 发表于 2022年11月8日 下午11:16。
转载请注明:Mybatis-plus之映射篇(部分注解使用) | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...