定义

@Column 注解的作用是对数据库列的一些自定义设置。

  • name: 自定义与db column 列名的映射,默认值为 ""。
  • updateIfNull: 当生成更新语句的时候,是否更新null 的字段, 默认值为false。
  • tableOrAlias: 自定义的列的表明或别名, 主要作用于多表查询指定列来自于哪张表, 默认值为 ""。
  • insertIgnore: 在插入的时候直接忽略掉这个属性。
  • insertIfNull: 当生成插入语句的时候,是否插入null的字段,默认值为false。
  • jdbcType: 根据Mybatis官方文档,当插入更新可空列的时候,需要制定jdbcType.

name 属性

  • 在基本概览中我们讲到,默认遵循mapUnderscoreToCamelCase 规范。如果你不遵循这个规范,那么你需要使用。
  • 如果这个entity 是多表join 出来的,那么一定要使用这个注解去标注这个属性是从哪个表里面来的。

java code:

Class Student {
    // map to db column "id".
    @Column(name="id")
    private Integer studentId;
    // get/set ...
}

db table:

CREATE TABLE student(
    id    INT PRIMARY KEY,
)

updateIfNull 属性

这个属性设置代表,当作用于更新的时候,如果值是null 的时候是否要更新,类似于mybaits xml 更新操作。 如果设置成true 即为强制更新。

// map to db table student.
@Table(name = "student")
Class Student {
    private Integer studentId;

    // map to db column "description".
    @Column(name = "description", updateIfNull = true)
    private String studentDescription;
    // get/set ...
}

db table:

CREATE TABLE student(
    id            INT PRIMARY KEY,
    description    VARCHAR(100)
)

xml:

<update id="update">
        UPDATE student
        <set>
            <if test="pojo.id != null">`id` = #{pojo.id},</if>
             `description` = #{pojo.description}
        </set>
</update>

tableOrAlias 属性

当多表join的时候一定要使用这个属性。这个对于筛选特别重要,因为join 的时候是多表,我们必须指定这个列是查询哪个表的列,否则会报错。

举个列子,我们想筛选CategoryName 是Be 开头的,但是我们查询的是一个 Product 和 Category join 出来的view, 所以在最后的 output 中, 是(WHERE category.category_name like ?) 去 指定这个是来自category 表中(当然你也可以使用别名来表示category 表)

db table:

CREATE TABLE category (
  category_id   INT PRIMARY KEY,
  category_name VARCHAR (50) NOT NULL,
  description   VARCHAR (100)
);

CREATE TABLE product (
  product_id    INT PRIMARY KEY,
  category_id   INT NOT NULL,
  product_name  VARCHAR (50) NOT NULL,
  price         DECIMAL
);

Java code:

// ProductView entity.
public class ProductView {
    // these columns from "product" table.
    @Column(name = "product_id", tableOrAlias="product")
    private Long productID;
    @Column(name = "product_name", tableOrAlias="product")
    private String productName;
    @Column(name = "price", tableOrAlias="product")
    private BigDecimal price;

    // these columns from "category" table.
    @Column(name = "category_id", tableOrAlias="category")
    private Long categoryID;
    @Column(name = "category_name", tableOrAlias="category")
    private String categoryName;
    @Column(name = "description", tableOrAlias="category")
    private String description;

    // get/set ...
}

test case:

@Test
public void testStartWith() throws Exception {
        FilterDescriptor nameFilter =
                new FilterDescriptor(FilterCondition.AND, "categoryName", FilterOperator.START_WITH, "Be");
        Map<String, Object> queryParams =
                mybatisQueryProvider.getWhereQueryParamMap(
                        ProductView.class, "whereExpression", nameFilter);
        List<ProductView> productViews = northwindDao.getProductViewsByDynamic(queryParams);
        assertEquals("Beverages", productViews.get(0).getCategoryName());
}

console output:

JDBC Connection [ProxyConnection[PooledConnection[conn9: url=jdbc:h2:mem:default user=SA]]] will not be managed by Spring
==>  Preparing: SELECT * FROM product LEFT JOIN category ON product.category_id = category.category_id WHERE (category.category_name LIKE ?) 
==> Parameters: Be%(String)
<==    Columns: PRODUCT_ID, CATEGORY_ID, PRODUCT_NAME, PRICE, CATEGORY_ID, CATEGORY_NAME, DESCRIPTION
<==        Row: 1, 1, Northwind Traders Chai, 18.0000, 1, Beverages, test
<==      Total: 1

insertIgnore 属性

这个属性其实主要解决了一个自增的列的问题,当我们生成insert语句的时候,我们希望自增列不去设值,这个时候这个属性就非常有用,一般设置在自增ID列上。

insertIfNull 属性

这个属性和updateIfNull 类似,是否null 的时候插入这个属性。

jdbcType 属性

根据官方文档,当插入更新的时候需要指定jdbcType。

The JDBC Type from the list of supported types that follows this table. The JDBC type is only required for nullable columns upon insert, update or delete. This is a JDBC requirement, not an MyBatis one. So even if you were coding JDBC directly, you'd need to specify this type – but only for nullable values.

@Table(name = "product")
public class Product {
    @Column(name = "product_id", insertIgnore = true, jdbcType = JdbcType.INTEGER)
    private Integer productID;
    @Column(jdbcType = JdbcType.VARCHAR)
    private String productName;
    @Column(jdbcType = JdbcType.NUMERIC)
    private BigDecimal price;
    @Column(jdbcType = JdbcType.INTEGER)
    private Integer categoryID;
    // get /set ...
}

debug 插入语句

INSERT INTO product (product_name, category_id) VALUES (#{productName,jdbcType=VARCHAR}, #{categoryID,jdbcType=INTEGER})

results matching ""

    No results matching ""